Build1 distinct publisher3 min readUpdated
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

Compiled by The EngineerSomething wrong?How this is made
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 [16]. Inputs over 512 bytes fall back to the original calculation [17]. 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 [17].
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.
Follow any of these and your For You feed starts watching them — no settings page required.
Ranked by verification strength, evidence, and original report placement.
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.
The post states the entropy function is called hundreds of thousands of times, which makes the per-call floating-point operations worth optimizing.
Evidence-backed comparisons of source perspectives and observed adoption signals. Read the methodology
Which Builder, Operator, and Investor concerns the observed source mix emphasized—not a truth score.
Evidence, demonstrated adoption, hype gap, incentives, and confidence are assessed independently, each on its own current evidence. How these are measured.
Reproducible method, self-reported measurement
The technical core is unusually well evidenced for a blog post: the algebraic derivation is shown step by step, both the naive and optimized Go functions are printed in full, the table size is justified arithmetically, boundary conditions are enumerated, and the exact benchmark command and raw ns/op output are included, making the result independently reproducible. Evidence stops short of high because the measurement comes solely from the author's own machine with no disclosed benchmark input size or byte distribution, no repetition/variance data, and no third-party reproduction anywhere in the cluster.
One self-reported in-project use
Adoption evidence is limited to the author's own project: the optimized function is described as shipping in the Crenox entropy package and serving the scanner's Tier 2 stage. The cluster contains no downloads, stars, dependent projects, external users, or any indication that other codebases have picked up the technique, so measured adoption is near the floor rather than absent.
Mildly overstated framing, honest internals
The framing leans on the larger of two equivalent numbers - 41.4% throughput rather than the ~29% latency reduction the post itself computes - and the '41%' headline describes one microbenchmarked function, not the scanner's end-to-end speed, while the fast path only covers inputs up to 512 bytes. The gap stays small because the post volunteers the latency arithmetic, discloses the fallback path and its 512-byte bound, and avoids claiming any product-level or user-visible improvement.
Author is the project's own maintainer
The post is a first-person ('How We Increased...') write-up by Khaled Hani about the Crenox repository he works on, published on a developer-blogging platform where visibility for the project is a direct benefit, and the only quantitative claims are self-run benchmarks of that project's code. That is a clear promotional incentive. It is tempered by the fact that the mechanism is fully disclosed as reproducible code and math rather than an unverifiable assertion, and no product, pricing, or funding ask appears in the cluster.
Verifiable technique, uncorroborated numbers
Confidence is moderate: the mathematical and implementation claims are checkable directly from the published derivation and code and are very likely correct, which anchors most of the cluster. The specific performance figures rest on a single self-run benchmark from one publisher with undisclosed inputs and no independent confirmation, and adoption beyond the author's own project is entirely unknown, which caps overall confidence near the middle.
build
Your meter now runs on someone else's machine: signed receipts, fsync, and failing open1 distinct publisher
build
Zero-Instrumentation Go Tracing Works, But You Are Now Maintaining ABI Offsets1 distinct publisher
build
A 12MB Go binary bets agent cost control is cache stickiness, not a dashboard1 distinct publisher
build
Your agent needs the API call, not the API key1 distinct publisher
Distinct publishers with included, body-backed reporting in this cluster.
dev.to
1 article · August 16, 2026