yoshuawuyts / futures-concurrency

Structured concurrency operations for async Rust
https://docs.rs/futures-concurrency
Apache License 2.0
400 stars 31 forks source link

Implement precise waking on `alloc` targets #166

Open yoshuawuyts opened 6 months ago

yoshuawuyts commented 6 months ago

https://github.com/yoshuawuyts/futures-concurrency/pull/163 has introduced support for #[no_std] environments, but in order to achieve that it has implemented a less efficient version of WakerArray and WakerVec, which wake all of their child futures every time they're woken. We should implement a more efficient version of this which works on #[no_std] environments.

This is not needed for correctness; merely for efficiency. But especially on embedded environments, efficiency matters - so it really would be great if we could tackle this. Thanks!

alexmoon commented 6 months ago

I think that precise waking is going to require some sort of allocator. The waker context needs to be able to outlive the associated future. In no_std you could do something like a static arena of wakers you can allocate from. You would probably also want some way to define the size of that arena at compile time.

For alloc it's significantly simpler. I think the goal here should be to make ReadinessVec/ReadinessArray lock-free which would enable their use in both std and alloc contexts (and maybe improve performance at the same time).

max-heller commented 6 months ago

In no_std you could do something like a static arena of wakers you can allocate from. You would probably also want some way to define the size of that arena at compile time.

I've experimented with this approach but haven't found a way to make it ergonomic. With TAIT it becomes possible to define type-inferred statics with a macro, which might help