News for hackers. Stuff that matters.
Re:Identifying Rust's collect:<Vec<_>>() memory leak footgun by invpt on Thursday January 18, 2024 @12:39PM (#39044780)
I appreciate the mention of Box<[T]>. I've never (or at least very rarely?) seen people using it, even in rustc, although I'm sure there's somewhere in the compiler that uses it. I've kind of gotten the feeling that Box<[T]> is frowned upon as a needless optimization because the one-word storage difference is so minimal, but I appreciate having the immutable size semantics as a way of ensuring no extra allocations occur by way of the methods on Vec.
Re:Identifying Rust's collect:<Vec<_>>() memory leak footgun by tsnl on Thursday January 18, 2024 @01:53PM (#39045971)
Another downside of Box<T> is that Vec::into_boxed_slice copies all elements from the source vector. Vec::shrink_to_fit does not appear to copy. This is based on the stable docs.
Re:Identifying Rust's collect:<Vec<_>>() memory leak footgun by sapiogram on Thursday January 18, 2024 @02:02PM (#39046082)
It doesn't copy the elements if the source vector is already at max capacity, which you can often arrange for beforehand.
Re:Identifying Rust's collect:<Vec<_>>() memory leak footgun by cmrx64 on Thursday January 18, 2024 @02:14PM (#39046232)
For example, by explicitly calling shrink_to_fit. So you either have fragmentation, or an extra copy if you're not careful, but none of the solutions let you forget about the detail entirely.
Re:Identifying Rust's collect:<Vec<_>>() memory leak footgun by invpt on Thursday January 18, 2024 @02:34PM (#39046488)
If you look at the source for into_boxed_slice, it calls shrink_to_fit at the beginning before doing anything else. Hence the documentation is slightly wrong, and no copies occur.

Edit: I submitted a PR to clear up the docs: https://github.com/rust-lang/rust/pull/120110

Re:Identifying Rust's collect:<Vec<_>>() memory leak footgun by cmrx64 on Thursday January 18, 2024 @04:30PM (#39048035)
thank you!
Re:Identifying Rust's collect:<Vec<_>>() memory leak footgun by sapiogram on Thursday January 18, 2024 @02:53PM (#39046736)
Or alternatively, the vector was created with `Vec::with_capacity()` or `ExactSizeIterator::collect()`, and had the correct capacity all along.
Re:Identifying Rust's collect:<Vec<_>>() memory leak footgun by ironhaven on Thursday January 18, 2024 @01:27PM (#39045556)
Another rust type you should look out for is Box<str>In my rust code I find a lot more uses of Box<str> rather than Box<[T]>. Strings are often fixed length identifiers or usernames that get passed around frequently and stored in data structures. Box<str> can be a drop-in replacement in a surprising amount of code
Re:Identifying Rust's collect:<Vec<_>>() memory leak footgun by zozbot234 on Thursday January 18, 2024 @01:40PM (#39045785)
Arc<str> is an atomically ref-counted string, also quite useful in async or multi-threaded code where (unlike w/ Box) you sometimes can't know in advance which task will drop the last reference to some data.
Re:Identifying Rust's collect:<Vec<_>>() memory leak footgun by bobbylarrybobby on Thursday January 18, 2024 @03:36PM (#39047317)
Box<str> is used pervasively in the rust compiler instead of String for exactly this reason. Basically every string of code the compiler looks at is, unsurprisingly, constant.
Re:Identifying Rust's collect:<Vec<_>>() memory leak footgun by steveklabnik on Thursday January 18, 2024 @04:23PM (#39047957)
Yeah, the general concept here is https://en.wikipedia.org/wiki/String_interning, Box<str> is great for it.