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

Feature request: const fn new #7

Closed
kstrafe opened this issue Apr 9, 2024 · 2 comments · Fixed by #9
Closed

Feature request: const fn new #7

kstrafe opened this issue Apr 9, 2024 · 2 comments · Fixed by #9
Assignees
Labels
enhancement New feature or request

Comments

@kstrafe
Copy link

kstrafe commented Apr 9, 2024

Making Bump::new be const fn allows for use in thread locals via

thread_local! {
    static X: Bump = const { Bump::new() };
}

This makes thread local access much faster because we do not need to perform a dynamic check for each access to the thread local.

@bluurryy bluurryy added the enhancement New feature or request label Apr 10, 2024
@bluurryy
Copy link
Owner

bluurryy commented Apr 10, 2024

If we implemented new like bumpalo using their EMPTY_CHUNK trick, we could make it const in nightly with #![feature(const_refs_to_static)].

The thing is that we have a doubly linked list of chunks instead of singly linked list. This is so we can reuse and don't have to deallocate chunks that were allocated in a scope.

We would have to check every time we create a new scope or checkpoint if we are pointing to that EMPTY_CHUNK and if so allocate a real chunk that we can mutate and link up with future chunks.

EDIT: Is that tradeoff worth it?

@bluurryy
Copy link
Owner

bluurryy commented Apr 20, 2024

Bump allocators can now be constructed without allocation via Bump::unallocated. This only works with the Global allocator.
It's const when the feature nightly-const-refs-to-static is enabled.

type Bump = bump_scope::Bump<Global, 1, true, false>;

thread_local! {
    static BUMP: Bump = const { Bump::unallocated() };
}

An unallocated bump allocator can't create scopes or checkpoints, but everything else works.
You can use scopes by converting it into a guaranteed allocated bump allocator with into_guaranteed_allocated or as_guaranteed_allocated(_mut).

EDIT: updated function names for v0.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants