Skip to content

Optimise simple tuple extraction #23224

Open
@noti0na1

Description

@noti0na1

Compiler version

3.7.2-RC1-bin-20250520-baac46c-NIGHTLY-git-baac46c

Minimized example

class Test:
  def f1: (Int, Int) = (1, 2)

  def test1 =
    val (a, b) = f1
    a + b

Output

After typer: scala compile --server=false -S 3.nightly -Xprint:typer tuples.scala

package <empty> {
  class Test() extends Object() {
    def f1: Tuple2[Int, Int] = Tuple2.apply[Int, Int](1, 2)
    def test1: Int =
      {
        val $1$: (Int, Int) =
          this.f1:(Int, Int) @unchecked match 
            {
              case Tuple2.unapply[Int, Int](a @ _, b @ _) =>
                Tuple2.apply[Int, Int](a, b)
            }
        val a: Int = $1$._1
        val b: Int = $1$._2
        a + b
      }
  }
}

After genBCode: scala compile --server=false -S 3.nightly -Xprint:genBCode tuples.scala

package <empty> {
  @SourceFile("tuples.scala") class Test extends Object {
    def <init>(): Unit =
      {
        super()
        ()
      }
    def f1(): Tuple2 = new Tuple2$mcII$sp(1, 2)
    def test1(): Int =
      {
        val $1$: Tuple2 =
          matchResult1[Tuple2]: 
            {
              case val x1: Tuple2 = this.f1():Tuple2
              if x1 ne null then
                {
                  case val a: Int = x1._1$mcI$sp()
                  case val b: Int = x1._2$mcI$sp()
                  return[matchResult1] new Tuple2$mcII$sp(a, b)
                }
               else ()
              throw new MatchError(x1)
            }
        val a: Int = $1$._1$mcI$sp()
        val b: Int = $1$._2$mcI$sp()
        a + b
      }
  }
}

Expectation

Calling _i on the tuple directly:

val $1$: (Int, Int) = this.f1:(Int, Int)
val a: Int = $1$._1
val b: Int = $1$._2
a + b

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:desugarDesugaring happens after parsing but before typing, see desugar.scalaitype:performance

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions