Skip to content

Improve performance of ConcurrentReferenceHashMap creation #2051

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

Merged

Conversation

stsypanov
Copy link
Contributor

@stsypanov stsypanov commented Dec 13, 2018

I've discovered that on some workloads instantiation of CCRHM takes significant amount of time (e.g. loading interface-base projections in Spring Data JPA). Here's the patch that reduces map instantiation time:

//before
Benchmark                   Mode  Cnt    Score   Error  Units
concurrentReferenceHashMap  avgt  100  712,179 ± 7,233  ns/op

//after
Benchmark                   Mode  Cnt    Score   Error  Units
concurrentReferenceHashMap  avgt   50  496,598 ± 4,601  ns/op

Benchmark is very simple:

@Benchmark
public ConcurrentReferenceHashMap concurrentReferenceHashMap() {
  return new ConcurrentReferenceHashMap();
}

Key ideas behind the changes:

  • initialSize is the same at each loop iteration in constructor of ConcurrentReferenceHashMap, so expression 1 << calculateShift(initialCapacity, MAXIMUM_SEGMENT_SIZE); can be hoisted out of constructor of Segment and then out of the loop
  • same for resizeThreshold calculation
  • Segment.references declared as volatile so I suspect when new segment is written into ConcurrentReferenceHashMap.segments array the field segments must be loaded at each iteration due to HB semantics, same for this.segments.length. To avoid this I propose to populate ConcurrentReferenceHashMap.segments as local var and the write it to the field. Event if my assumption about HB is wrong reduction of interaction with fields helps us in interpreter mode.

@jhoeller
Copy link
Contributor

Good points, I'll merge those changes right away! Thanks for raising this.

@jhoeller jhoeller merged commit 112cc70 into spring-projects:master Dec 13, 2018
@stsypanov stsypanov deleted the enhance-concurrent-ref-hash-map branch January 3, 2019 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants