Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 59c889e2 authored by TROPHIME Valentin's avatar TROPHIME Valentin
Browse files

fix(iterator): block size error is cleaner

parent 86ba25ea
No related branches found
No related tags found
No related merge requests found
...@@ -6,7 +6,7 @@ edition = "2021" ...@@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
embassy-time = { version = "0.3.2", features = ["log", "std", ] } embassy-time = { version = "0.3.2", features = ["log", "std"] }
embassy-futures = "0.1.1" embassy-futures = "0.1.1"
[dev-dependencies] [dev-dependencies]
...@@ -20,3 +20,7 @@ critical-section = { version = "1.1", features = ["std"] } ...@@ -20,3 +20,7 @@ critical-section = { version = "1.1", features = ["std"] }
embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "bc0180800d751e651c0d15c807285c11cdb4f486" } embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "bc0180800d751e651c0d15c807285c11cdb4f486" }
embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "bc0180800d751e651c0d15c807285c11cdb4f486" } embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "bc0180800d751e651c0d15c807285c11cdb4f486" }
embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "bc0180800d751e651c0d15c807285c11cdb4f486" } embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "bc0180800d751e651c0d15c807285c11cdb4f486" }
[profile.release-with-debug]
inherits = "release"
debug = true
use embassy_futures::yield_now; use embassy_futures::yield_now;
use embassy_time::{Duration, Instant}; use embassy_time::{Duration, Instant};
// use tracing::{field, span, Level};
pub trait PreemptiveIterator: Iterator + Sized { pub trait PreemptiveIterator: Iterator + Sized {
async fn preemptive_fold<B, F: FnMut(B, Self::Item) -> B>( async fn preemptive_fold<B, F: FnMut(B, Self::Item) -> B>(
...@@ -25,6 +26,7 @@ impl<T, I: Iterator<Item = T>> PreemptiveIterator for I { ...@@ -25,6 +26,7 @@ impl<T, I: Iterator<Item = T>> PreemptiveIterator for I {
let mut n_iters = 1; let mut n_iters = 1;
let react_us = reactivity.as_micros(); let react_us = reactivity.as_micros();
loop { loop {
debug_assert!(n_iters > 0);
let start = Instant::now(); let start = Instant::now();
let mut total_block = 0; let mut total_block = 0;
let mut time = Duration::from_micros(0); let mut time = Duration::from_micros(0);
...@@ -44,8 +46,9 @@ impl<T, I: Iterator<Item = T>> PreemptiveIterator for I { ...@@ -44,8 +46,9 @@ impl<T, I: Iterator<Item = T>> PreemptiveIterator for I {
} }
let time_us = time.as_micros(); let time_us = time.as_micros();
// forced to pass by percent value to avoid floats for adjusting iteration // forced to pass by percent value to avoid floats for adjusting iteration
let percent = ((100 * (time_us - react_us)) / react_us) as usize; // hack 90 ratio < to have a upper bound for n_iters in order to avoid doubling in the next block
n_iters = total_block.saturating_sub(((total_block - 1) * percent) / 100); let ratio = ((90 * time_us) / react_us) as usize;
n_iters = (total_block * 100) / ratio;
yield_now().await; yield_now().await;
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment