-
Notifications
You must be signed in to change notification settings - Fork 9
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
Split Allocator
trait
#112
Comments
The main motivation here is performance. Not only |
See #9 for a long discussion precisely this issue. My position is that:
In the end I don't think the use cases justify the additional API complexity. |
And there are allocators that need to perform actual deserialization but restore state from pointer. I agree that proposed change is not that useful for I can't agree that custom allocators are used mostly with And note that |
I still think this introduces a huge amount of API complexity. I'd recommend reading the full discussion in #9, it has examples like |
Box is clonable if
Making always panicking I don't think
unsafe trait Deallocator {
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout);
}
impl<A> Deallocator for A
where
A: Allocator,
{
#[inline(always)]
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
Allocator::deallocate(self, ptr, layout);
}
} Collection types may start using |
I don't think leaking is acceptable for arenas like Bumpalo, but I'm not sure the premise of the motivation is correct:
I think based on the definitions, if the allocator is zero-sized just like But, I do understand that for some allocators, you need state for the allocation but not for the deallocation, so for these types, splitting them would be ideal so that things which only need dealloc can be smaller. I think that's maybe what they were actually implying, just under specified. |
Hello, splitting allocator is useful for the following use case, while I'm not sure if in the form originally presented. I want to use allocator api to |
Exactly. Where "some" is for example For this reason people actually copy |
Maybe instead of making allocator and deallocator traits allow, under specific conditions, allocate and deallocate using different allocators? In case of box it would mean, for example,
I see the benefit there: you may construct initial allocator with initial pointer to memory, and then when Vec would want grow or shrink memory, it would pass previous pointer. That way allocator would not need state. Allowing allocations and deallocations under specific (to be documented) circumstances is a lot less disruptive and "light" change, while covering OP use case for bumpalo and other too |
Getting rid of state that allows allocations, but still having |
Note that there is independently some desire to have a kind of " Although, unfortunately, while this works for the uniquely-owned As an interesting side note, with the storage model, the duplicated pointer in |
It is up to implementor of the trait. My use case involves storing a single For
|
Wouldn't it be better to make |
In the case of I personally feel that in practice, there aren't enough of these generic uses of |
You still insist that having I insist that owning But that's true that I don't have more use-cases. Only allocators with zero-state deallocators to save memory. |
Currently there's only
Allocator
trait that provides both allocations and deallocations.And
Box
,Vec
and other types hasA: Allocator
generic parameter.However there are allocators with no-op deallocations and thus do not require collections and smart-pointers to keep any allocator state to deallocate.
It would reduce size and improve performance somewhat significantly if
Box<T, A>
would be able to use ZSTA
parameter if no state is required for deallocation and allocation is not needed.I propose the following solution:
Split
Allocator
trait into two -Deallocator
andAllocator
.They can be defined as following.
Define that deallocator
deallocator: D
created using<D as From<A>>::from(alloc)
may deallocate memory allocated byalloc
, any of its copy and equivalent allocators.Leave only
A: Deallocator
bound on collections and smart-pointers and all their impl blocks where allocation is not performed.Implement
From<Box<T, A>> for Box<T, D> where D: From<A>
and similar impls for other types with allocator type.This impl may conflict with others. The alternative is to add a method.
After this is done then allocators with possible no-op deallocation (like
bumpalo::Bump
orblink_alloc::BlinkAlloc
) may define ZST deallocator type that does nothing on deallocation and only provides a lifetime to ensure that allocator is not reset.On the
bumpalo
as exampleThe text was updated successfully, but these errors were encountered: