-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Implement allocators, integrate with box, smart pointers, containers #12038
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
#4252 is required for allocators (to be able to call |
Assigning 1.0 (at least to put a spec in, if nothing else, though we probably should have an implementation as well). Depending on how allocators integrate with |
We at least need to have high-confidence that we can do it post-1.0. |
If it's done with default parameters, it would be backwards compatible.
Functions like // this becomes a TreeSet<int, Heap> and will still print in errors as TreeSet<int>
let mut xs = TreeSet::new();
xs.insert(5);
channel.send(xs); // this is a TreeSet<int, CacheAlignedAllocator>
let mut xs = TreeSet::with_alloc(CACHE_ALIGNED_ALLOCATOR);
xs.insert(5);
channel.send(xs); |
cc me |
I was at Bloomberg last year, and I found https://github.com/bloomberg/bde/wiki/BDE-Allocator-Model to be extremely powerful, and potentially something to model Rust's allocators after. Pros: Where your memory comes from is very explicit. It makes it very easy to do things such as hierarchical allocators. For example, having a bump allocator allocate its memory out of an arena of bump allocators which allocates its memory out of the stack. Writing code that only uses stack in inner-loops becomes possible, and I've seen very little code like it outside of that company. Cons: Where your memory comes from is very explicit. Basically every struct that can allocate memory gains a new |
The precise details of this isn't quite true. Every struct that can allocate memory would gain a new field like (for illustration purposes): struct Foo<A: Allocator> {
// ...
alloc: A
} i.e. not required to be a pointer. Also, Rust has well-defined zero-sized types, so one can substitute in something like (Just glancing at the BDE API now, it seems like it would be preferable for Rust to expose an API like jemalloc's experimental API, where e.g. FWIW, #4252 is now closed, so it's probably possible to start experimenting with this seriously. |
The way BDE does it, is that every struct/class which allocates gets an |
If some allocator needs shared state, the struct MyCoolAllocator {
priv inner: Rc<MyCoolSharedState>
}
impl Allocator for MyCoolAllocator { ... } We definitely don't need to force every struct taking an allocator to be a word larger (since we have zero-sized types, unlike C++), or force the entirety of each allocators to be behind a pointer. |
The allocators are designed as honest to goodness classes in BDE, with pure virtual allocate() and deallocate() methods. I've seen traditional C++ template-based allocators in use and BDE allocators in practice, and I much preferred BDEs. The semantics were extremely clean, and the vtable dispatches were entirely worth it. |
Why not have impl Allocator for ~Allocator { ... }
// etc (would be just so one can choose between extra words & virtual calls, and static dispatch (and no extra words) seamlessly? (I guess this might not be quite so clean.) |
Yeah. At this point we're rehashing a lot of arguments that the guys behind BDE hashed out over years with the C++ standards committee. It's... a noisy one. I'm just putting the idea out there for now for when this gets looked at, as a possible inspiration for design. |
I've taken the liberty of updating the head comment of this issue. I'm going to try and keep it up to date with interesting tidbits and (eventually) a link to a concrete rfc. |
One more subtlety that will very likely come up during implementation: copy and move semantics. A copy will have to specify a new allocator to allocate out of, and there will likely have to be two versions of move: one which just moves a pointer, and the other which will move memory between two allocation zones. |
Triage: There seems to be active work on this. |
Nominating for removal. We've recently said that allocators are not 1.0. |
(P-high, not 1.0; removing nomination tag; should migrate to rfc repo issue.) |
(filed on RFC repo issue rust-lang/rfcs#538 ) |
Closing in favor of the RFC issue. |
Rustup r? `@ghost` changelog: none
RELATED WORK
BLOCKING BUGS
The text was updated successfully, but these errors were encountered: