diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/collections/BoundedConcurrentHashMap.java b/hibernate-core/src/main/java/org/hibernate/internal/util/collections/BoundedConcurrentHashMap.java index 3a139cd776..727ef56207 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/collections/BoundedConcurrentHashMap.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/collections/BoundedConcurrentHashMap.java @@ -270,10 +270,7 @@ public class BoundedConcurrentHashMap extends AbstractMap HashEntry createNewEntry(K key, int hash, HashEntry next, V value); /** - * Invokes eviction policy algorithm and returns set of evicted entries. - *

- *

- * Set cannot be null but could possibly be an empty set. + * Invokes eviction policy algorithm. */ void execute(); @@ -339,11 +336,11 @@ public class BoundedConcurrentHashMap extends AbstractMap private final float batchThresholdFactor; private final Set> evicted; - public LRU(Segment s, int capacity, float lf, int maxBatchSize, float batchThresholdFactor) { + LRU(Segment s, int capacity, float lf, int maxBatchSize, float batchThresholdFactor) { super( capacity, lf, true ); this.segment = s; this.trimDownSize = capacity; - this.maxBatchQueueSize = maxBatchSize > MAX_BATCH_SIZE ? MAX_BATCH_SIZE : maxBatchSize; + this.maxBatchQueueSize = Math.min( maxBatchSize, MAX_BATCH_SIZE ); this.batchThresholdFactor = batchThresholdFactor; this.accessQueue = new ConcurrentLinkedQueue<>(); this.evicted = new HashSet<>(); @@ -425,7 +422,7 @@ public class BoundedConcurrentHashMap extends AbstractMap return Eviction.LRU; } - protected boolean isAboveThreshold() { + private boolean isAboveThreshold() { return size() > trimDownSize; } @@ -825,9 +822,8 @@ public class BoundedConcurrentHashMap extends AbstractMap * Removes this entry from the cache. This operation is not specified in * the paper, which does not account for forced eviction. */ - private V remove() { + private void remove() { boolean wasHot = ( state == Recency.LIR_RESIDENT ); - V result = value; LIRSHashEntry end = owner != null ? owner.queueEnd() : null; evict(); @@ -837,8 +833,6 @@ public class BoundedConcurrentHashMap extends AbstractMap end.migrateToStack(); } } - - return result; } } @@ -894,7 +888,6 @@ public class BoundedConcurrentHashMap extends AbstractMap * accessed more recently than the last hot entry are present in the stack). * The stack is ordered by recency, with its most recently accessed entry * at the top, and its least recently accessed entry at the bottom. - *

*

  • The LIRS queue, Q, which enqueues all cold entries for eviction. Cold * entries (by definition in the queue) may be absent from the stack (due to * pruning of the stack). Cold entries are added to the end of the queue @@ -919,11 +912,11 @@ public class BoundedConcurrentHashMap extends AbstractMap private int hotSize; - public LIRS(Segment s, int capacity, int maxBatchSize, float batchThresholdFactor) { + LIRS(Segment s, int capacity, int maxBatchSize, float batchThresholdFactor) { this.segment = s; this.maximumSize = capacity; this.maximumHotSize = calculateLIRSize( capacity ); - this.maxBatchQueueSize = maxBatchSize > MAX_BATCH_SIZE ? MAX_BATCH_SIZE : maxBatchSize; + this.maxBatchQueueSize = Math.min( maxBatchSize, MAX_BATCH_SIZE ); this.batchThresholdFactor = batchThresholdFactor; this.accessQueue = new ConcurrentLinkedQueue>(); this.accessQueueSize = new AtomicInteger(); @@ -997,8 +990,8 @@ public class BoundedConcurrentHashMap extends AbstractMap } /* - * Invoked without holding a lock on Segment - */ + * Invoked without holding a lock on Segment + */ @Override public boolean onEntryHit(HashEntry e) { accessQueue.add( (LIRSHashEntry) e ); @@ -1168,7 +1161,7 @@ public class BoundedConcurrentHashMap extends AbstractMap } @SuppressWarnings("unchecked") - static Segment[] newArray(int i) { + private static Segment[] newArray(int i) { return new Segment[i]; } @@ -1359,68 +1352,6 @@ public class BoundedConcurrentHashMap extends AbstractMap } } - void rehash() { - HashEntry[] oldTable = table; - int oldCapacity = oldTable.length; - if ( oldCapacity >= MAXIMUM_CAPACITY ) { - return; - } - - /* - * Reclassify nodes in each list to new Map. Because we are - * using power-of-two expansion, the elements from each bin - * must either stay at same index, or move with a power of two - * offset. We eliminate unnecessary node creation by catching - * cases where old nodes can be reused because their next - * fields won't change. Statistically, at the default - * threshold, only about one-sixth of them need cloning when - * a table doubles. The nodes they replace will be garbage - * collectable as soon as they are no longer referenced by any - * reader thread that may be in the midst of traversing table - * right now. - */ - - HashEntry[] newTable = HashEntry.newArray( oldCapacity << 1 ); - threshold = (int) ( newTable.length * loadFactor ); - int sizeMask = newTable.length - 1; - for ( int i = 0; i < oldCapacity; i++ ) { - // We need to guarantee that any existing reads of old Map can - // proceed. So we cannot yet null out each bin. - HashEntry e = oldTable[i]; - - if ( e != null ) { - HashEntry next = e.next; - int idx = e.hash & sizeMask; - - // Single node on list - if ( next == null ) { - newTable[idx] = e; - } - else { - // Reuse trailing consecutive sequence at same slot - HashEntry lastRun = e; - int lastIdx = idx; - for ( HashEntry last = next; last != null; last = last.next ) { - int k = last.hash & sizeMask; - if ( k != lastIdx ) { - lastIdx = k; - lastRun = last; - } - } - newTable[lastIdx] = lastRun; - - // Clone all remaining nodes - for ( HashEntry p = e; p != lastRun; p = p.next ) { - int k = p.hash & sizeMask; - HashEntry n = newTable[k]; - newTable[k] = eviction.createNewEntry( p.key, p.hash, n, p.value ); - } - } - } - } - table = newTable; - } - /** * Remove; match on key only if value null, else match both. */ @@ -1535,7 +1466,7 @@ public class BoundedConcurrentHashMap extends AbstractMap concurrencyLevel = Math.max( concurrencyLevel, 1 ); // concurrencyLevel cannot be less than 1 // minimum two elements per segment - if ( capacity < concurrencyLevel * 2 && capacity != 1 ) { + if ( capacity < concurrencyLevel << 1 && capacity != 1 ) { throw new IllegalArgumentException( "Maximum capacity has to be at least twice the concurrencyLevel" ); } @@ -2004,28 +1935,6 @@ public class BoundedConcurrentHashMap extends AbstractMap return es != null ? es : ( entrySet = new EntrySet() ); } - /** - * Returns an enumeration of the keys in this table. - * - * @return an enumeration of the keys in this table - * - * @see #keySet() - */ - public Enumeration keys() { - return new KeyIterator(); - } - - /** - * Returns an enumeration of the values in this table. - * - * @return an enumeration of the values in this table - * - * @see #values() - */ - public Enumeration elements() { - return new ValueIterator(); - } - /* ---------------- Iterator Support -------------- */ abstract class HashIterator {