Skip to content

Commit 22cb945

Browse files
committed
reuse slot objects for lookups
Signed-off-by: Sandesh Kumar <sandeshkr419@gmail.com>
1 parent 16d0fb5 commit 22cb945

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1717
- Add BooleanQuery rewrite moving constant-scoring must clauses to filter clauses ([#18510](https://github.com/opensearch-project/OpenSearch/issues/18510))
1818
- Add functionality for plugins to inject QueryCollectorContext during QueryPhase ([#18637](https://github.com/opensearch-project/OpenSearch/pull/18637))
1919
- Add support for non-timing info in profiler ([#18460](https://github.com/opensearch-project/OpenSearch/issues/18460))
20+
- Optimize Composite Aggregations by removing unnecessary object allocations ([#18531](https://github.com/opensearch-project/OpenSearch/pull/18531))
2021

2122
### Changed
2223
- Update Subject interface to use CheckedRunnable ([#18570](https://github.com/opensearch-project/OpenSearch/issues/18570))

server/src/main/java/org/opensearch/search/aggregations/bucket/composite/CompositeValuesCollectorQueue.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ private class Slot {
6060
this.value = initial;
6161
}
6262

63+
// This is to be only for reusable slot
64+
public void set(int newValue) {
65+
this.value = newValue;
66+
}
67+
6368
@Override
6469
public boolean equals(Object o) {
6570
if (this == o) return true;
@@ -82,6 +87,14 @@ public int hashCode() {
8287
private final Map<Slot, Integer> map; // to quickly find the slot for a value
8388
private final SingleDimensionValuesSource<?>[] arrays;
8489

90+
/**
91+
* A reusable, flyweight Slot instance to avoid object allocation and reduce GC pressure
92+
* during map lookups in the high-frequency collect() path. This object is NOT
93+
* thread-safe, but is safe here because each collector instance is confined to a
94+
* single thread.
95+
*/
96+
private final Slot reusableSlot = new Slot(0);
97+
8598
private LongArray docCounts;
8699
private boolean afterKeyIsSet = false;
87100

@@ -125,7 +138,8 @@ boolean isFull() {
125138
* the slot if the candidate is already in the queue or null if the candidate is not present.
126139
*/
127140
Integer getCurrentSlot() {
128-
return map.get(new Slot(CANDIDATE_SLOT));
141+
reusableSlot.set(CANDIDATE_SLOT); // Update the state of the reusable slot
142+
return map.get(reusableSlot); // Use the single reusable slot instance for the lookup
129143
}
130144

131145
/**
@@ -322,7 +336,8 @@ boolean addIfCompetitive(int indexSortSourcePrefix, long inc) {
322336
if (size() >= maxSize) {
323337
// the queue is full, we replace the last key with this candidate
324338
int slot = pop();
325-
map.remove(new Slot(slot));
339+
reusableSlot.set(slot); // Use reusable for remove
340+
map.remove(reusableSlot);
326341
// and we recycle the deleted slot
327342
newSlot = slot;
328343
} else {

0 commit comments

Comments
 (0)