-
Notifications
You must be signed in to change notification settings - Fork 25.5k
Optimize index lookup for single default project case #123675
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
Optimize index lookup for single default project case #123675
Conversation
We can avoid using the volatile field when there is only a single default project. Relates: elastic#123662
Pinging @elastic/es-core-infra (Team:Core/Infra) |
if (isSingleProject()) { | ||
final var project = projectMetadata.get(DEFAULT_PROJECT_ID); | ||
return project.hasIndex(index) ? Optional.of(project) : Optional.empty(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically the optimization can be applied to any single project, not only just the default. But I didn't do it for two reasons:
- Existing code only handles single-default project
- No great way to get the single project if it is not the default. We can use
projectMetadata.values().iterator().next()
. But that feels quite a bit overhead since we are talking about a few cpu cycles here.
Are you sure this is cheaper? |
If we do multiple lookups, shouldn't the existing code do multiple volatile read? I cannot be certain about the performance without benchmarking. That said, I think there is a good chance for this change to be cheaper for single-default project setup since both size and containsKey checks should be fairly cheap for a single-element map. For multi-project setup, it performs an extra size check compared to the existing code which is, strictly speadking, more expensive. In any case, there is no need to merge this before benchmarking is avilable for changes in #123662. |
Let me change this to draft so that it does not get merged accidentally. |
Also, the existing code can write to the volatile field multiple times. It is an edge case but not impossible when the concurrency is high. |
What if we compute the value of We could take it even further by, instead of storing it as a boolean, storing the default/single field as |
I actually started with this but was not sure whether it would deem to be too hacky. Personally I don't mind it. It would make the single project scenario more generic to cover not only a single "default" project but also any single project, which could be useful in future when a single big project needs to take a cluster for its own. |
I wouldn't consider that to be hacky. I see it as just pre-computing/caching a value, which we do in tons of places. |
I thought about hacky because it would be the only instance filed that is not de/serialized for an essentially "record" object. I remember having discussion in the past about this being no good. But as commented earlier, I don't personally mind it. |
Both |
Thanks you are right. I forgot most of Metadata is now with ProjectMetadata. With ProjectMetadata, the |
We can avoid using the volatile field when there is only a single default project.
Relates: #123662