3 ms·
Probably a naive question, but couldn't it do the same optimization for the code above? I believe Javascript engines like V8 do something of that sort.
by lhorie 6y ago
Probably a naive question, but couldn't it do the same optimization for the code above? I believe Javascript engines like V8 do something of that sort.
- erik_seaberg 6y agoA []int doesn’t have the same layout in memory as a []interface{}. You could copy the values to a new slice, but that’s expensive and it breaks anything relying on slices being mutable in place (this and covariance vs. contravariance is why Java won’t cast from int[] to Object[]).
- lhorie 6y agoSure, but that's when we think in similar terms to what memory layout C code represents as opposed to what actually gets emitted by gcc -O3. I definitely appreciate being able to mentally visualize what the memory layout of go code looks like, though I'm not sure that's actually an explicit goal of the language.
- kyrra 6y agoMemory layout is something people definitely consider and go. See posts like: https://dave.cheney.net/2015/10/09/padding-is-hard https://dave.cheney.net/2015/10/09/padding-is-hard