Skip to content

libexpr: Optimise Value::type(), ValueStorage::getInternalType()#15271

Merged
Ericson2314 merged 2 commits intomasterfrom
faster-type-internal-type
Feb 17, 2026
Merged

libexpr: Optimise Value::type(), ValueStorage::getInternalType()#15271
Ericson2314 merged 2 commits intomasterfrom
faster-type-internal-type

Conversation

@xokdvium
Copy link
Contributor

Motivation

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.

Context


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

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.
@xokdvium
Copy link
Contributor Author

This has a noticeable ~3% wall time reduction and ~5% less instructions executed for common eval scenarios.

@Ericson2314 Ericson2314 added this pull request to the merge queue Feb 17, 2026
Merged via the queue into master with commit ebcd31e Feb 17, 2026
30 of 31 checks passed
@Ericson2314 Ericson2314 deleted the faster-type-internal-type branch February 17, 2026 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants