Repository: groovy Updated Branches: refs/heads/master 8e6dd80e1 -> 59f619b9d
Improve the performance of `ConcurrentCommonCache` further(3.0.0+ Only) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/59f619b9 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/59f619b9 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/59f619b9 Branch: refs/heads/master Commit: 59f619b9dd2824e487f5e82a140a2e18eb863234 Parents: 8e6dd80 Author: danielsun1106 <[email protected]> Authored: Sun Mar 4 02:38:41 2018 +0800 Committer: danielsun1106 <[email protected]> Committed: Sun Mar 4 02:38:41 2018 +0800 ---------------------------------------------------------------------- .../runtime/memoize/ConcurrentCommonCache.java | 22 +++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/59f619b9/src/main/java/org/codehaus/groovy/runtime/memoize/ConcurrentCommonCache.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/codehaus/groovy/runtime/memoize/ConcurrentCommonCache.java b/src/main/java/org/codehaus/groovy/runtime/memoize/ConcurrentCommonCache.java index a758eb2..79b622d 100644 --- a/src/main/java/org/codehaus/groovy/runtime/memoize/ConcurrentCommonCache.java +++ b/src/main/java/org/codehaus/groovy/runtime/memoize/ConcurrentCommonCache.java @@ -114,19 +114,31 @@ public class ConcurrentCommonCache<K, V> implements EvictableCache<K, V>, ValueC public V getAndPut(K key, ValueProvider<? super K, ? extends V> valueProvider, boolean shouldCache) { V value; - long stamp = sl.readLock(); - try { - value = commonCache.get(key); + // try optimistic read first, which is non-blocking + long optimisticReadStamp = sl.tryOptimisticRead(); + value = commonCache.get(key); + if (sl.validate(optimisticReadStamp)) { if (null != convertValue(value)) { return value; } + } + + long stamp = sl.readLock(); + try { + // if stale, read again + if (!sl.validate(optimisticReadStamp)) { + value = commonCache.get(key); + if (null != convertValue(value)) { + return value; + } + } - long ws = sl.tryConvertToWriteLock(stamp); + long ws = sl.tryConvertToWriteLock(stamp); // the new local variable `ws` is necessary here! if (0L == ws) { // Failed to convert read lock to write lock sl.unlockRead(stamp); stamp = sl.writeLock(); - // try to find the cached value again + // try to read again value = commonCache.get(key); if (null != convertValue(value)) { return value;
