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

Expose the event bump allocator. #44

Open
rj00a opened this issue Apr 12, 2024 · 5 comments
Open

Expose the event bump allocator. #44

rj00a opened this issue Apr 12, 2024 · 5 comments

Comments

@rj00a
Copy link
Owner

rj00a commented Apr 12, 2024

For reasons of efficiency, events in evenio are allocated in a bumpalo bump allocator. This allocator is cleared once the top level World::send call returns. It would be useful if users had access to this arena for their own purposes. It should be possible for events to safely contain data borrowed from the allocator, which would eliminate the need for heap allocations in many cases.

Essentially, we remove the 'static requirement from Sender::send and replace it with, say, 'e representing the lifetime of data allocated from the arena. I would expect the 'e lifetime to carry through received events as well, so that data from a received event can be stuffed into a sent event without cloning.

There's also the issue of collections. Custom allocators are still nightly-only, so we would have to fork the standard collections as bumpalo does. I would expect this to be available behind a non-default feature flag.

If #43 happens then I'm guessing we would need a 'w: 'e bound somewhere.

Requirements

  • Safe interface.
  • Events can take borrowed data from a received event and put it in a sent event.
  • Items from the bumpalo crate are not exposed in the public API directly.
  • Some way to allocate + send in a &mut World context.
  • Optional collections module.
@andrewgazelka
Copy link
Contributor

This would be amazing, especially for what I am doing with https://github.com/andrewgazelka/hyperion. Just ran into this issue recently.

@andrewgazelka
Copy link
Contributor

Can you lay out more specifics for how you think this will be done? For instance, will there be a new handler parameter that is specifically used to obtain the bump allocator? Such as a struct named Bump, GlobalBump, or WorldBump? Or do you think we will allow World to be a handler parameter, as is the case in Bevy, I believe?

@rj00a
Copy link
Owner Author

rj00a commented May 5, 2024

I'm not sure exactly. Sender already has an exclusive reference to the allocator in order to queue events, so it would probably make sense to have the allocation API there. If we want received events and sent events to share a lifetime, we might also need some sort of combined SenderReceiver handler param. Because as of now, fn<'a>(Receiver<'a, E>, Sender<'a, ()>) isn't a valid handler and I'm not sure what type-level wizardry is required to make that work.

@andrewgazelka
Copy link
Contributor

andrewgazelka commented May 15, 2024

I'm not sure exactly. Sender already has an exclusive reference to the allocator in order to queue events, so it would probably make sense to have the allocation API there. If we want received events and sent events to share a lifetime, we might also need some sort of combined SenderReceiver handler param. Because as of now, fn<'a>(Receiver<'a, E>, Sender<'a, ()>) isn't a valid handler and I'm not sure what type-level wizardry is required to make that work.

CleanShot 2024-05-15 at 11 04 56@2x

Are you sure Sender needs an exclusive reference to bumpalo? I'm pretty sure allocating just requires a shared reference it's not Send or Sync.

So, since everything is not Send and Sync right now, you should be able to have a shared reference, I think.

@rj00a
Copy link
Owner Author

rj00a commented May 19, 2024

Yeah since Sender is !Send and !Sync I think we can just make everything internally mutable.

rj00a added a commit that referenced this issue May 19, 2024
This PR removes the `'static` requirement from `Sender::send` and
`Sender::send_to` and exposes the bump allocator by providing allocation
methods on `Sender`. `Sender` is now entirely internally mutable (takes
`&self` on all the methods).

This partially addresses #44 but is missing a few things.
- Doesn't allow received events with borrowed data to pass though
`Sender::send` without a clone because `Receiver` and `Sender` have
different lifetimes. Might still need a combined `Sender` and `Receiver`
type.
- Can't use the bump allocator from a `&mut World`.
- No optional `collections` module. I think it would be easier to just
add an unstable feature for the unstable allocator api for those who
need it.
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