Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect size of [][]string #16

Open
Dmitriy-Kulagin opened this issue Oct 5, 2022 · 2 comments
Open

Incorrect size of [][]string #16

Dmitriy-Kulagin opened this issue Oct 5, 2022 · 2 comments

Comments

@Dmitriy-Kulagin
Copy link

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.

@Dmitriy-Kulagin
Copy link
Author

Dmitriy-Kulagin commented Oct 5, 2022

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"}.

@Dmitriy-Kulagin
Copy link
Author

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).

This was referenced Oct 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant