4 ms·
> For example, I feel it's just too easy to call `.clone()` to store an owned value instead of storing a reference and introducing the correct lifetime constrai
by stymaar 4y ago
> For example, I feel it's just too easy to call `.clone()` to store an owned value instead of storing a reference and introducing the correct lifetime constraints.
There's three levels when on the ownership ladder:
1. Taking ownership of an object: this means your have mutability and 'static lifetime. This often implies cloning though.
2. Having the object behind a ref-counted pointer (Rc or Arc in multi-trhreaded scenario) this way you have 'static but not mutability (unless you're using interior mutability with RefCell/Mutex but now you have to think about deadlock and all)
3. Taking shared references (&) to your object. It's the most performant but now you have to deal with lifetimes so it's not always worth it.
Rust beginners often jump from 1. to 3. (Or don't because “too tedious”) but 2. is a sweet spot that works in many cases and should not be overlooked!
- 0b01 4y ago4. Cast to &'static and *. Expert Rust hacker level.
- willglynn 4y ago`Box::leak()` gets you a `&'static` at runtime. No raw pointers and no `unsafe`, but also no way to free the memory (because `&'static`). This is a technique I've used in anger: https://github.com/AS207960/xml-serde/pull/8 https://github.com/AS207960/xml-serde/pull/8
- ibotty 4y agoI didn't read the pull request in detail, but won't leaking the fields lead to memory leaks when `xml-serde` is used in a long-running application?
- willglynn 4y agoThis particular situation had to do with `&'static str`s baked into the program repeatedly getting compiled into `Regex`es at runtime. It wasn't possible to precompile these `Regex`es due to `serde` architectural limitations. I chose to cache them at runtime by compiling `&'static str`s once and leaking to make a corresponding `&'static Regex`. This is a "leak" insofar as I can't ever release them, but it's leaking into a global cache, and it's bounded because the input strings can't ever be released either. There was a code path which handles dynamic strings, and that path still allocates and frees regexes after the changeset.