Skip to content
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

Const flags describing allocator properties #124

Open
the8472 opened this issue Apr 25, 2024 · 3 comments
Open

Const flags describing allocator properties #124

the8472 opened this issue Apr 25, 2024 · 3 comments

Comments

@the8472
Copy link
Member

the8472 commented Apr 25, 2024

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

  • support for alignment-changing realloc (posix realloc doesn't, jemalloc does)
  • does realloc try to avoid memcpy (doing virtual memory schenanigans)
  • whether deallocation does anything (vs. an arena that drops all allocations at once)
  • support for zero-length allocations
  • if alloc_zeroed is any different from allocate
  • do realloc/dealloc care about the layout information being correct
  • etc.

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

(from #120 (comment))

@the8472 the8472 changed the title Const flags describing alloctor properties Const flags describing allocator properties Apr 25, 2024
@Amanieu
Copy link
Member

Amanieu commented Apr 25, 2024

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

@the8472
Copy link
Member Author

the8472 commented Apr 25, 2024

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.

@the8472
Copy link
Member Author

the8472 commented Sep 27, 2024

Another useful property would be if the allocator could indicate support for zero-sized allocations, e.g. by always returning NonNull::dangling.
With this knowledge we would be able to optimize out some branches in alloc code that check the requested allocation size and always call the allocator instead. This would simplify things for LLVM since it would see a straight, branch-free path from allocation, use, deallocation, which can help optimizing temporaries

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants