Skip to content

Make shardIndexMap in AbstractSearchAsyncAction a local variable #117168

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

Conversation

original-brownbear
Copy link
Member

We only need this rather large map in run, lets create it on the fly there to save the rather large redundant field and save on state and complexity in general. Also, this saves one round of copying the shard list which is always nice for large numbers of shards.

We only need this rather large map in `run`, lets create it on the fly
there to save the rather large redundant field and save on state and
complexity in general.
@elasticsearchmachine
Copy link
Collaborator

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

@elasticsearchmachine elasticsearchmachine added Team:Search Foundations Meta label for the Search Foundations team in Elasticsearch v9.0.0 labels Nov 20, 2024
final Map<SearchShardIterator, Integer> shardIndexMap = Maps.newHashMapWithExpectedSize(shardIterators.length);
for (int i = 0; i < shardIterators.length; i++) {
shardIndexMap.put(shardIterators[i], i);
}
Copy link
Member

Choose a reason for hiding this comment

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

This is fine because run is always every called once on a specific async action instance?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea, the results array among other things is pretty much single-use :)

}
this.shardIndexMap = Collections.unmodifiableMap(shardMap);
this.shardIterators = searchIterators.toArray(SearchShardIterator[]::new);
Arrays.sort(shardIterators);
Copy link
Member

Choose a reason for hiding this comment

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

can we remove the map while leaving the rest as it was?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure I was just annoyed by the needless extra copy and figured it's not worth an individual PR? :)

Copy link
Member

Choose a reason for hiding this comment

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

is timSort vs Arrays.sort on purpose?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think we have a timSort for arrays in Lucene? + the Lucene timSort seems to be somewhat obsolete now anyway potentially with List#sort being a thing in recent Java versions and likely more efficient?
-> results are unchanged and this seems to be the canonical solution now :)?

Copy link
Contributor

@jimczi jimczi left a comment

Choose a reason for hiding this comment

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

LGTM

@original-brownbear original-brownbear merged commit f4bbe75 into elastic:main Jan 21, 2025
16 checks passed
@original-brownbear original-brownbear deleted the save-needless-big-shardIndexMap branch January 21, 2025 09:11
original-brownbear added a commit to original-brownbear/elasticsearch that referenced this pull request Jan 31, 2025
…stic#117168)

We only need this rather large map in `run`, lets create it on the fly
there to save the rather large redundant field and save on state and
complexity in general.
original-brownbear added a commit that referenced this pull request Jan 31, 2025
…7168) (#121408)

We only need this rather large map in `run`, lets create it on the fly
there to save the rather large redundant field and save on state and
complexity in general.
prdoyle pushed a commit to prdoyle/elasticsearch that referenced this pull request Jan 31, 2025
…stic#117168) (elastic#121408)

We only need this rather large map in `run`, lets create it on the fly
there to save the rather large redundant field and save on state and
complexity in general.
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.18.0 v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants