Tech
July 20, 2026
0 views
2 min read

Eliminating Go bounds checks with unsafe

Curated by Patrick
Source: Hacker News
Eliminating Go bounds checks with unsafe
Tech Daily Byte Analysis

The author shows that the Go compiler inserts a runtime panic guard for every slice index, which adds a compare, a conditional jump and even a function call in tiny functions. By rewriting the classic `binary.LittleEndian.Uint32` wrapper to accept a raw slice and an offset, then using `unsafe.SliceData`, `unsafe.Add` and a direct `*uint32` dereference, the code eliminates the remaining check on `b[3]`. The new `loadU32LE` function compiles only on little‑endian CPUs (amd64, 386, arm64, loong64, ppc64le, wasm) via a build tag, and it removes the call‑induced prologue, yielding a leaner inner loop for the author’s Brotli encoder. The technique mirrors what the `klauspost/compress` package already does, confirming that this unsafe shortcut is battle‑tested in production compression libraries.

The move reflects a broader tension in the Go ecosystem: the language’s safety guarantees versus the relentless pressure to squeeze every cycle out of performance‑critical paths such as compression, networking, and serialization. As Go’s optimizer has grown smarter—automatically removing many checks when loop bounds are provable—developers still encounter stubborn cases where the compiler cannot infer safety, prompting a turn to `unsafe`. This mirrors patterns in Rust (using `unsafe` blocks) and C++ (intrinsics) where low‑level hacks are accepted when the payoff is measurable. The practice also underscores the importance of architecture‑specific builds; the same code would be undefined on big‑endian targets, limiting portability.

Looking ahead, the reliance on `unsafe` for bound‑check elimination carries risk: future Go releases may change slice layout or pointer semantics, potentially breaking the trick or introducing subtle bugs. Teams must rigorously audit that the offset never exceeds slice length, because the compiler will no longer guard against out‑of‑bounds reads. Monitoring Go release notes for BCE improvements is prudent—if the compiler eventually proves the safety of these patterns, the unsafe shim can be retired, restoring full language safety without sacrificing speed.

Key Takeaways

Rewriting `binary.LittleEndian.Uint32` with `unsafe.SliceData` and `unsafe.Add` removes the final bounds check and the call overhead, turning the routine into a leaf function.

The optimization is limited to little‑endian architectures and guarded by a build tag, so it cannot be used universally across all Go targets.

Conventional BCE tricks (e.g., slicing to the exact length before loops) still work, but some hot paths—like the Brotli encoder’s byte loads—require unsafe to achieve maximal performance.

Developers must treat such unsafe shortcuts as maintenance liabilities; future compiler

About the Source

This analysis is based on reporting by Hacker News. Here is a short excerpt for context:

Comments
Read the original at Hacker News

More in Tech