Too Much Go Misdirection
Published on: 2025-07-03 00:40:32
Poking through layers of indirection in go trying to recover some efficiency.
Many functions in go take an io.Reader interface as input. This is a sensible default, allowing for streaming of data, instead of loading the entirety into memory. In some cases, though, we need the bytes. Which we may already have, in which case it would be great to simply use those bytes. Alas, this can be quite difficult.
context
I’m decoding some images. I’m using libavif and libheif via C bindings. For reasons primarily motivated by simplicity, I’m using the simple memory interfaces for these libraries, which makes it much easier to get the data from go into C. The streaming interface is much more work, and anyway the libraries would then just buffer the data internally, making another copy. Not every decoder fully works in a streaming fashion.
So the primary do the work function takes a []byte and passes it to C, and there’s a wrapper that does things the go way with an io.Reader , which does a full
... Read full article.