Re:Identifying Rust's collect:<Vec<_>>() memory leak footgun
by mplanchard on Thursday January 18, 2024 @10:01PM
(#39051099)
Thank you for explaining it this way. I’ve been seeing this bit of news off and on all day and knew there was something I wasn’t understanding. This was the key bit
It LOOKS like there is more because the capacity inflates. But capacity is written in terms of sizeof(T), not bits
the capacity (and length) is 128 times an array of 1024 of a u16. We then re-use the same underlying array which has a CAPACITY of 128 times (1024 * 16) and put in a u8 (the cast). So each item went from 1024 * 16 to 8.So previously we had 128 * 1024 * 16 = 2,097,152 bits.
How many times can we put 8 bits in a capacity of 2,097,152 bits?
262,144
How many times can we put 16 bits in a capacity of 2,097,152 bits?
131,072