From b298356e49da8e5a1f85cd55bdfbe8fd2f41f525 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Mon, 25 Apr 2022 09:18:28 -0700 Subject: [PATCH 1/3] Remove hppc from terms agg The terms aggs take include/excludes for terms, and for numeric fields use an hppc set of longs. This commit converts to using a HashSet. relates #84735 --- .../aggregations/bucket/terms/IncludeExclude.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/IncludeExclude.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/IncludeExclude.java index ed37c325b5c1c..2621e49f5022a 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/IncludeExclude.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/IncludeExclude.java @@ -7,9 +7,6 @@ */ package org.elasticsearch.search.aggregations.bucket.terms; -import com.carrotsearch.hppc.LongHashSet; -import com.carrotsearch.hppc.LongSet; - import org.apache.lucene.index.SortedSetDocValues; import org.apache.lucene.index.Terms; import org.apache.lucene.index.TermsEnum; @@ -145,15 +142,15 @@ public boolean accept(long value) { } public static class SetBackedLongFilter extends LongFilter { - private LongSet valids; - private LongSet invalids; + private Set valids; + private Set invalids; private SetBackedLongFilter(int numValids, int numInvalids) { if (numValids > 0) { - valids = new LongHashSet(numValids); + valids = new HashSet<>(numValids); } if (numInvalids > 0) { - invalids = new LongHashSet(numInvalids); + invalids = new HashSet<>(numInvalids); } } From 3eb4c662afeb3a22f3b59ad72c02d16598da2b1d Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Tue, 26 Apr 2022 14:12:16 -0700 Subject: [PATCH 2/3] alternative without allocation --- .../bucket/terms/IncludeExclude.java | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/IncludeExclude.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/IncludeExclude.java index 2621e49f5022a..a6a39176dedf5 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/IncludeExclude.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/IncludeExclude.java @@ -142,9 +142,34 @@ public boolean accept(long value) { } public static class SetBackedLongFilter extends LongFilter { + // Autoboxing long could cause allocations when doing Set.contains, so + // this alternative to java.lang.Long is not final so that a preallocated instance + // can be used in accept (note that none of this is threadsafe!) + private static class Long { + private long value; + + private Long(long value) { + this.value = value; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Long that = (Long) o; + return value == that.value; + } + + @Override + public int hashCode() { + return java.lang.Long.hashCode(value); + } + } private Set valids; private Set invalids; + private Long spare = new Long(0); + private SetBackedLongFilter(int numValids, int numInvalids) { if (numValids > 0) { valids = new HashSet<>(numValids); @@ -156,15 +181,16 @@ private SetBackedLongFilter(int numValids, int numInvalids) { @Override public boolean accept(long value) { - return (valids == null || valids.contains(value)) && (invalids == null || invalids.contains(value) == false); + spare.value = value; + return (valids == null || valids.contains(spare)) && (invalids == null || invalids.contains(spare) == false); } private void addAccept(long val) { - valids.add(val); + valids.add(new Long(val)); } private void addReject(long val) { - invalids.add(val); + invalids.add(new Long(val)); } } From 7aa44bb197d5d1a1fd5deb535686609ec1e657d7 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Tue, 26 Apr 2022 14:27:51 -0700 Subject: [PATCH 3/3] spotless --- .../search/aggregations/bucket/terms/IncludeExclude.java | 1 + 1 file changed, 1 insertion(+) diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/IncludeExclude.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/IncludeExclude.java index a6a39176dedf5..ce27b1a24a0db 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/IncludeExclude.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/IncludeExclude.java @@ -165,6 +165,7 @@ public int hashCode() { return java.lang.Long.hashCode(value); } } + private Set valids; private Set invalids;