rust-lang / wg-allocators

Home of the Allocators working group: Paving a path for a standard set of allocator traits to be used in collections!
http://bit.ly/hello-wg-allocators
203 stars 9 forks source link

Const flags describing allocator properties #124

Open the8472 opened 2 months ago

the8472 commented 2 months ago

It would be useful if allocators had an associated constant of flags describing their properties, such as

Containers can then choose to optimize their behavior based on those properties.

(from https://github.com/rust-lang/wg-allocators/issues/120#issuecomment-1999418118)

Amanieu commented 2 months ago

Can you give examples of how generic code (e.g. Vec) could make use of these properties?

the8472 commented 2 months ago

support for alignment-changing realloc

Would allow the in-place collect code in vec to apply in more cases. Currently GlobaAlloc does this: https://doc.rust-lang.org/nightly/src/alloc/alloc.rs.html#302-321 which means shrinking efficiency depends on the layouts, but some allocators can do better than that

does realloc try to avoid memcpy

can help picking between new-allocation-and-grow vs. reuse-allocation-and-shrink strategies. I.e. when shrinking incurs memcpy costs then it's better to use a fresh allocation from the beginning.

It would also let us pick between realloc vs. alloc-manually-copy-dealloc when there's a huge discrepancy between capacity and length.

do realloc/dealloc care about the layout information being correct

lets in-place-collect skip some realloc calls

whether deallocation does anything

for non-drop T we can turn Container's Drop into a noop

And that's just examples. The list should be extensible of course. Flags would need to have conservative defaults.