rorygraves / scalac_perf

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

remove unneeded builders for empty results #26

Open mkeskells opened 6 years ago

mkeskells commented 6 years ago

a common pattern in the collection code is to create a builder, iterate a collection adding to the builder, and then call result

for the special case of a empty collection this can be optimised to call empty without the generation of the builder

e.g. in GenTraversableFactory

def tabulate[A](n: Int)(f: Int => A): CC[A] = {
    val b = newBuilder[A]
    b.sizeHint(n)
    var i = 0
    while (i < n) {
      b += f(i)
      i += 1
    }
    b.result()
  }

could be

def tabulate[A](n: Int)(f: Int => A): CC[A] = {
 if (n == 0) empty else {
    val b = newBuilder[A]
    b.sizeHint(n)
    var i = 0
    while (i < n) {
      b += f(i)
      i += 1
    }
    b.result()
  }
}

(and the same for concat, iterate and fill in GenTraversableFactory) and some others in other places in the libaray code

this optimisation has occurred in several places, but several have not. Should be reviewed, profiled etc

Probably several PRs

ackratos commented 6 years ago

if no new joiner want practice with this issue, I'd like to take it:)

pkukielka commented 6 years ago

Hi, could you please assign me to this issue?