Eliminating Go Bounds Checks with Unsafe for Hot Path Optimization

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.
Bound checks elimination is probably one of the most robust, most productive optimization techniques in the Go world.
- naruhodo
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
- pjmlp
> 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
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