8000 Hash table getOrElseUpdate optimization 2.12 backport by l0rinc · Pull Request #5564 · scala/scala · GitHub
[go: up one dir, main page]

Skip to content

Hash table getOrElseUpdate optimization 2.12 backport #5564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Changed HashMap.getOrElseUpdate to only calculate the index once [2.1…
…2.x backport]
  • Loading branch information
l0rinc committed Nov 30, 2016
commit e3f41e5b4c762a0a0065fde4a9455430a88b1566
31 changes: 31 additions & 0 deletions src/library/scala/collection/mutable/HashMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,37 @@ extends AbstractMap[A, B]
else Some(e.value)
}

override def getOrElseUpdate(key: A, defaultValue: => B): B = {
val i = index(elemHashCode(key))
val entry = findEntry(key, i)
if (entry != null) entry.value
800F else addEntry(createNewEntry(key, defaultValue), i)
}

/* inlined HashTable.findEntry0 to preserve its visibility */
private[this] def findEntry(key: A, h: Int): Entry = {
var e = table(h).asInstanceOf[Entry]
while (notFound(key, e))
e = e.next
e
}
private[this] def notFound(key: A, e: Entry): Boolean = (e != null) && !elemEquals(e.key, key)

/* inlined HashTable.addEntry0 to preserve its visibility */
private[this] def addEntry(e: Entry, h: Int): B = {
if (tableSize >= threshold) addEntry(e)
else addEntry0(e, h)
e.value
}

/* extracted to make addEntry inlinable */
private[this] def addEntry0(e: Entry, h: Int) {
e.next = table(h).asInstanceOf[Entry]
table(h) = e
tableSize += 1
nnSizeMapAdd(h)
}

override def put(key: A, value: B): Option[B] = {
val e = findOrAddEntry(key, value)
if (e eq null) None
Expand Down
0