Apache Lucene open-source search software
LUCENE-10379: Count directly into the dense values array in FastTaxonomyFacetCounts#countAll (#605)
Greg Miller2f5e3c323b4d
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 0e768b1fa24..b82a4fc6d8e 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -156,6 +156,9 @@ Optimizations* LUCENE-10356: Further optimize facet counting for single-valued TaxonomyFacetCounts. (Greg Miller)+* LUCENE-10379: Count directly into the dense values array in FastTaxonomyFacetCounts#countAll.+ (Guo Feng, Greg Miller)+Changes in runtime behavior---------------------diff --git a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/FastTaxonomyFacetCounts.java b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/FastTaxonomyFacetCounts.javaindex 1e7d8311352..2b81ee7d7f7 100644--- a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/FastTaxonomyFacetCounts.java+++ b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/FastTaxonomyFacetCounts.java@@ -84,13 +84,27 @@ public class FastTaxonomyFacetCounts extends IntTaxonomyFacets {ConjunctionUtils.intersectIterators(Arrays.asList(hits.bits.iterator(), valuesIt));if (singleValued != null) {- while (it.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {- increment((int) singleValued.longValue());+ if (values != null) {+ while (it.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {+ values[(int) singleValued.longValue()]++;+ }+ } else {+ while (it.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {+ sparseValues.addTo((int) singleValued.longValue(), 1);+ }}} else {- while (it.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {- for (int i = 0; i < multiValued.docValueCount(); i++) {- increment((int) multiValued.nextValue());+ if (values != null) {+ while (it.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {+ for (int i = 0; i < multiValued.docValueCount(); i++) {+ values[(int) multiValued.nextValue()]++;+ }+ }+ } else {+ while (it.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {+ for (int i = 0; i < multiValued.docValueCount(); i++) {+ sparseValues.addTo((int) multiValued.nextValue(), 1);+ }}}}@@ -100,6 +114,7 @@ public class FastTaxonomyFacetCounts extends IntTaxonomyFacets {}private void countAll(IndexReader reader) throws IOException {+ assert values != null;for (LeafReaderContext context : reader.leaves()) {SortedNumericDocValues multiValued =context.reader().getSortedNumericDocValues(indexFieldName);@@ -117,19 +132,18 @@ public class FastTaxonomyFacetCounts extends IntTaxonomyFacets {if (liveDocs != null && liveDocs.get(doc) == false) {continue;}- increment((int) singleValued.longValue());- }- continue;- }-- for (int doc = multiValued.nextDoc();- doc != DocIdSetIterator.NO_MORE_DOCS;- doc = multiValued.nextDoc()) {- if (liveDocs != null && liveDocs.get(doc) == false) {- continue;+ values[(int) singleValued.longValue()]++;}- for (int i = 0; i < multiValued.docValueCount(); i++) {- increment((int) multiValued.nextValue());+ } else {+ for (int doc = multiValued.nextDoc();+ doc != DocIdSetIterator.NO_MORE_DOCS;+ doc = multiValued.nextDoc()) {+ if (liveDocs != null && liveDocs.get(doc) == false) {+ continue;+ }+ for (int i = 0; i < multiValued.docValueCount(); i++) {+ values[(int) multiValued.nextValue()]++;+ }}}}diff --git a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/IntTaxonomyFacets.java b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/IntTaxonomyFacets.javaindex 3f1dc17dece..cdec3f1153f 100644--- a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/IntTaxonomyFacets.java+++ b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/IntTaxonomyFacets.java@@ -31,10 +31,21 @@ import org.apache.lucene.facet.TopOrdAndIntQueue;/** Base class for all taxonomy-based facets that aggregate to a per-ords int[]. */public abstract class IntTaxonomyFacets extends TaxonomyFacets {- /** Per-ordinal value. */- private final int[] values;+ /**+ * Dense ordinal values.+ *+ * <p>We are making this and {@link #sparseValues} protected for some expert usage. e.g. It can be+ * checked which is being used before a loop instead of calling {@link #increment} for each+ * iteration.+ */+ protected final int[] values;- private final IntIntHashMap sparseValues;+ /**+ * Sparse ordinal values.+ *+ * @see #values for why protected.+ */+ protected final IntIntHashMap sparseValues;/** Sole constructor. */protected IntTaxonomyFacets(