Hi all,
Here is a small benchmark, to show the memory consumed by a buffered channel:
func getChan1() chan int {
return make(chan int, 101)
}
var c chan int
func BenchmarkChan1(b *testing.B) {
for i := 0; i < b.N; i++ {
c = getChan1()
}
}
This code tells me various values, depending on the buffer size S of my buffered channel, here are some values:
1 -> 112 B/op
2 -> 112 B/op
3 -> 128 B/op
4 -> 128 B/op
5 -> 144 B/op
6 -> 144 B/op
7 -> 160 B/op
I have thus deduced that the actual size occupied by a buffered channel of int, is given by this formula: size(channel of S ints) = 96 + Sizeof(int) * 2 * [(N+1) / 2]
Why does a channel of S ints occupy the same memory as a channel of S + 1 ints, when S is odd ?
This is strange but not strange enough. Now comes the weird part:
100 -> 896 B/op
101 -> 1024 B/op
102 -> 1024 B/op
103 -> 1024 B/op
104 -> 1024 B/op
199 -> 1792 B/op
200 -> 1792 B/op
201 -> 1792 B/op
202 -> 1792 B/op
300 -> 2688 B/op
301 -> 2688 B/op
998 -> 8192 B/op
999 -> 8192 B/op
1000 -> 8192 B/op
1001 -> 8192 B/op
What is going on here ? For a buffered channel of size > 100 there is an additional overhead, where does it come from ?
It is something to do with scaling, or maybe padding data structure to optimize ? Or maybe my benchmark malfunctions for large-ish numbers, how can one explain this behaviour ?