remove size bound from cache recycler for performance reasons

This commit is contained in:
Simon Willnauer 2013-04-19 12:36:12 +02:00
parent 2d13aa29f8
commit a1c62759c9
1 changed files with 63 additions and 219 deletions

View File

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