A 41% Throughput Win That Was Algebra, Not Assembly
A Go secret scanner sped up its Shannon entropy stage by rewriting the formula and precomputing a 4 KiB table, with no CGo, no unsafe, and no heap allocations.
The Engineer · Build desk

What happened
- Khaled Hani reports that rewriting the Shannon entropy equation and precomputing a small lookup table produced a 41.4% increase in benchmark throughput in the Crenox Git secret scanner.
- The optimized implementation maintained 0 heap allocations per operation.
- The post states the solution was not CGo, assembly, or unsafe code; it was algebra.
- Crenox runs as a pre-commit hook on a developer's workstation before every commit, so the scanner has to do a lot of work without adding noticeable latency to the development workflow.
- Crenox uses a three-tier detection pipeline: Tier 1 Aho-Corasick pattern matching across 125+ known provider patterns; Tier 2 Shannon entropy analysis for high-randomness candidates such as generic tokens, hashes and credentials; Tier 3 context-aware verification to reduce false positives from test fixtures, mock data and non-secret identifiers.
Why it matters
Khaled Hani, writing about the Crenox Git secret scanner, reports that the entropy stage of its detection pipeline got 41.4% more throughput after the Shannon entropy formula was algebraically restructured and the expensive part moved into a precomputed lookup table [1][2]. The interesting part is what was not used: the post is explicit that the fix was not CGo, assembly, or unsafe [3], which is the usual destination when someone decides a hot loop is out of headroom.
The constraint is real. Crenox runs as a pre-commit hook on a developer workstation before every commit, so the scanner has to finish its work without adding noticeable latency to the commit [4]. Detection runs in three tiers: Aho-Corasick matching over 125-plus known provider patterns, then Shannon entropy analysis on high-randomness candidates such as generic tokens, hashes and credentials, then context-aware verification to suppress false positives from test fixtures and mock data [5]. Tier 2 is called hundreds of thousands of times, according to the author, which is what makes per-call floating point arithmetic worth attacking [6].
The naive implementation builds a 256-entry frequency table, then for each non-zero count computes p = count/n and accumulates -p*log2(p): a float conversion, a division, a math.Log2 call and more arithmetic per distinct byte value [7][8]. Substituting p = c/n and applying the logarithm identity gives H = log2(n) - (1/n) * sum(c*log2(c)), because every byte contributes exactly once to the frequency table so the counts sum to n [9]. Mathematically identical, per the post [10], but the loop body is now c*log2(c) on an integer with a bounded range [11].
Bounded is the operative word. If the input is at most 512 bytes, no individual byte frequency can exceed 512, so every value of c*log2(c) the loop will ever need can be computed once at init into a [513]float64 array, sized 513 rather than 512 because Go arrays are zero-indexed and a run of 512 identical bytes needs index 512 [12][13][14]. That table is 4,104 bytes, roughly 4.01 KiB [15]. The fast path then does one math.Log2, on n, instead of one per distinct byte present, which in the naive version can be up to 256 [17]. Inputs over 512 bytes fall back to the original calculation [16]. Allocations stayed at zero per operation [2].
Read the 41.4% figure the right way. A 41.4% throughput increase corresponds to about 29% less time per call, which is a solid but not exotic result for removing a transcendental function from a loop [18]. It also only applies below the 512-byte threshold; anything larger takes the fallback and gets nothing [16].
What to watch: whether real scanner inputs actually sit under 512 bytes in practice, because a line-oriented scanner will and a blob-oriented one will not; whether the fallback path becomes the new hot path on large files, which would argue for a segmented or chunked table rather than a bigger one; and whether the two code paths agree closely enough at the entropy thresholds that drive Tier 3, since a candidate sitting on the boundary can be classified differently by two routines that are equivalent on paper. The transferable lesson is the ordering: rewrite the expression, bound the domain, precompute, and only then consider the escape hatches.
Clarity's read
What the record supports and how the coverage leans. The claims behind it follow.
Reality
- Evidence64
- Adoption14
- Hype gap+14
- Incentives62
- Confidence57
Claim ledger
Ranked by verification strength, evidence, and original report placement.
- [1]
Khaled Hani reports that rewriting the Shannon entropy equation and precomputing a small lookup table produced a 41.4% increase in benchmark throughput in the Crenox Git secret scanner.
- [2]
The optimized implementation maintained 0 heap allocations per operation.
- [3]
The post states the solution was not CGo, assembly, or unsafe code; it was algebra.
- [4]
Crenox runs as a pre-commit hook on a developer's workstation before every commit, so the scanner has to do a lot of work without adding noticeable latency to the development workflow.
- [5]
Crenox uses a three-tier detection pipeline: Tier 1 Aho-Corasick pattern matching across 125+ known provider patterns; Tier 2 Shannon entropy analysis for high-randomness candidates such as generic tokens, hashes and credentials; Tier 3 context-aware verification to reduce false positives from test fixtures, mock data and non-secret identifiers.
- [6]
The post states the entropy function is called hundreds of thousands of times, which makes the per-call floating-point operations worth optimizing.
- [7]
The naive Go implementation builds a freq [256]int frequency table over the input bytes, then for each non-zero count computes p = float64(count)/n and accumulates entropy -= p * math.Log2(p).
- [8]
For each distinct byte the naive hot loop performs a floating-point conversion, a floating-point division, a math.Log2 calculation, and additional floating-point arithmetic.
- [9]
Substituting P(x_i) = c_i/n into Shannon entropy and applying a logarithm identity yields entropy = log2(n) - (sum of c_i * log2(c_i)) / n, using the fact that every byte contributes exactly once to the frequency table so the counts sum to n.
- [10]
The post states the rewritten form is mathematically equivalent to the original Shannon entropy formula.
- [11]
In the rewritten form the loop only needs count * log2(count) per frequency, and count is an integer with a bounded range, which creates the opportunity to precompute the expensive part.
- [12]
The fast path targets inputs up to 512 bytes; when n <= 512 every individual byte frequency satisfies 1 <= c_i <= 512.
- [13]
An init function fills var xLog2xTable [513]float64 with float64(i) * math.Log2(float64(i)) for i from 1 to 512.
- [14]
Because Go arrays are zero-indexed and an input of 512 copies of the same byte gives count = 512, requiring index 512, [513]float64 is the correct array size.
- [15]
The table holds 513 float64 values: 513 x 8 bytes = 4,104 bytes, about 4.01 KiB.
- [16]
The optimized Shannon function uses the lookup table for inputs of 512 bytes or fewer and keeps the original per-probability calculation as a fallback for larger buffers, because a byte frequency can exceed 512 for larger inputs.
- [17]
The fast path evaluates math.Log2 exactly once per call (on n), while the naive loop evaluates math.Log2 once per non-zero frequency, up to 256 times, since the frequency table has 256 entries.
- [18]
A 41.4% throughput increase corresponds to roughly 29% less time per operation.
Get this ledger inside ChatGPT. Ask about any of our stories and the answer brings its figure, each claim’s verdict and the sources. Free, no account.
Sources
1 independent publisher whose own reporting we read for this story.
- dev.toHow We Increased Shannon Entropy Throughput in Go by 41% Using a Lookup Table.
1 article · August 16, 2026
Topics and entities
Follow any of these and your For You feed starts watching them — no settings page required.
Topics
- Developer Tooling LatencyFollow
- Go Performance OptimizationFollow
- Shannon Entropy AnalysisFollow
- Secret ScanningFollow