diff options
author | Grzegorz Kossakowski <grzegorz.kossakowski@gmail.com> | 2012-08-14 12:57:51 +0200 |
---|---|---|
committer | Grzegorz Kossakowski <grzegorz.kossakowski@gmail.com> | 2012-08-20 08:11:07 +0100 |
commit | 67756313a1f4e30aa62d5e5553fc3a9dff788d57 (patch) | |
tree | 4345556f1917cacd271f4da0c79702c0e922e2c6 /src/library | |
parent | b2f8acda7586a992bfe2827e439e572eb42e917a (diff) | |
download | scala-67756313a1f4e30aa62d5e5553fc3a9dff788d57.tar.gz scala-67756313a1f4e30aa62d5e5553fc3a9dff788d57.tar.bz2 scala-67756313a1f4e30aa62d5e5553fc3a9dff788d57.zip |
Don't allocate closure in List.hashCode.
Provided specialized version of hashing algorithm
for List that does not allocate the closure.
This saves us 124k closures allocated in a compiler
when compiling `Vector.scala` from std library.
That's aroudn 5% of all closures allocated for that
compiler run.
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/util/hashing/MurmurHash3.scala | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/src/library/scala/util/hashing/MurmurHash3.scala b/src/library/scala/util/hashing/MurmurHash3.scala index 3efd5b5e72..1cfb8276fe 100644 --- a/src/library/scala/util/hashing/MurmurHash3.scala +++ b/src/library/scala/util/hashing/MurmurHash3.scala @@ -157,6 +157,20 @@ private[hashing] class MurmurHash3 { // Finalization finalizeHash(h, data.length) } + + final def listHash(xs: collection.immutable.List[_], seed: Int): Int = { + var n = 0 + var h = seed + var elems = xs + while (!elems.isEmpty) { + val head = elems.head + val tail = elems.tail + h = mix(h, head.##) + n += 1 + elems = tail + } + finalizeHash(h, n) + } } /** @@ -199,7 +213,11 @@ object MurmurHash3 extends MurmurHash3 { /** To offer some potential for optimization. */ - def seqHash(xs: collection.Seq[_]): Int = orderedHash(xs, seqSeed) + def seqHash(xs: collection.Seq[_]): Int = xs match { + case xs: List[_] => listHash(xs, seqSeed) + case xs => orderedHash(xs, seqSeed) + } + def mapHash(xs: collection.Map[_, _]): Int = unorderedHash(xs, mapSeed) def setHash(xs: collection.Set[_]): Int = unorderedHash(xs, setSeed) |