# Eliminating Go Bounds Checks with Unsafe for Hot Path Optimization

> Hacker News story 48908884 · language: en · submitted 2026-07-14
> Web version: https://zeli.app/story/48908884
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 52 · by [abnercoimbre](<https://news.ycombinator.com/user?id=abnercoimbre>) · 💬 22
[original](<https://blog.andr2i.com/posts/2026-07-06-eliminating-go-bound-checks-with-unsafe>) · [HN discussion](<https://news.ycombinator.com/item?id=48908884>)

## Summary

I explore how to eliminate Go bounds checks in performance-critical code using unsafe pointer arithmetic when the compiler cannot prove safety. By replacing standard library calls with unsafe techniques, we can reduce instruction count and branch overhead, turning non-leaf functions into efficient leaf functions. This approach, used in libraries like go-brrr and klauspost/compress, can more than double the speed of operations like little-endian integer loading.

## Key quote

> Bound checks elimination is probably one of the most robust, most productive optimization techniques in the Go world.

## HN discussion (22 comments)

### [naruhodo](<https://news.ycombinator.com/item?id=48976555>)

> I like reading articles like this, but as per usual, I also find these articles frustrating to read because they don't specify calling conventions[0] (which are many and varied) - particularly the allocation of arguments to registers and the stack frame.
>
> Articles about GoLang assembly language[1] are particularly vexing because the instruction parameters are bass-ackwards - source, destination - like AT&T syntax, but register references are missing their % sigil and so appear to be MASM-style[2].
>
> Authors blogging from deep inside some technical tent should take pity on readers who are not so deeply in the tent and offer a brief primer on assumed knowledge.
>
> Any mistakes in the above should be viewed as confirmation of my confusion.
>
> [0] https://en.wikipedia.org/wiki/X86_calling_conventions
>
> [1] https://go.dev/doc/asm#x86
>
> [2]https://en.wikipedia.org/wiki/X86_assembly_language

### [pjmlp](<https://news.ycombinator.com/item?id=48977282>)

> \> As I already complained I wish Go had the nobounds compiler hint but it doesn't, so the only viable option we are left with is using unsafe pointer arithmetic.
>
> Because what the IT infrastructure needs is more CVEs.

### [archargelod](<https://news.ycombinator.com/item?id=48976341>)

> Is there any way in Go to selectively turn off bounds checking for a block, function or module? E.g. in Nim I can just do:
>
> proc littleEndian(b: openarray[byte]): uint32 =
>
> {.push boundChecks: off.}
>
> return uint32(b[0]) or (uint32(b[1]) shl 8) or (uint32(b[2]) shl 16) or (uint32(b[3]) shl 24)
>
> {.pop.}
>
> And GCC is smart enough to reduce it to a single operation:
>
> 000000000000bf80 <littleEndian_u0__session95202695079520951784538200>:
>
> bf80:       8b 07                   mov    (%rdi),%eax
>
> bf82:       c3                      ret

---

## Cite

- Page: https://zeli.app/story/48908884
- Date: 2026-07-14
- Source: blog.andr2i.com (https://blog.andr2i.com/posts/2026-07-06-eliminating-go-bound-checks-with-unsafe)
- Hacker News: https://news.ycombinator.com/item?id=48908884

## Other languages

- zh: https://zeli.app/zh/story/48908884 · markdown: https://zeli.app/zh/story/48908884.md
- ja: https://zeli.app/ja/story/48908884 · markdown: https://zeli.app/ja/story/48908884.md
- ko: https://zeli.app/ko/story/48908884 · markdown: https://zeli.app/ko/story/48908884.md
- de: https://zeli.app/de/story/48908884 · markdown: https://zeli.app/de/story/48908884.md
- es: https://zeli.app/es/story/48908884 · markdown: https://zeli.app/es/story/48908884.md
