rorygraves / scalac_perf

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

bytecode for varargs #21

Open mkeskells opened 7 years ago

mkeskells commented 7 years ago

def r0: Regex = new Regex(toString)

generates

  // access flags 0x1
  public r0()Lscala/util/matching/Regex;
   L0
    LINENUMBER 12 L0
    NEW scala/util/matching/Regex
    DUP
    ALOAD 0
    INVOKEVIRTUAL scala/collection/immutable/XX.toString ()Ljava/lang/String;
    GETSTATIC scala/Predef$.MODULE$ : Lscala/Predef$;
    ICONST_0
    ANEWARRAY java/lang/String
    CHECKCAST [Ljava/lang/Object;
    INVOKEVIRTUAL scala/Predef$.wrapRefArray ([Ljava/lang/Object;)Lscala/collection/mutable/WrappedArray;
    INVOKESPECIAL scala/util/matching/Regex.<init> (Ljava/lang/String;Lscala/collection/Seq;)V
    ARETURN
   L1
    LOCALVARIABLE this Lscala/collection/immutable/XX; L0 L1 0
    MAXSTACK = 5
    MAXLOCALS = 1

but it could generate
  // access flags 0x1
  public r()Lscala/util/matching/Regex;
   L0
    LINENUMBER 9 L0
    NEW scala/util/matching/Regex
    DUP
    ALOAD 0
    INVOKEVIRTUAL scala/collection/immutable/XX.toString ()Ljava/lang/String;
    GETSTATIC scala/collection/mutable/WrappedArray$.MODULE$ : Lscala/collection/mutable/WrappedArray$;
    INVOKEVIRTUAL scala/collection/mutable/WrappedArray$.empty ()Lscala/collection/mutable/WrappedArray;
    INVOKESPECIAL scala/util/matching/Regex.<init> (Ljava/lang/String;Lscala/collection/Seq;)V
    ARETURN
   L1
    LOCALVARIABLE this Lscala/collection/immutable/XX; L0 L1 0
    MAXSTACK = 4
    MAXLOCALS = 1

as it is implicitely def r: Regex = new Regex(toString, mutable.WrappedArray.empty[String] : _*)

we could go firther than that and generate the optimsed target, but that is harder

ft655508 commented 7 years ago

finding:

  1. the code we need to change about the WrappedArray creation is in UnCurry.scala, transformVarargs.
  2. for the empty case, current implementation won't generate empty array everytime, it uses Nil as the sequence (no extra array and WrappedArray created).

question:

  1. equality ofRef0, ofRef1, ofRef2... need to compare with ofRef
  2. we only save some cost to create array, we still need to create the wrapped array, which fits the (X*) ==> (Seq[X]) generated by the scala compiler
  3. generic wrapped array - handled but need to spend some more time to see what cases it covers
  4. performance test - compile akka/actor 20s vs original 22s but we need to recompile the scalac compiler with the new built version, then compile the other projects, because the change impatcs more on runtime than the compile time