rorygraves / scalac_perf

The Scala programming language
http://www.scala-lang.org/
16 stars 3 forks source link

direct var reference #46

Open mkeskells opened 6 years ago

mkeskells commented 6 years ago

Nil has multiple levels of methods for var access

tail calls tl, tl accesses the field and we have an tl$access that isnt needed?

  // access flags 0x1
  // signature ()Lscala/collection/immutable/List<TB;>;
  // declaration: scala.collection.immutable.List<B> tl$access$1()
  public tl$access$1()Lscala/collection/immutable/List;
   L0
    LINENUMBER 451 L0
    ALOAD 0
    GETFIELD scala/collection/immutable/$colon$colon.tl : Lscala/collection/immutable/List;
    ARETURN
   L1
    LOCALVARIABLE this Lscala/collection/immutable/$colon$colon; L0 L1 0
    MAXSTACK = 1
    MAXLOCALS = 1

  // access flags 0x1
  // signature ()Lscala/collection/immutable/List<TB;>;
  // declaration: scala.collection.immutable.List<B> tl()
  public tl()Lscala/collection/immutable/List;
   L0
    LINENUMBER 451 L0
    ALOAD 0
    GETFIELD scala/collection/immutable/$colon$colon.tl : Lscala/collection/immutable/List;
    ARETURN
   L1
    LOCALVARIABLE this Lscala/collection/immutable/$colon$colon; L0 L1 0
    MAXSTACK = 1
    MAXLOCALS = 1

  // access flags 0x1
  // signature (Lscala/collection/immutable/List<TB;>;)V
  // declaration: void tl_$eq(scala.collection.immutable.List<B>)
  public tl_$eq(Lscala/collection/immutable/List;)V
    // parameter final  x$1
   L0
    LINENUMBER 451 L0
    ALOAD 0
    ALOAD 1
    PUTFIELD scala/collection/immutable/$colon$colon.tl : Lscala/collection/immutable/List;
    RETURN
   L1
    LOCALVARIABLE this Lscala/collection/immutable/$colon$colon; L0 L1 0
    LOCALVARIABLE x$1 Lscala/collection/immutable/List; L0 L1 1
    MAXSTACK = 2
    MAXLOCALS = 2

  // access flags 0x1
  // signature ()Lscala/collection/immutable/List<TB;>;
  // declaration: scala.collection.immutable.List<B> tail()
  public tail()Lscala/collection/immutable/List;
   L0
    LINENUMBER 452 L0
    ALOAD 0
    INVOKEVIRTUAL scala/collection/immutable/$colon$colon.tl ()Lscala/collection/immutable/List;
    ARETURN
   L1
    LOCALVARIABLE this Lscala/collection/immutable/$colon$colon; L0 L1 0
    MAXSTACK = 1
    MAXLOCALS = 1
retronym commented 6 years ago

This is a bit of a corner case because of the accessor created for the non-public case class parameter.

@SerialVersionUID(509929039250432923L) // value computed by serialver for 2.11.2, annotation added in 2.11.4
final case class ::[B](override val head: B, private[scala] var tl: List[B]) extends List[B] {
  override def tail : List[B] = this.tl : @inline
  override def isEmpty: Boolean = false
}

Results in:

  public scala.collection.immutable.List<B> tail();
    Code:
       0: aload_0
       1: getfield      #23                 // Field tl:Lscala/collection/immutable/List;
       4: areturn

Which might help some unlucky code that got caught at the threshhold of -XX:MaxInlineLevel.