Skip to content

Commit c1d0832

Browse files
Remove some redundant ref-counting from SearchHits
Remove ref-counting that is obviously redundant because of clear ownership transfers from `SearchHits`.
1 parent 789eb2f commit c1d0832

File tree

5 files changed

+22
-15
lines changed

5 files changed

+22
-15
lines changed

server/src/main/java/org/elasticsearch/action/search/SearchPhaseController.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,26 +273,28 @@ public static SearchResponseSections merge(
273273
if (reducedQueryPhase.isEmptyResult) {
274274
return SearchResponseSections.EMPTY_WITH_TOTAL_HITS;
275275
}
276-
ScoreDoc[] sortedDocs = reducedQueryPhase.sortedTopDocs.scoreDocs;
277276
var fetchResults = fetchResultsArray.asList();
278-
final SearchHits hits = getHits(reducedQueryPhase, ignoreFrom, fetchResultsArray);
277+
SearchHits hits = getHits(reducedQueryPhase, ignoreFrom, fetchResultsArray);
279278
try {
280279
if (reducedQueryPhase.suggest != null && fetchResults.isEmpty() == false) {
281-
mergeSuggest(reducedQueryPhase, fetchResultsArray, hits, sortedDocs);
280+
mergeSuggest(reducedQueryPhase, fetchResultsArray, hits.getHits().length, reducedQueryPhase.sortedTopDocs.scoreDocs);
282281
}
283-
return reducedQueryPhase.buildResponse(hits, fetchResults);
282+
var res = reducedQueryPhase.buildResponse(hits, fetchResults);
283+
hits = null;
284+
return res;
284285
} finally {
285-
hits.decRef();
286+
if (hits != null) {
287+
hits.decRef();
288+
}
286289
}
287290
}
288291

289292
private static void mergeSuggest(
290293
ReducedQueryPhase reducedQueryPhase,
291294
AtomicArray<? extends SearchPhaseResult> fetchResultsArray,
292-
SearchHits hits,
295+
int currentOffset,
293296
ScoreDoc[] sortedDocs
294297
) {
295-
int currentOffset = hits.getHits().length;
296298
for (CompletionSuggestion suggestion : reducedQueryPhase.suggest.filter(CompletionSuggestion.class)) {
297299
final List<CompletionSuggestion.Entry.Option> suggestionOptions = suggestion.getOptions();
298300
for (int scoreDocIndex = currentOffset; scoreDocIndex < currentOffset + suggestionOptions.size(); scoreDocIndex++) {

server/src/main/java/org/elasticsearch/action/search/SearchResponseSections.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public SearchResponseSections(
6161
int numReducePhases
6262
) {
6363
this.hits = hits;
64-
hits.incRef();
6564
this.aggregations = aggregations;
6665
this.suggest = suggest;
6766
this.profileResults = profileResults;

server/src/main/java/org/elasticsearch/search/SearchHits.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ public boolean decRef() {
252252
}
253253

254254
private void deallocate() {
255+
var hits = this.hits;
255256
for (int i = 0; i < hits.length; i++) {
256257
assert hits[i] != null;
257258
hits[i].decRef();

server/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,18 @@ public void execute(SearchContext context, int[] docIdsToLoad, RankDocShardInfo
8484
try {
8585
hits = buildSearchHits(context, docIdsToLoad, profiler, rankDocs);
8686
} finally {
87-
// Always finish profiling
88-
ProfileResult profileResult = profiler.finish();
89-
// Only set the shardResults if building search hits was successful
90-
if (hits != null) {
91-
context.fetchResult().shardResult(hits, profileResult);
92-
hits.decRef();
87+
try {
88+
// Always finish profiling
89+
ProfileResult profileResult = profiler.finish();
90+
// Only set the shardResults if building search hits was successful
91+
if (hits != null) {
92+
context.fetchResult().shardResult(hits, profileResult);
93+
hits = null;
94+
}
95+
} finally {
96+
if (hits != null) {
97+
hits.decRef();
98+
}
9399
}
94100
}
95101
}

server/src/main/java/org/elasticsearch/search/fetch/FetchSearchResult.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public void shardResult(SearchHits hits, ProfileResult profileResult) {
6868
existing.decRef();
6969
}
7070
this.hits = hits;
71-
hits.mustIncRef();
7271
assert this.profileResult == null;
7372
this.profileResult = profileResult;
7473
}

0 commit comments

Comments
 (0)