3 ms·
Reduce allocation in Go code
- Animats 12y agoGo does many small allocations behind the scenes. A "select" with more than one case does some allocation. The Go compiler generates special-case code for a single-case select. More than one case uses a complicated N-case library function that does some allocation. Passing a parameter as an "interface{}", or creating a slice, also causes the creation of temporary objects, which are usually (always?) on the heap. This may or may not be a big problem depending on how the allocator and garbage collector work. Anyone know?
- tptacek 12y agoFWIW: If you're working with strings gradually built out of other strings, the Golang idiom is bytes.Buffer, to which you write strings as if to a file. It's the Golang equivalent of building an array of strings in Python and join()ing it.
- kevin_thibedeau 12y agoAnd how does this work when Unicode combining characters come into play?