3 ms·
If you have a buffered channel with 100 "true"s in it, you're using 100 bytes. If you have a buffered channel with 100 "struct{}{}"s in it, you only need to st
by tczMUFlmoNk 11mo ago
If you have a buffered channel with 100 "true"s in it, you're using 100 bytes.
If you have a buffered channel with 100 "struct{}{}"s in it, you only need to store the length, since the element type is zero-sized.
- Someone 11mo agoSo, you’re having a channel through which you can only ever send "struct{}{}"s? If the compiler has an optimization to turn that into a single int, can’t it do the same for a channel that can only send other kinds of unique values? That would allow for more readable code.
- tczMUFlmoNk 11mo agoSure. If T is a zero sized type, then chan T should just be an atomic int. I am not sure how much this improves code readability in practice—I see chan struct{} frequently, but I cannot recall ever having seen chan T for a zero-sized T other than struct{}.