Skip to content

Commit 9f431e2

Browse files
committed
Polishing
(cherry picked from commit 8965d9b)
1 parent cba8f22 commit 9f431e2

1 file changed

Lines changed: 35 additions & 40 deletions

File tree

spring-core/src/main/java/org/springframework/util/ConcurrentLruCache.java

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public V get(K key) {
102102
if (this.capacity == 0) {
103103
return this.generator.apply(key);
104104
}
105-
final Node<K, V> node = this.cache.get(key);
105+
Node<K, V> node = this.cache.get(key);
106106
if (node == null) {
107107
V value = this.generator.apply(key);
108108
put(key, value);
@@ -115,9 +115,9 @@ public V get(K key) {
115115
private void put(K key, V value) {
116116
Assert.notNull(key, "key must not be null");
117117
Assert.notNull(value, "value must not be null");
118-
final CacheEntry<V> cacheEntry = new CacheEntry<>(value, CacheEntryState.ACTIVE);
119-
final Node<K, V> node = new Node<>(key, cacheEntry);
120-
final Node<K, V> prior = this.cache.putIfAbsent(node.key, node);
118+
CacheEntry<V> cacheEntry = new CacheEntry<>(value, CacheEntryState.ACTIVE);
119+
Node<K, V> node = new Node<>(key, cacheEntry);
120+
Node<K, V> prior = this.cache.putIfAbsent(node.key, node);
121121
if (prior == null) {
122122
processWrite(new AddTask(node));
123123
}
@@ -128,7 +128,7 @@ private void put(K key, V value) {
128128

129129
private void processRead(Node<K, V> node) {
130130
boolean drainRequested = this.readOperations.recordRead(node);
131-
final DrainStatus status = this.drainStatus.get();
131+
DrainStatus status = this.drainStatus.get();
132132
if (status.shouldDrainBuffers(drainRequested)) {
133133
drainOperations();
134134
}
@@ -229,7 +229,7 @@ public boolean contains(K key) {
229229
*/
230230
@Nullable
231231
public boolean remove(K key) {
232-
final Node<K, V> node = this.cache.remove(key);
232+
Node<K, V> node = this.cache.remove(key);
233233
if (node == null) {
234234
return false;
235235
}
@@ -238,27 +238,29 @@ public boolean remove(K key) {
238238
return true;
239239
}
240240

241-
/*
241+
/**
242242
* Transition the node from the {@code active} state to the {@code pending removal} state,
243243
* if the transition is valid.
244244
*/
245245
private void markForRemoval(Node<K, V> node) {
246-
for (; ; ) {
247-
final CacheEntry<V> current = node.get();
246+
while (true) {
247+
CacheEntry<V> current = node.get();
248248
if (!current.isActive()) {
249249
return;
250250
}
251-
final CacheEntry<V> pendingRemoval = new CacheEntry<>(current.value, CacheEntryState.PENDING_REMOVAL);
251+
CacheEntry<V> pendingRemoval = new CacheEntry<>(current.value, CacheEntryState.PENDING_REMOVAL);
252252
if (node.compareAndSet(current, pendingRemoval)) {
253253
return;
254254
}
255255
}
256256
}
257257

258+
258259
/**
259260
* Write operation recorded when a new entry is added to the cache.
260261
*/
261262
private final class AddTask implements Runnable {
263+
262264
final Node<K, V> node;
263265

264266
AddTask(Node<K, V> node) {
@@ -276,22 +278,22 @@ public void run() {
276278

277279
private void evictEntries() {
278280
while (currentSize.get() > capacity) {
279-
final Node<K, V> node = evictionQueue.poll();
281+
Node<K, V> node = evictionQueue.poll();
280282
if (node == null) {
281283
return;
282284
}
283285
cache.remove(node.key, node);
284286
markAsRemoved(node);
285287
}
286288
}
287-
288289
}
289290

290291

291292
/**
292293
* Write operation recorded when an entry is removed to the cache.
293294
*/
294295
private final class RemovalTask implements Runnable {
296+
295297
final Node<K, V> node;
296298

297299
RemovalTask(Node<K, V> node) {
@@ -311,7 +313,7 @@ public void run() {
311313
*/
312314
private enum DrainStatus {
313315

314-
/*
316+
/**
315317
* No drain operation currently running.
316318
*/
317319
IDLE {
@@ -321,7 +323,7 @@ boolean shouldDrainBuffers(boolean delayable) {
321323
}
322324
},
323325

324-
/*
326+
/**
325327
* A drain operation is required due to a pending write modification.
326328
*/
327329
REQUIRED {
@@ -331,7 +333,7 @@ boolean shouldDrainBuffers(boolean delayable) {
331333
}
332334
},
333335

334-
/*
336+
/**
335337
* A drain operation is in progress.
336338
*/
337339
PROCESSING {
@@ -368,12 +370,6 @@ private static final class ReadOperations<K, V> {
368370

369371
private static final int BUFFER_COUNT = detectNumberOfBuffers();
370372

371-
private static int detectNumberOfBuffers() {
372-
int availableProcessors = Runtime.getRuntime().availableProcessors();
373-
int nextPowerOfTwo = 1 << (Integer.SIZE - Integer.numberOfLeadingZeros(availableProcessors - 1));
374-
return Math.min(4, nextPowerOfTwo);
375-
}
376-
377373
private static final int BUFFERS_MASK = BUFFER_COUNT - 1;
378374

379375
private static final int MAX_PENDING_OPERATIONS = 32;
@@ -384,30 +380,25 @@ private static int detectNumberOfBuffers() {
384380

385381
private static final int BUFFER_INDEX_MASK = BUFFER_SIZE - 1;
386382

387-
/*
388-
* Number of operations recorded, for each buffer
389-
*/
383+
// Number of operations recorded, for each buffer
390384
private final AtomicLongArray recordedCount = new AtomicLongArray(BUFFER_COUNT);
391385

392-
/*
393-
* Number of operations read, for each buffer
394-
*/
386+
// Number of operations read, for each buffer
395387
private final long[] readCount = new long[BUFFER_COUNT];
396388

397-
/*
398-
* Number of operations processed, for each buffer
399-
*/
389+
// Number of operations processed, for each buffer
400390
private final AtomicLongArray processedCount = new AtomicLongArray(BUFFER_COUNT);
401391

402392
@SuppressWarnings("rawtypes")
403393
private final AtomicReferenceArray<Node<K, V>>[] buffers = new AtomicReferenceArray[BUFFER_COUNT];
404394

405395
private final EvictionQueue<K, V> evictionQueue;
406396

397+
@SuppressWarnings("rawtypes")
407398
ReadOperations(EvictionQueue<K, V> evictionQueue) {
408399
this.evictionQueue = evictionQueue;
409400
for (int i = 0; i < BUFFER_COUNT; i++) {
410-
this.buffers[i] = new AtomicReferenceArray<>(BUFFER_SIZE);
401+
this.buffers[i] = new AtomicReferenceArray(BUFFER_SIZE);
411402
}
412403
}
413404

@@ -459,6 +450,12 @@ private void drainReadBuffer(int bufferIndex) {
459450
}
460451
this.processedCount.lazySet(bufferIndex, writeCount);
461452
}
453+
454+
private static int detectNumberOfBuffers() {
455+
int availableProcessors = Runtime.getRuntime().availableProcessors();
456+
int nextPowerOfTwo = 1 << (Integer.SIZE - Integer.numberOfLeadingZeros(availableProcessors - 1));
457+
return Math.min(4, nextPowerOfTwo);
458+
}
462459
}
463460

464461

@@ -544,10 +541,9 @@ Node<K, V> poll() {
544541
if (this.first == null) {
545542
return null;
546543
}
547-
final Node<K, V> f = this.first;
548-
final Node<K, V> next = f.getNext();
544+
Node<K, V> f = this.first;
545+
Node<K, V> next = f.getNext();
549546
f.setNext(null);
550-
551547
this.first = next;
552548
if (next == null) {
553549
this.last = null;
@@ -566,13 +562,12 @@ void add(Node<K, V> e) {
566562
}
567563

568564
private boolean contains(Node<K, V> e) {
569-
return (e.getPrevious() != null) || (e.getNext() != null) || (e == this.first);
565+
return (e.getPrevious() != null || e.getNext() != null || e == this.first);
570566
}
571567

572-
private void linkLast(final Node<K, V> e) {
573-
final Node<K, V> l = this.last;
568+
private void linkLast(Node<K, V> e) {
569+
Node<K, V> l = this.last;
574570
this.last = e;
575-
576571
if (l == null) {
577572
this.first = e;
578573
}
@@ -583,8 +578,8 @@ private void linkLast(final Node<K, V> e) {
583578
}
584579

585580
private void unlink(Node<K, V> e) {
586-
final Node<K, V> prev = e.getPrevious();
587-
final Node<K, V> next = e.getNext();
581+
Node<K, V> prev = e.getPrevious();
582+
Node<K, V> next = e.getNext();
588583
if (prev == null) {
589584
this.first = next;
590585
}

0 commit comments

Comments
 (0)