Skip to content

Comment on Eliminating Go bounds checks with unsafe

Comments

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

There is nothing in the language spec. It leaves it up to the implementation to decide how to optimize. Whether or not your implementation has an extension to perform the same will ultimately depend on the implementation you are using, although you are unlikely to find it in any of the popular implementations.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.