Vec autoshrink?

Was there ever a proposal to have Vecs auto shrink (and if so, why was it rejected). Auto shrink here defined as:

if len <= capacity / 4, shrink capacity down to capacity / 2

(basically the inverse of double when out of space)

It's the common behavior across languages including C++ std::vector and Java ArrayList, so proposals and conversations may scattered over decades. Searching those terms instead would yields better result like this as they existed longer and have a lot more accumulated users.

1 Like

If interpreted strictly, it would make Vec::with_capacity a synonym for Vec::new.

Less cheekily, it's a horrible fit for things like buffers where you set the length to 0 often. Especially if you know the size you do need in advance.

You could make a new type that does it.

4 Likes

Doing a reallocation every time you clear a Vec would be less than great for performance and also be mostly redundant since Vec::shrink_to()and Vec::shrink_to_fit() already exist. It would also go against rust's general philosophy of not quietly performing magic behind your back.

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.