miniboxing-plugin icon indicating copy to clipboard operation
miniboxing-plugin copied to clipboard

Follow-up on #61 - the output is suboptimal

Open VladUreche opened this issue 11 years ago • 2 comments

https://github.com/miniboxing/miniboxing-plugin/issues/61

Too many conversions from Long to Int.

VladUreche avatar Dec 29 '13 01:12 VladUreche

To show this:

$ cat gh-bug-61.scala 
package miniboxing.tests.compile.bug61

class C[@miniboxed T] {
  def foo(t: T): T = ???
}

class D extends C[Int] {
  override def foo(t: Int): Int = t + t * t - t + 3 
}

$ mb-scalac gh-bug-61.scala -Xprint:minibox-commit -P:minibox:Yone-way
[[syntax trees at end of            minibox-commit]] // gh-bug-61.scala
package miniboxing.tests.compile.bug61 {
  abstract trait C[@miniboxed T] extends Object {
    def foo(t: T): T;
    def foo_J(T_TypeTag: Byte, t: Long): Long
  };
  ...
  class D extends miniboxing.tests.compile.bug61.C_J[Int] {
    ...
    override def foo(t: Int): Int = MiniboxConversions.this.minibox2int(D.this.foo_J(5, MiniboxConversions.this.int2minibox(t)));
    override def foo_J(T_TypeTag: Byte, t: Long): Long = int2minibox(minibox2int(t).+(minibox2int(t).*(minibox2int(t))).-(minibox2int(t)).+(3))
  }
}

In a more optimized translation, foo_J should just be:

    override def foo_J(T_TypeTag: Byte, _t: Long): Long = {
      val t: Int = minibox2int(t)
      int2minibox(_t.+(t.*(t)).-(t).+(3))
    }

VladUreche avatar Nov 21 '14 22:11 VladUreche

At least we don't change Int to Long:

$ cat question1.scala 
package miniboxing.tests.compile.question1

class C[@miniboxed T] {
  def foo(t: T): T = t
}

class D extends C[Int] {
  override
  def foo(t: Int): Int = {
    val t1: Int = t  // these two values should keep the
    val t2: Int = t1 // type Int, not become Long/Double
    t2
  }
}

$ mb-scalac question1.scala -Xprint:minibox-commit -P:minibox:Yone-way
[[syntax trees at end of            minibox-commit]] // question1.scala
package miniboxing.tests.compile.question1 {
  abstract trait C[@miniboxed T] extends Object {
    def foo(t: T): T;
    def foo_J(T_TypeTag: Byte, t: Long): Long
  };
  ...
  class D extends miniboxing.tests.compile.question1.C_J[Int] {
    ...
    override def foo(t: Int): Int = minibox2int(D.this.foo_J(5, int2minibox(t)));
    override def foo_J(T_TypeTag: Byte, t: Long): Long = {
      val t1: Int = MiniboxConversions.this.minibox2int(t);
      val t2: Int = t1;
      MiniboxConversions.this.int2minibox(t2)
    }
  }
}

TODO: Add this as a test case!

VladUreche avatar Nov 21 '14 22:11 VladUreche