-
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
Avoiding passing size and alignment to dealloc when allocator doesn't need them #120
Comments
The counter-argument is that since this information is always available at compile-time, why shouldn't we pass it to the allocator? This is an important optimization for allocators since the size of an allocation may not be stored near the allocation itself. C++ actually added sized deallocation functions in C++14 (https://open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3536.html) for this reason, but they have the problem of dealing with backwards-compatibility. |
It's only available in compile-time for things like But IMO it's counting pennies. I can't imagine a situation where data structure is required to keep track of allocation alignment causing problems. |
I know it's very useful for some allocators, but not for all of them. Specifically, for the very common case of executables using the Memory overhead for collections isn't a problem (they have to track sizes anyway), but it's a problem of code bloat. Deallocation calls in Rust are ubiquitous due to unwinding and use of I'd expect fn drop(&mut self) {
if A::NEEDS_LAYOUT {
A::dealloc(self.ptr, Layout::new(self.size, etc))
} else {
A::layoutless_dealloc(self.ptr)
}
} |
But your example passes layout into |
Doesn't this just boil down to inlining Regarding the system allocator we actually do make use of the alignment information for deallocation in some cases, for example on Windows: https://github.com/rust-lang/rust/blob/9aa232ecc7bb006a1fad404f437b049482021a3a/library/std/src/sys/pal/windows/alloc.rs#L210. I still think that we should keep the layout for deallocation, but I could be convinced otherwise if you can show a significant size saving in a real program (e.g. ripgrep). |
To be precise, it doesn't need alignment info per se. The issue is that it's like two allocators in one. The first simply wraps |
Unfortunately, the special
So I think for this to happen one of these needs to change. For example,
|
Tangentially, I think it would be useful if allocators had an associated constant of flags describing their properties, such as whether they support alignment-changing realloc (posix realloc doesn't, jemalloc does), whether their realloc avoids memcpy (doing virtual memory schenanigans), whether deallocation does anything (vs. an arena that drops all allocations at once) and similar things. Containers can then choose to optimize their behavior based on those properties. |
Currently Rust requires passing
Layout
to the deallocator. However, Rust is frequently used with theSystem
allocator that wrapslibc::free
, which doesn't need this information. This means that often all the extra size and layout tracking on the Rust side is wasted effort. This is a tiny code bloat, but deallocation code paths are nearly everywhere, so it adds up.https://rust.godbolt.org/z/K3xTd9MEr
I wonder if this could be somehow made optional in the
Allocator
trait?The text was updated successfully, but these errors were encountered: