Repository navigation
minor optimization for -mbmi2 compilation flag - #4746
Merged
Merged
Conversation
The decompression side already compiles out its runtime BMI2 probe when DYNAMIC_BMI2 is 0. The compression side did not: cctx->bmi2 was unconditional, so both ZSTD_initCCtx() and ZSTD_initStaticCCtx() issued a CPUID sequence per context even under -mbmi2, where the result is unused. Mirror the decompression side: guard the field and both assignments, add ZSTD_CCtx_get_bmi2(). Also route ZSTD_initStaticCCtx() through ZSTD_cpuSupportsBmi2(), which checks BMI1 as well — required because BMI2_TARGET_ATTRIBUTE enables "lzcnt,bmi,bmi2". ZSTD_createCCtx + ZSTD_freeCCtx under -mbmi2: 2031 -> 100 ns; unchanged without it. Fuzzer and zstreamtest pass under -O2, -mbmi2, and -DDYNAMIC_BMI2=0.
Both bmi2 fields claimed "== 1 if the CPU supports BMI2 and 0 otherwise", which is false for the value consumers actually see: when BMI2 is enabled at compile time, DYNAMIC_BMI2 is 0 and the accessors return 0 even though the CPU does support BMI2. The value selects between the default and the BMI2_TARGET_ATTRIBUTE variant of a function; it is not a capability bit. Benign today, since every consumer ignores it when DYNAMIC_BMI2 is 0, but it invites future code to branch on it as a capability test and silently take the wrong path on the fastest machines. Comments only, no functional change.
Cyan4973
force-pushed
the
cctx-static-bmi2
branch
from
August 27, 2026 19:47
046fa3b to
1fcad56
Compare
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.
When
-mbmi2is specified, the code is compiled withbmi2supposed present,therefore there is no need to check for the presence of this feature flag in the cpuid,
saving one request at each CCtx creation.
This is correctly optimized on the decompression side,
but not on the compression side, which still issues a request.
On systems working on top of VMs, the cpuid request is intercepted,
and its resolution is much longer than usual on bare metal,
moving up from ~100ns to ~2us.
Consequently, in scenarios where small files are repetitively compressed using fast mode while not re-using a CCtx,
this request is paid at each compression, and is no longer negligible.
This is fixed in this PR.