From e864d5785ef0ea64a50ac73206df8bab140dc765 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Sat, 30 Mar 2013 15:50:36 +0100 Subject: [PATCH] optimize matcher reset to not create unnecessary string objects --- .../TermsStringOrdinalsFacetExecutor.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/elasticsearch/search/facet/terms/strings/TermsStringOrdinalsFacetExecutor.java b/src/main/java/org/elasticsearch/search/facet/terms/strings/TermsStringOrdinalsFacetExecutor.java index 54882e8962f..00ee0f2fe8a 100644 --- a/src/main/java/org/elasticsearch/search/facet/terms/strings/TermsStringOrdinalsFacetExecutor.java +++ b/src/main/java/org/elasticsearch/search/facet/terms/strings/TermsStringOrdinalsFacetExecutor.java @@ -22,7 +22,9 @@ package org.elasticsearch.search.facet.terms.strings; import com.google.common.collect.ImmutableSet; import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.CharsRef; import org.apache.lucene.util.PriorityQueue; +import org.apache.lucene.util.UnicodeUtil; import org.elasticsearch.common.CacheRecycler; import org.elasticsearch.common.collect.BoundedTreeSet; import org.elasticsearch.index.fielddata.BytesValues; @@ -50,7 +52,6 @@ public class TermsStringOrdinalsFacetExecutor extends FacetExecutor { private final TermsFacet.ComparatorType comparatorType; private final int size; - private final int numberOfShards; private final int minCount; private final ImmutableSet excluded; private final Matcher matcher; @@ -65,7 +66,6 @@ public class TermsStringOrdinalsFacetExecutor extends FacetExecutor { this.indexFieldData = indexFieldData; this.size = size; this.comparatorType = comparatorType; - this.numberOfShards = context.numberOfShards(); this.ordinalsCacheAbove = ordinalsCacheAbove; if (excluded == null || excluded.isEmpty()) { @@ -92,8 +92,8 @@ public class TermsStringOrdinalsFacetExecutor extends FacetExecutor { @Override public InternalFacet buildFacet(String facetName) { + final CharsRef spare = new CharsRef(); AggregatorPriorityQueue queue = new AggregatorPriorityQueue(aggregators.size()); - for (ReaderAggregator aggregator : aggregators) { if (aggregator.nextPosition()) { queue.add(aggregator); @@ -124,9 +124,12 @@ public class TermsStringOrdinalsFacetExecutor extends FacetExecutor { if (excluded != null && excluded.contains(value)) { continue; } - // LUCENE 4 UPGRADE: use Lucene's RegexCapabilities - if (matcher != null && !matcher.reset(value.utf8ToString()).matches()) { - continue; + if (matcher != null) { + UnicodeUtil.UTF8toUTF16(value, spare); + assert spare.toString().equals(value.utf8ToString()); + if (!matcher.reset(spare).matches()) { + continue; + } } InternalStringTermsFacet.TermEntry entry = new InternalStringTermsFacet.TermEntry(value, count); ordered.insertWithOverflow(entry); @@ -167,9 +170,12 @@ public class TermsStringOrdinalsFacetExecutor extends FacetExecutor { if (excluded != null && excluded.contains(value)) { continue; } - // LUCENE 4 UPGRADE: use Lucene's RegexCapabilities - if (matcher != null && !matcher.reset(value.utf8ToString()).matches()) { - continue; + if (matcher != null) { + UnicodeUtil.UTF8toUTF16(value, spare); + assert spare.toString().equals(value.utf8ToString()); + if (!matcher.reset(spare).matches()) { + continue; + } } InternalStringTermsFacet.TermEntry entry = new InternalStringTermsFacet.TermEntry(value, count); ordered.add(entry);