←back to thread

Go subtleties

(harrisoncramer.me)
234 points darccio | 1 comments | | HN request time: 0.217s | source
Show context
Someone ◴[] No.45670768[source]
FTA: “In Go, empty structs occupy zero bytes. The Go runtime handles all zero-sized allocations, including empty structs, by returning a single, special memory address that takes up no space.

This is why they’re commonly used to signal on channels when you don’t actually have to send any data. Compare this to booleans, which still must occupy some space.”

I would expect the compiler to ensure that all references to true and false reference single addresses, too. So, at best, the difference of the more obscure code is to, maybe, gain 8 bytes. What do I overlook?

replies(5): >>45671332 #>>45671424 #>>45671852 #>>45672220 #>>45674995 #
1. Zambyte ◴[] No.45671332[source]
> I would expect the compiler to ensure that all references to true and false reference single addresses, too.

Why? If you're sending a constant true to a channel, wouldn't that true value exist in the stack frame for the function call? It seems like that would make more sense than a pointer to a constant true value being stored in the stack frame and dereferencing that every time you need the constant value.

> So, at best, the difference of the more obscure code is to, maybe, gain 8 bytes. What do I overlook?

Constructing channels in a loop would potentially multiply memory usage here