You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allocating currently is based on a size in bytes and an alignment. For a given type Talign is simply the alignment of T in most cases and size is a multiple of the size of T. More exotic layouts can be constructed with a [repr(align(...))] struct containing [u8; N].
Currently Layout looks like this:
structLayout{size:usize,align:NonZeroUsize,}
size is a multiple of mem::size_of::<T>() and align is almost always mem::align_of::<T>(). If we would add T to this struct, we could simply remove align, so Layout would look like this (Renamed to MemoryLayout as Layout is stable. Also MemoryLayout is more describtive.):
structMemoryLayout<T>{capacity:usize,}
We want mem::size_of::<Option<MemoryLayout<T>>>() == mem::size_of::<MemoryLayout<T>>(). Do we really need a layout with zero capacity? We have allowed ZSTs in AllocRef, which is fine, but I really don't think we need to support zeroed-capacity allocations.
AllocRef has to support unsized types to deallocate Box<T: !Sized>, this must be changed to MemoryLayout<T: ?Sized>, but size_of and align_of requires T: Sized so we need to store the alignment for T: !Sized. Also we can't use capacity, as we don't know the size of T but we can still use size instead.
Okay, thats the same as Layout. However we can abuse the layout of pointers: T: Sized pointers are 8 bytes wide, T: !Sized are 16 bytes wide, a fat pointer. Putting everything in one struct:
and wrap the different functions. Our layout is now capacity-based for sized types.
I think this is a pretty big deal, as we halfed the size of Layout which is regularly used in the allocator API (for sized types, which is almost always the case. For unsized types nothing changes).
The text was updated successfully, but these errors were encountered:
I don't think this is the right abstraction for the AllocRef trait. Your proposed API seems primarily aimed at array-like allocations, but most of the capacity-based allocation logic should be handled by RawVec instead of messing with the generic allocation API.
I'm pretty happy with the current Layout type, it describes exactly what information needs to be passed to an allocator: the size of a block of memory and its alignment.
Like @Amanieu, I feel that this focuses too much on a particular use case, while adding a lot of complexity. The Layout is a simple yet versatile solution to the same(?) problem.
Allocating currently is based on a size in bytes and an alignment. For a given type
T
align
is simply the alignment ofT
in most cases andsize
is a multiple of the size ofT
. More exotic layouts can be constructed with a[repr(align(...))]
struct containing[u8; N]
.Currently
Layout
looks like this:size
is a multiple ofmem::size_of::<T>()
and align is almost alwaysmem::align_of::<T>()
. If we would addT
to this struct, we could simply removealign
, soLayout
would look like this (Renamed toMemoryLayout
asLayout
is stable. AlsoMemoryLayout
is more describtive.):We want
mem::size_of::<Option<MemoryLayout<T>>>() == mem::size_of::<MemoryLayout<T>>()
. Do we really need a layout with zero capacity? We have allowed ZSTs inAllocRef
, which is fine, but I really don't think we need to support zeroed-capacity allocations.AllocRef
has to support unsized types to deallocateBox<T: !Sized>
, this must be changed toMemoryLayout<T: ?Sized>
, butsize_of
andalign_of
requiresT: Sized
so we need to store the alignment forT: !Sized
. Also we can't usecapacity
, as we don't know the size ofT
but we can still usesize
instead.Okay, thats the same as
Layout
. However we can abuse the layout of pointers:T: Sized
pointers are 8 bytes wide,T: !Sized
are 16 bytes wide, a fat pointer. Putting everything in one struct:Now we have to interpret the data. For
T: Sized
T: Sized
:T: !Sized
:Then we just change
MemoryLayout
toand wrap the different functions. Our layout is now capacity-based for sized types.
I think this is a pretty big deal, as we halfed the size of
Layout
which is regularly used in the allocator API (for sized types, which is almost always the case. For unsized types nothing changes).The text was updated successfully, but these errors were encountered: