Skip to content

[micro-opt] WeakHashSet can skip dereference if hash mismatch when traversing chains #26790

Description

@tanishiking

When I look at the flamegraphs generated for scala3-benchmarks, I found AppliedUniques.linkedListLoop / WeakHashSet.linkedListLoop accounts for several % (4-5%) on some benchmarks.

linkedListLoop is traversing on the chain (of separate chaining), for looking up an entry from the chain.

One obvious optimization I realized is that, currently we always dereference WeakReference on the chain, and compare it with an entry we're looking up, but we should be able to compare hash and skip if they doesn't match.

@tailrec
def linkedListLoop(entry: Entry[AppliedType] | Null): AppliedType = entry match
case null => addEntryAt(bucket, newType, h, oldHead)
case _ =>
val e = entry.get
if e != null && (e.tycon eq tycon) && e.args.eqElements(args) then e
else linkedListLoop(entry.tail)
linkedListLoop(oldHead)
end if

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions