Skip to content

[Composite Terms Aggregation] Re-use slot objects for look-ups #18531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ private class Slot {
this.value = initial;
}

// This is to be only for reusable slot
public void set(int newValue) {
this.value = newValue;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -82,6 +87,14 @@ public int hashCode() {
private final Map<Slot, Integer> map; // to quickly find the slot for a value
private final SingleDimensionValuesSource<?>[] arrays;

/**
* A reusable, flyweight Slot instance to avoid object allocation and reduce GC pressure
* during map lookups in the high-frequency collect() path. This object is NOT
* thread-safe, but is safe here because each collector instance is confined to a
* single thread.
*/
private final Slot reusableSlot = new Slot(0);

private LongArray docCounts;
private boolean afterKeyIsSet = false;

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

/**
Expand Down Expand Up @@ -322,7 +336,8 @@ boolean addIfCompetitive(int indexSortSourcePrefix, long inc) {
if (size() >= maxSize) {
// the queue is full, we replace the last key with this candidate
int slot = pop();
map.remove(new Slot(slot));
reusableSlot.set(slot); // Use reusable for remove
map.remove(reusableSlot);
// and we recycle the deleted slot
newSlot = slot;
} else {
Expand Down
Loading