Skip to content

When nested functions are inlined, boxing variables should be avoided #4522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Glavo opened this issue May 12, 2018 · 1 comment
Closed

When nested functions are inlined, boxing variables should be avoided #4522

Glavo opened this issue May 12, 2018 · 1 comment

Comments

@Glavo
Copy link
Contributor

Glavo commented May 12, 2018

For this code:

object C {
  def fun: Int = {
    var a = 10
    inline def f(arg: => Unit) = {
      a += 1
      arg
    }

    f {
      return a
    }
    a
  }
}

The decompiled code looks like this:

public final class C$ {
    public static final C$ MODULE$;

    static {
        new C$();
    }

    public C$() {
        MODULE$ = this;
    }

    public int fun() {
        IntRef a = IntRef.create(10);
        int var2 = a.elem + 1;
        a.elem = var2;
        return a.elem;
    }

    private void f$1(IntRef a$1, Function0 arg) {
        int var3 = a$1.elem + 1;
        a$1.elem = var3;
        arg.apply();
    }
}

a should not be boxed

@nicolasstucki
Copy link
Contributor

This is the correct transformation for this captured Int reference. Though local optimizations with -optimize do remove this extra box.

With -optimise:

def fun(): Int = 
      {
        var a: Int = 10
        {
          a = 1.+(a)
          return a
        }
        a: Int
      }

Without -optimise:

def fun(): Int = 
      {
        val a: scala.runtime.IntRef = scala.runtime.IntRef$#create(10)
        {
          {
            val ev$2: Int = a.elem.+(1)
            a.elem = ev$2
          }
          {
            return a.elem
          }
        }
        a.elem: Int
      }

allanrenucci added a commit that referenced this issue Aug 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants