Newer
Older
# Preemptive iterator
Prototype aiming to provide a simple interface to introduce preemptive iterator
for asynchronous Rust programs. Its purpose is to increase reactivity of
`async fn` spending all of it's time in a long blocking loop task by simply
changing the final iterator method chains.
It provides the `PreemptiveIterator` trait implemented for all standard Rust
types implementing `core::iter::Iterator`. For the moment our trait provide only
`fold, for_each` equivalent.
Typical overhead values is between 1 and 5%. Worst case around 30% and happens
when using few iterations (<100) with short duration tasks (< 10 ns).
Reduce the Overhead for the worst cases, there are some ideas :
- Increasing the number of iterations for the base case of exponential grows i.e
starts at 8 or 16 or 32 instead of 1. This reduces the number of calls to
`embassy_time::Instant::elapsed` and also reduce the number of call to fold
and next and do less branching in the while loop. This is easy but can also be
bad for reactivity purposes if the real block size that should be found is
less than this starting size.
- Change the implementation completely and use a type trick such as in rayon.
This could reduce the overhead of the calls to fold and next. Even if you
don't do any call to `elapsed` the overhead is still of approximately 10% on
the worst cases. This approach could help us to decrease this overhead.
- Criterion helps reduce benchmarking weirdness but still need to run bench
with:
`RUSTFLAGS="-C llvm-args=-align-all-functions=6 -C llvm-args=-align-all-nofallthru-blocks=6" cargo bench`
To reduce impact of alignment.
- Try various block size policies:
- Adapt grow rate for exponential grows up with information from measured
time.
- Size of the starting point inferred from reactivity ?
- `try_fold, try_for_each` (problem: generic `Try` trait is unstable)
- `all, any, collect, count, lt, ge, inspect, last, max, min`
- `all, any` ?
- `collect, partition, unzip` ?
- `count, last` ?
- `ge, gt, le, lt, eq, ne` ?
- `min, max, min_by, max_by, min_by_key, max_by_key` ?
- `product, sum, reduce` ?