3 ms·
Go gives you data structures with fewer pointers than Java, which helps cut down on GC pressure. type point struct { x int16 y int16 }
by crawshaw 12y ago
Go gives you data structures with fewer pointers than Java, which helps cut down on GC pressure.
type point struct {
x int16
y int16
}
points := make([]point, 1e6)
The value points uses 4 MB of memory and contains one pointer, not a million.
- eropple 12y agoSure. Now do this in Java (I have no idea why I'm getting downvoted for this thread because I think this stuff is fairly straightforward and nobody's actually making an argument to the contrary, but whatever): short[] x = new short[1000000]; short[] y = new short[1000000]; 4MB of shorts. Now you can use the argument of locality of reference, to be sure--but you're already throwing out the window by keeping around a million items. This is why I'm saying that in the what I would call most perf-critical cases, value type structs are a convenience much more than a tool to realize significant benefits. They are nice-to-haves. They don't make your code magicfast.