libexpr: Optimise Value::type(), ValueStorage::getInternalType()#15271
Merged
Ericson2314 merged 2 commits intomasterfrom Feb 17, 2026
Merged
libexpr: Optimise Value::type(), ValueStorage::getInternalType()#15271Ericson2314 merged 2 commits intomasterfrom
Value::type(), ValueStorage::getInternalType()#15271Ericson2314 merged 2 commits intomasterfrom
Conversation
This reduces the churn when changing up the order of values in a follow-up commit. This should have been done from the start ideally to improve readability.
Using nix::unreachable() in getInternalType() and type() turns out to be quite expensive and prevents inlining. Also Value::type got compiled to a jump table which has a high overhead from indirect jumps. Using an explicit lookup table turns out to be more efficient. This does mean that we lose out on nice diagnostics from nix::unreachable calls, but this code is probably one of the hottests functions in the whole evaluator, so I think the tradeoff is worth it. The nixUnreachableWhenHardened boils down to nix::unreachable when UBSan is enabled so we still have good coverage there.
Contributor
Author
|
This has a noticeable ~3% wall time reduction and ~5% less instructions executed for common eval scenarios. |
Ericson2314
approved these changes
Feb 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Using
nix::unreachable()ingetInternalType()andtype()turnsout to be quite expensive and prevents inlining. Also
Value::typegot compiled to a jump table which has a high overhead from indirect
jumps. Using an explicit lookup table turns out to be more efficient.
This does mean that we lose out on nice diagnostics from nix::unreachable
calls, but this code is probably one of the hottests functions in the whole
evaluator, so I think the tradeoff is worth it. The nixUnreachableWhenHardened
boils down to nix::unreachable when UBSan is enabled so we still have good
coverage there.
Context
Add 👍 to pull requests you find important.
The Nix maintainer team uses a GitHub project board to schedule and track reviews.