From 112cc70231172176aef70d855890aeee3762563e Mon Sep 17 00:00:00 2001 From: stsypanov Date: Tue, 11 Dec 2018 18:02:19 +0200 Subject: [PATCH] Improve performance of ConcurrentReferenceHashMap creation --- .../util/ConcurrentReferenceHashMap.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/ConcurrentReferenceHashMap.java b/spring-core/src/main/java/org/springframework/util/ConcurrentReferenceHashMap.java index 802518e023..4e6de5b8f2 100644 --- a/spring-core/src/main/java/org/springframework/util/ConcurrentReferenceHashMap.java +++ b/spring-core/src/main/java/org/springframework/util/ConcurrentReferenceHashMap.java @@ -179,10 +179,13 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen int size = 1 << this.shift; this.referenceType = referenceType; int roundedUpSegmentCapacity = (int) ((initialCapacity + size - 1L) / size); - this.segments = (Segment[]) Array.newInstance(Segment.class, size); - for (int i = 0; i < this.segments.length; i++) { - this.segments[i] = new Segment(roundedUpSegmentCapacity); + int initialSize = 1 << calculateShift(roundedUpSegmentCapacity, MAXIMUM_SEGMENT_SIZE); + Segment[] segments = (Segment[]) Array.newInstance(Segment.class, size); + int resizeThreshold = (int) (initialSize * getLoadFactor()); + for (int i = 0; i < segments.length; i++) { + segments[i] = new Segment(initialSize, resizeThreshold); } + this.segments = segments; } @@ -481,11 +484,11 @@ public class ConcurrentReferenceHashMap extends AbstractMap implemen */ private int resizeThreshold; - public Segment(int initialCapacity) { + public Segment(int initialSize, int resizeThreshold) { this.referenceManager = createReferenceManager(); - this.initialSize = 1 << calculateShift(initialCapacity, MAXIMUM_SEGMENT_SIZE); - this.references = createReferenceArray(this.initialSize); - this.resizeThreshold = (int) (this.references.length * getLoadFactor()); + this.initialSize = initialSize; + this.references = createReferenceArray(initialSize); + this.resizeThreshold = resizeThreshold; } @Nullable