3 ms·
I’m also not much of a Go programmer, but looking at the benchmark code[1] it doesn’t actually use the values after they are retrieved, so if the compiler is sm
by paulgb 5y ago
I’m also not much of a Go programmer, but looking at the benchmark code[1] it doesn’t actually use the values after they are retrieved, so if the compiler is smart it may never have to check the type at runtime. It does, however, have to wrap the primitive in the `any` type, which seems to be what’s making it slow.
Question for Go programmers: does using the `any` type result in an additional memory lookup if the wrapped type is a sized primitive like an int, or is the value stored directly alongside the type information?
[1] https://github.com/sekoyo/deque/blob/86df0003850acaf3039c2d603cec858846816fac/deque_test.go#L578 https://github.com/sekoyo/deque/blob/86df0003850acaf3039c2d6...
Edit: I wrote that before you added the second paragraph, which sounds right to me.
- omegabravo 5y agoyes, interfaces have a memory redirection https://research.swtch.com/interfaces https://research.swtch.com/interfaces
- morelisp 5y agoThis article is outdated; the second thing it says under "Memory Optimizations" does not happen anymore, and that's what's being asked about.
- morelisp 5y ago> does using the `any` type result in an additional memory lookup if the wrapped type is a sized primitive like an int, or is the value stored directly alongside the type information? The answer to this question is complicated because it's changed several times. Prior to Go 1.4, it stored word-sized or smaller values directly without allocation. Go 1.4 started a big GC rework, part of which required fields not contain possibly-pointer-possibly-other-values. https://github.com/golang/go/issues/8405 https://github.com/golang/go/issues/8405 Obviously this made people unhappy, so it kicked off several rounds of optimizations to claw back common cases over several versions, the most recent of which was 1.15's introduction of 256 interface constants which are automatically used for any numeric value that fits in a single byte (including booleans). 1.9 also made constant strings preallocate matching constant interfaces if it sees them passed as an interface. This is also why there's so much benefit to inliner improvements; any inlining improvement translates rapidly into devirtualization improvements.