From 53cb026f409d43052b34c24c72f0024103840f39 Mon Sep 17 00:00:00 2001 From: jvican Date: Sun, 20 Nov 2016 20:29:38 +0100 Subject: [PATCH] Fix #1619: Reduce collision rate in NameTable The fix is consistent with Scalac: https://github.com/scala/scala/pull/5474/files. --- compiler/src/dotty/tools/dotc/core/Names.scala | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/Names.scala b/compiler/src/dotty/tools/dotc/core/Names.scala index e05e5a6b9674..deaa29ecf891 100644 --- a/compiler/src/dotty/tools/dotc/core/Names.scala +++ b/compiler/src/dotty/tools/dotc/core/Names.scala @@ -546,13 +546,15 @@ object Names { private[this] var size = 1 /** The hash of a name made of from characters cs[offset..offset+len-1]. */ - private def hashValue(cs: Array[Char], offset: Int, len: Int): Int = - if (len > 0) - (len * (41 * 41 * 41) + - cs(offset) * (41 * 41) + - cs(offset + len - 1) * 41 + - cs(offset + (len >> 1))) - else 0 + private def hashValue(cs: Array[Char], offset: Int, len: Int): Int = { + var i = offset + var hash = 0 + while (i < len + offset) { + hash = 31 * hash + cs(i) + i += 1 + } + hash + } /** Is (the ASCII representation of) name at given index equal to * cs[offset..offset+len-1]?