size
size copied to clipboard
Incorrect size of [][]string
cnt := 1000
ss := make([][]string, cnt, cnt) // len = cap = 1000
s := make([]string, 1, 1) // len = cap = 1 : slice header = 24
s[0] = "1234567890" // len = 10 symbols/bytes, string header = 16
fmt.Println(size.Of(s)) // 50 = 10 + 16 + 24
for i := range ss {
ss[i] = s // the same value for all values in upper slice
}
fmt.Println(size.Of(ss)) // 74 !!!
74 bytes to store slice of 1000 elements?
Underlying array of upper slice should have (as I understand) 1000 pointers to s. So, the expected result have to be: 50(s) + 24(upper slice header) + 8(per pointer)*cnt = 8074.
Probably I'm not right. Elements of underlying array of upper slice of [][]string have to be slice headers. So, probably right result should be 26(string+header)+24(upper slice header)+24*1000(upper slice elements)=24050.
8074 can be correct for []*[]string with 1000 pointers to the same slice of strings: []string{"1234567890"}.
Probably fix:
>>>>>> size.go:37
case reflect.Slice:
-- // return 0 if this node has been visited already
++ // return slice header size if this node has been visited already
if cache[v.Pointer()] {
-- return 0
++ return int(v.Type().Size())
}
At least it works as it should (I think).