From 9078f296a108d76d388d8dc3a98bad01113d3805 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 11f0b55a8dbe..8b5b994d7b2f 100644 --- a/compiler/src/dotty/tools/dotc/core/Names.scala +++ b/compiler/src/dotty/tools/dotc/core/Names.scala @@ -221,13 +221,15 @@ object Names { private 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]?