diff --git a/modules/benchmark/micro/src/main/java/org/elasticsearch/benchmark/search/facet/HistogramFacetSearchBenchmark.java b/modules/benchmark/micro/src/main/java/org/elasticsearch/benchmark/search/facet/HistogramFacetSearchBenchmark.java index 86f768accfd..aa175fe5ee1 100644 --- a/modules/benchmark/micro/src/main/java/org/elasticsearch/benchmark/search/facet/HistogramFacetSearchBenchmark.java +++ b/modules/benchmark/micro/src/main/java/org/elasticsearch/benchmark/search/facet/HistogramFacetSearchBenchmark.java @@ -61,7 +61,7 @@ public class HistogramFacetSearchBenchmark { Client client = clientNode.client(); - long COUNT = SizeValue.parseSizeValue("1m").singles(); + long COUNT = SizeValue.parseSizeValue("5m").singles(); int BATCH = 500; int QUERY_WARMUP = 20; int QUERY_COUNT = 200; @@ -112,7 +112,8 @@ public class HistogramFacetSearchBenchmark { } } client.admin().indices().prepareRefresh().execute().actionGet(); - System.out.println("--> Number of docs in index: " + client.prepareCount().setQuery(matchAllQuery()).execute().actionGet().count()); + COUNT = client.prepareCount().setQuery(matchAllQuery()).execute().actionGet().count(); + System.out.println("--> Number of docs in index: " + COUNT); System.out.println("--> Warmup..."); // run just the child query, warm up first diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/CacheRecycler.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/CacheRecycler.java index f1cc58bd868..f9fb331fb5a 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/CacheRecycler.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/CacheRecycler.java @@ -38,6 +38,36 @@ public class CacheRecycler { intArray.remove(); } + // ----- TLongLongHashMap ---- + + private static ThreadLocal>> longLongHashMap = new ThreadLocal>>(); + + + public static TLongLongHashMap popLongLongMap() { + SoftReference> ref = longLongHashMap.get(); + Deque deque = ref == null ? null : ref.get(); + if (deque == null) { + deque = new ArrayDeque(); + longLongHashMap.set(new SoftReference>(deque)); + } + if (deque.isEmpty()) { + return new TLongLongHashMap(); + } + TLongLongHashMap map = deque.pollFirst(); + map.clear(); + return map; + } + + public static void pushLongLongMap(TLongLongHashMap map) { + SoftReference> ref = longLongHashMap.get(); + Deque deque = ref == null ? null : ref.get(); + if (deque == null) { + deque = new ArrayDeque(); + longLongHashMap.set(new SoftReference>(deque)); + } + deque.add(map); + } + // ----- TIntIntHashMap ---- private static ThreadLocal>> intIntHashMap = new ThreadLocal>>(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/datehistogram/CountDateHistogramFacetCollector.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/datehistogram/CountDateHistogramFacetCollector.java index bf84943b457..c816ce2decd 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/datehistogram/CountDateHistogramFacetCollector.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/datehistogram/CountDateHistogramFacetCollector.java @@ -20,6 +20,7 @@ package org.elasticsearch.search.facet.datehistogram; import org.apache.lucene.index.IndexReader; +import org.elasticsearch.common.CacheRecycler; import org.elasticsearch.common.joda.time.MutableDateTime; import org.elasticsearch.common.trove.map.hash.TLongLongHashMap; import org.elasticsearch.index.cache.field.data.FieldDataCache; @@ -93,6 +94,7 @@ public class CountDateHistogramFacetCollector extends AbstractFacetCollector { } @Override public Facet facet() { + CacheRecycler.pushLongLongMap(histoProc.counts()); return new InternalCountDateHistogramFacet(facetName, comparatorType, histoProc.counts()); } @@ -102,7 +104,7 @@ public class CountDateHistogramFacetCollector extends AbstractFacetCollector { public static class DateHistogramProc implements LongFieldData.DateValueInDocProc { - protected final TLongLongHashMap counts = new TLongLongHashMap(); + protected final TLongLongHashMap counts = CacheRecycler.popLongLongMap(); @Override public void onValue(int docId, MutableDateTime dateTime) { counts.adjustOrPutValue(dateTime.getMillis(), 1, 1); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/histogram/CountHistogramFacetCollector.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/histogram/CountHistogramFacetCollector.java index 0749c4e655c..37d8411ddf8 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/histogram/CountHistogramFacetCollector.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facet/histogram/CountHistogramFacetCollector.java @@ -20,6 +20,7 @@ package org.elasticsearch.search.facet.histogram; import org.apache.lucene.index.IndexReader; +import org.elasticsearch.common.CacheRecycler; import org.elasticsearch.common.trove.map.hash.TLongLongHashMap; import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.field.data.FieldDataType; @@ -85,6 +86,7 @@ public class CountHistogramFacetCollector extends AbstractFacetCollector { } @Override public Facet facet() { + CacheRecycler.pushLongLongMap(histoProc.counts()); return new InternalCountHistogramFacet(facetName, comparatorType, histoProc.counts()); } @@ -96,7 +98,7 @@ public class CountHistogramFacetCollector extends AbstractFacetCollector { private final long interval; - private final TLongLongHashMap counts = new TLongLongHashMap(); + private final TLongLongHashMap counts = CacheRecycler.popLongLongMap(); private int missing;