Skip to content
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

Remove some redundant ref-counting from SearchHits #124948

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

original-brownbear
Copy link
Member

Remove ref-counting that is obviously redundant because of clear
ownership transfers from SearchHits.

This makes the compiler's life a little easier (i.e. saves cache and instructions) now and makes it easier to release SearchHits earlier than it is released today during response serialization.

Remove ref-counting that is obviously redundant because of clear
ownership transfers from `SearchHits`.
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search-foundations (Team:Search Foundations)

@elasticsearchmachine elasticsearchmachine added the Team:Search Foundations Meta label for the Search Foundations team in Elasticsearch label Mar 15, 2025
SearchResponse theResponse = mockSearchPhaseContext.searchResponse.get();
assertNotNull(theResponse);
assertEquals(numInnerHits, theResponse.getHits().getHits()[0].getInnerHits().size());
ExpandSearchPhase phase = newExpandSearchPhase(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This stuff is what blows up the LoC changed in this PR but I felt it best to inline these values still since their lifecycle is now bound to the SearchResponseSections.

@@ -252,6 +252,7 @@ public boolean decRef() {
}

private void deallocate() {
var hits = this.hits;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just saving the memory reference, turns out this was what made this method show up in profiling.

ProfileResult profileResult = profiler.finish();
// Only set the shardResults if building search hits was successful
if (hits != null) {
context.fetchResult().shardResult(hits, profileResult);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For ease of reviewing: this is the only production callsite where we pass a pooled instance into shardResult so changing shardResult(..) and this logic is relatively obviously safe.

}
}

private static void mergeSuggest(
ReducedQueryPhase reducedQueryPhase,
AtomicArray<? extends SearchPhaseResult> fetchResultsArray,
SearchHits hits,
int currentOffset,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to escape the hits from the caller just to get their length, adjusting this to make things more obviously safe in the only callsite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>non-issue :Search Foundations/Search Catch all for Search Foundations Team:Search Foundations Meta label for the Search Foundations team in Elasticsearch v8.19.0 v9.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants