Skip to content

Commit 69212c0

Browse files
Copilotanidotnet
andcommitted
Optimize EqualsFilter to use fast path for non-numeric values
Use direct index lookup for non-numeric values (O(log n)) while scanning for numeric values to support cross-type comparisons. This addresses the performance concern while maintaining correctness for numeric filtering. Co-authored-by: anidotnet <[email protected]>
1 parent 3a586ae commit 69212c0

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

nitrite/src/main/java/org/dizitart/no2/filters/EqualsFilter.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,36 @@ public boolean apply(Pair<NitriteId, Document> element) {
4545
public List<?> applyOnIndex(IndexMap indexMap) {
4646
List<NitriteId> nitriteIds = new ArrayList<>();
4747

48-
// Iterate through all index entries and use deepEquals for comparison
49-
// This allows numeric types to be compared properly (e.g., int vs long)
50-
for (Pair<Comparable<?>, ?> entry : indexMap.entries()) {
51-
if (deepEquals(getValue(), entry.getFirst())) {
52-
Object value = entry.getSecond();
53-
if (value instanceof List) {
54-
@SuppressWarnings("unchecked")
55-
List<NitriteId> result = (List<NitriteId>) value;
56-
nitriteIds.addAll(result);
48+
// If value is a Number, we need to check for numeric equivalents across types
49+
// Otherwise, use fast direct lookup
50+
if (getValue() instanceof Number) {
51+
// Scan for all numerically equivalent values
52+
for (Pair<Comparable<?>, ?> entry : indexMap.entries()) {
53+
if (deepEquals(getValue(), entry.getFirst())) {
54+
Object entryValue = entry.getSecond();
55+
if (entryValue instanceof List) {
56+
@SuppressWarnings("unchecked")
57+
List<NitriteId> result = (List<NitriteId>) entryValue;
58+
nitriteIds.addAll(result);
59+
}
5760
}
5861
}
62+
return nitriteIds;
63+
}
64+
65+
// Fast path for non-numeric values: direct lookup
66+
Object value = indexMap.get((Comparable<?>) getValue());
67+
if (value == null) {
68+
return new ArrayList<>();
69+
}
70+
71+
if (value instanceof List) {
72+
return ((List<?>) value);
5973
}
6074

61-
return nitriteIds;
75+
List<Object> result = new ArrayList<>();
76+
result.add(value);
77+
return result;
6278
}
6379

6480
@Override

0 commit comments

Comments
 (0)