Skip to content

Commit

Permalink
Leverage Rayon's fallback to simplify demo
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Jan 23, 2024
1 parent bd00649 commit 6148952
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
4 changes: 2 additions & 2 deletions demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ crate-type = ["cdylib"]
[dependencies]
wasm-bindgen-rayon = { path = "..", optional = true }
wasm-bindgen = { workspace = true }
rayon = { workspace = true, optional = true }
rayon = { workspace = true }
num-complex = "0.4.0"
once_cell = "1.7.2"
getrandom = { version = "0.2.2", features = ["js"] }
rand = "0.8.3"
hsl = "0.1.1"

[features]
parallel = ["rayon", "wasm-bindgen-rayon"]
parallel = ["wasm-bindgen-rayon"]
14 changes: 3 additions & 11 deletions demo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
use hsl::HSL;
use num_complex::Complex64;
use rand::Rng;
use wasm_bindgen::{prelude::*, Clamped};

#[cfg(feature = "parallel")]
use rayon::prelude::*;
use wasm_bindgen::{prelude::*, Clamped};

#[cfg(feature = "parallel")]
pub use wasm_bindgen_rayon::init_thread_pool;
Expand Down Expand Up @@ -74,19 +72,13 @@ impl Generator {
.copied()
}

// Multi-threaded implementation.
#[cfg(feature = "rayon")]
fn iter_bytes(&self) -> impl '_ + ParallelIterator<Item = u8> {
(0..self.height)
// Note: when built without atomics, into_par_iter() will
// automatically fall back to single-threaded mode.
.into_par_iter()
.flat_map_iter(move |y| self.iter_row_bytes(y))
}

// Single-threaded implementation.
#[cfg(not(feature = "rayon"))]
fn iter_bytes(&self) -> impl '_ + Iterator<Item = u8> {
(0..self.height).flat_map(move |y| self.iter_row_bytes(y))
}
}

#[wasm_bindgen]
Expand Down

0 comments on commit 6148952

Please sign in to comment.