benbjohnson / immutable

Immutable collections for Go
MIT License
713 stars 32 forks source link

Why ImmutableList of size 7 occupies 128 bytes? #42

Closed wzslr321 closed 1 year ago

wzslr321 commented 1 year ago

List below contains 7 elements, but when I inspect it with debugger it resizes to length of 32, filling the rest with empty strings. I'm curious, what is the reason for that? I understand that resizing is expensive, but it seems like a little bit too much at first glance.

list := immutable.NewList[string](
    "one",
     "two",
    "three",
    "four",
    "five",
    "six",
    "seven",
)

image

image

benbjohnson commented 1 year ago

This library is a port of immutable.js so I'm not sure what they're reasoning was for 32 specifically. IIRC the number needs to be a power of two so maybe it could be dropped down to 16 and that would make more sense.

It really only matters if you have a lot of small lists. In my experience, it's more common to see one or a few large immutable collections being shared rather than lots of small ones.

wzslr321 commented 1 year ago

Thanks for the answer :)