diff --git a/src/main/java/org/elasticsearch/common/DefaultCacheRecycler.java b/src/main/java/org/elasticsearch/common/DefaultCacheRecycler.java index 656025f2fa0..92c809891a8 100644 --- a/src/main/java/org/elasticsearch/common/DefaultCacheRecycler.java +++ b/src/main/java/org/elasticsearch/common/DefaultCacheRecycler.java @@ -42,7 +42,6 @@ import org.elasticsearch.common.util.concurrent.ConcurrentCollections; public class DefaultCacheRecycler implements Recycler { - private static final int QUEUE_MAX_SIZE = 256; @Override public void clear() { @@ -92,11 +91,7 @@ public class DefaultCacheRecycler implements Recycler { @Override @SuppressWarnings("unchecked") public ExtTHashMap popHashMap() { - Queue ref = hashMap.get(); - if (ref == null) { - return new ExtTHashMap(); - } - ExtTHashMap map = ref.poll(); + ExtTHashMap map = pop(hashMap); if (map == null) { return new ExtTHashMap(); } @@ -108,15 +103,8 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public void pushHashMap(ExtTHashMap map) { - Queue ref = hashMap.get(); - if (ref == null) { - ref = ConcurrentCollections.newQueue(); - hashMap.set(ref); - } - if (ref.size() < QUEUE_MAX_SIZE) { - map.clear(); - ref.add(map); - } + map.clear(); + push(hashMap, map); } // ----- THashSet ----- @@ -129,11 +117,7 @@ public class DefaultCacheRecycler implements Recycler { @Override @SuppressWarnings("unchecked") public THashSet popHashSet() { - Queue ref = hashSet.get(); - if (ref == null) { - return new THashSet(); - } - THashSet set = ref.poll(); + THashSet set = pop(hashSet); if (set == null) { return new THashSet(); } @@ -145,15 +129,8 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public void pushHashSet(THashSet map) { - Queue ref = hashSet.get(); - if (ref == null) { - ref = ConcurrentCollections.newQueue(); - hashSet.set(ref); - } - if (ref.size() < QUEUE_MAX_SIZE) { - map.clear(); - ref.add(map); - } + map.clear(); + push(hashSet, map); } // ------ ExtTDoubleObjectHashMap ----- @@ -166,11 +143,7 @@ public class DefaultCacheRecycler implements Recycler { @Override @SuppressWarnings("unchecked") public ExtTDoubleObjectHashMap popDoubleObjectMap() { - Queue ref = doubleObjectHashMap.get(); - if (ref == null) { - return new ExtTDoubleObjectHashMap(); - } - ExtTDoubleObjectHashMap map = ref.poll(); + ExtTDoubleObjectHashMap map = pop(doubleObjectHashMap); if (map == null) { return new ExtTDoubleObjectHashMap(); } @@ -182,15 +155,8 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public void pushDoubleObjectMap(ExtTDoubleObjectHashMap map) { - Queue ref = doubleObjectHashMap.get(); - if (ref == null) { - ref = ConcurrentCollections.newQueue(); - doubleObjectHashMap.set(ref); - } - if (ref.size() < QUEUE_MAX_SIZE) { - map.clear(); - ref.add(map); - } + map.clear(); + push(doubleObjectHashMap, map); } // ----- ExtTLongObjectHashMap ---- @@ -203,31 +169,20 @@ public class DefaultCacheRecycler implements Recycler { @Override @SuppressWarnings("unchecked") public ExtTLongObjectHashMap popLongObjectMap() { - Queue ref = longObjectHashMap.get(); - if (ref == null) { - return new ExtTLongObjectHashMap(); - } - ExtTLongObjectHashMap map = ref.poll(); - if (map == null) { + ExtTLongObjectHashMap map = pop(longObjectHashMap); + if (map == null ) { return new ExtTLongObjectHashMap(); } return map; } - + /* (non-Javadoc) * @see org.elasticsearch.common.Recycler#pushLongObjectMap(org.elasticsearch.common.trove.ExtTLongObjectHashMap) */ @Override public void pushLongObjectMap(ExtTLongObjectHashMap map) { - Queue ref = longObjectHashMap.get(); - if (ref == null) { - ref = ConcurrentCollections.newQueue(); - longObjectHashMap.set(ref); - } - if (ref.size() < QUEUE_MAX_SIZE) { - map.clear(); - ref.add(map); - } + map.clear(); + push(longObjectHashMap, map); } // ----- TLongLongHashMap ---- @@ -239,11 +194,7 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public TLongLongHashMap popLongLongMap() { - Queue ref = longLongHashMap.get(); - if (ref == null) { - return new TLongLongHashMap(); - } - TLongLongHashMap map = ref.poll(); + TLongLongHashMap map = pop(longLongHashMap); if (map == null) { return new TLongLongHashMap(); } @@ -255,16 +206,8 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public void pushLongLongMap(TLongLongHashMap map) { - Queue ref = longLongHashMap.get(); - if (ref == null) { - ref = ConcurrentCollections.newQueue(); - longLongHashMap.set(ref); - } - if (ref.size() < QUEUE_MAX_SIZE) { - map.clear(); - ref.add(map); - } - } + map.clear(); + push(longLongHashMap, map); } // ----- TIntIntHashMap ---- @@ -276,11 +219,7 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public TIntIntHashMap popIntIntMap() { - Queue ref = intIntHashMap.get(); - if (ref == null) { - return new TIntIntHashMap(); - } - TIntIntHashMap map = ref.poll(); + TIntIntHashMap map = pop(intIntHashMap); if (map == null) { return new TIntIntHashMap(); } @@ -292,15 +231,8 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public void pushIntIntMap(TIntIntHashMap map) { - Queue ref = intIntHashMap.get(); - if (ref == null) { - ref = ConcurrentCollections.newQueue(); - intIntHashMap.set(ref); - } - if (ref.size() < QUEUE_MAX_SIZE) { - map.clear(); - ref.add(map); - } + map.clear(); + push(intIntHashMap, map); } @@ -314,11 +246,7 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public TFloatIntHashMap popFloatIntMap() { - Queue ref = floatIntHashMap.get(); - if (ref == null) { - return new TFloatIntHashMap(); - } - TFloatIntHashMap map = ref.poll(); + TFloatIntHashMap map = pop(floatIntHashMap); if (map == null) { return new TFloatIntHashMap(); } @@ -330,15 +258,8 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public void pushFloatIntMap(TFloatIntHashMap map) { - Queue ref = floatIntHashMap.get(); - if (ref == null) { - ref = ConcurrentCollections.newQueue(); - floatIntHashMap.set(ref); - } - if (ref.size() < QUEUE_MAX_SIZE) { - map.clear(); - ref.add(map); - } + map.clear(); + push(floatIntHashMap, map); } @@ -352,11 +273,7 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public TDoubleIntHashMap popDoubleIntMap() { - Queue ref = doubleIntHashMap.get(); - if (ref == null) { - return new TDoubleIntHashMap(); - } - TDoubleIntHashMap map = ref.poll(); + TDoubleIntHashMap map = pop(doubleIntHashMap); if (map == null) { return new TDoubleIntHashMap(); } @@ -368,15 +285,8 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public void pushDoubleIntMap(TDoubleIntHashMap map) { - Queue ref = doubleIntHashMap.get(); - if (ref == null) { - ref = ConcurrentCollections.newQueue(); - doubleIntHashMap.set(ref); - } - if (ref.size() < QUEUE_MAX_SIZE) { - map.clear(); - ref.add(map); - } + map.clear(); + push(doubleIntHashMap, map); } @@ -390,11 +300,7 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public TByteIntHashMap popByteIntMap() { - Queue ref = byteIntHashMap.get(); - if (ref == null) { - return new TByteIntHashMap(); - } - TByteIntHashMap map = ref.poll(); + TByteIntHashMap map = pop(byteIntHashMap); if (map == null) { return new TByteIntHashMap(); } @@ -406,17 +312,11 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public void pushByteIntMap(TByteIntHashMap map) { - Queue ref = byteIntHashMap.get(); - if (ref == null) { - ref = ConcurrentCollections.newQueue(); - byteIntHashMap.set(ref); - } - if (ref.size() < QUEUE_MAX_SIZE) { - map.clear(); - ref.add(map); - } + map.clear(); + push(byteIntHashMap, map); } - + + // ----- TShortIntHashMap --- private final SoftWrapper> shortIntHashMap = new SoftWrapper>(); @@ -427,11 +327,7 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public TShortIntHashMap popShortIntMap() { - Queue ref = shortIntHashMap.get(); - if (ref == null) { - return new TShortIntHashMap(); - } - TShortIntHashMap map = ref.poll(); + TShortIntHashMap map = pop(shortIntHashMap); if (map == null) { return new TShortIntHashMap(); } @@ -443,15 +339,8 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public void pushShortIntMap(TShortIntHashMap map) { - Queue ref = shortIntHashMap.get(); - if (ref == null) { - ref = ConcurrentCollections.newQueue(); - shortIntHashMap.set(ref); - } - if (ref.size() < QUEUE_MAX_SIZE) { - map.clear(); - ref.add(map); - } + map.clear(); + push(shortIntHashMap, map); } @@ -469,7 +358,7 @@ public class DefaultCacheRecycler implements Recycler { if (ref == null) { return new TLongIntHashMap(); } - TLongIntHashMap map = ref.poll(); + TLongIntHashMap map = pop(longIntHashMap); if (map == null) { return new TLongIntHashMap(); } @@ -481,15 +370,8 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public void pushLongIntMap(TLongIntHashMap map) { - Queue ref = longIntHashMap.get(); - if (ref == null) { - ref = ConcurrentCollections.newQueue(); - longIntHashMap.set(ref); - } - if (ref.size() < QUEUE_MAX_SIZE) { - map.clear(); - ref.add(map); - } + map.clear(); + push(longIntHashMap, map); } // ------ TObjectIntHashMap ----- @@ -503,11 +385,7 @@ public class DefaultCacheRecycler implements Recycler { @Override @SuppressWarnings({"unchecked"}) public TObjectIntHashMap popObjectIntMap() { - Queue ref = objectIntHashMap.get(); - if (ref == null) { - return new TObjectIntHashMap(); - } - TObjectIntHashMap map = ref.poll(); + TObjectIntHashMap map = pop(objectIntHashMap); if (map == null) { return new TObjectIntHashMap(); } @@ -519,15 +397,8 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public void pushObjectIntMap(TObjectIntHashMap map) { - Queue ref = objectIntHashMap.get(); - if (ref == null) { - ref = ConcurrentCollections.newQueue(); - objectIntHashMap.set(ref); - } - if (ref.size() < QUEUE_MAX_SIZE) { - map.clear(); - ref.add(map); - } + map.clear(); + push(objectIntHashMap, map); } // ------ TIntObjectHashMap ----- @@ -541,11 +412,7 @@ public class DefaultCacheRecycler implements Recycler { @Override @SuppressWarnings({"unchecked"}) public TIntObjectHashMap popIntObjectMap() { - Queue ref = intObjectHashMap.get(); - if (ref == null) { - return new TIntObjectHashMap(); - } - TIntObjectHashMap map = ref.poll(); + TIntObjectHashMap map = pop(intObjectHashMap); if (map == null) { return new TIntObjectHashMap(); } @@ -557,15 +424,8 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public void pushIntObjectMap(TIntObjectHashMap map) { - Queue ref = intObjectHashMap.get(); - if (ref == null) { - ref = ConcurrentCollections.newQueue(); - intObjectHashMap.set(ref); - } - if (ref.size() < QUEUE_MAX_SIZE) { - map.clear(); - ref.add(map); - } + map.clear(); + push(intObjectHashMap, map); } // ------ TObjectFloatHashMap ----- @@ -578,11 +438,7 @@ public class DefaultCacheRecycler implements Recycler { @Override @SuppressWarnings({"unchecked"}) public TObjectFloatHashMap popObjectFloatMap() { - Queue ref = objectFloatHashMap.get(); - if (ref == null) { - return new TObjectFloatHashMap(); - } - TObjectFloatHashMap map = ref.poll(); + final TObjectFloatHashMap map = pop(objectFloatHashMap); if (map == null) { return new TObjectFloatHashMap(); } @@ -594,15 +450,8 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public void pushObjectFloatMap(TObjectFloatHashMap map) { - Queue ref = objectFloatHashMap.get(); - if (ref == null) { - ref = ConcurrentCollections.newQueue(); - objectFloatHashMap.set(ref); - } - if (ref.size() < QUEUE_MAX_SIZE) { - map.clear(); - ref.add(map); - } + map.clear(); + push(objectFloatHashMap, map); } // ----- int[] ----- @@ -634,15 +483,8 @@ public class DefaultCacheRecycler implements Recycler { */ @Override public void pushObjectArray(Object[] objects) { - Queue ref = objectArray.get(); - if (ref == null) { - ref = ConcurrentCollections.newQueue(); - objectArray.set(ref); - } - if (ref.size() < QUEUE_MAX_SIZE) { - Arrays.fill(objects, null); - ref.add(objects); - } + Arrays.fill(objects, null); + push(objectArray, objects); } @@ -687,28 +529,30 @@ public class DefaultCacheRecycler implements Recycler { } return ints; } - - /* (non-Javadoc) - * @see org.elasticsearch.common.Recycler#pushIntArray(int[]) - */ + @Override public void pushIntArray(int[] ints) { pushIntArray(ints, 0); } - /* (non-Javadoc) - * @see org.elasticsearch.common.Recycler#pushIntArray(int[], int) - */ @Override public void pushIntArray(int[] ints, int sentinal) { - Queue ref = intArray.get(); + Arrays.fill(ints, sentinal); + push(intArray, ints); + } + + private static final void push(SoftWrapper> wrapper, T obj) { + Queue ref = wrapper.get(); if (ref == null) { ref = ConcurrentCollections.newQueue(); - intArray.set(ref); - } - if (ref.size() < QUEUE_MAX_SIZE) { - Arrays.fill(ints, sentinal); - ref.add(ints); + wrapper.set(ref); } + ref.add(obj); } + + private static final T pop(SoftWrapper> wrapper) { + Queue queue = wrapper.get(); + return queue == null ? null : queue.poll(); + } + } \ No newline at end of file