Skip to content

Commit 7981473

Browse files
authored
wasmtime-wizer: make rayon optional (#12313)
* wasmtime-wizer: make rayon optional when compiling wasmtime-wizer to wasm32, rayon detects that and tries to use wasm-bindgen APIs the new rayon feature flag allows users to choose if snapshotting should be parallel or sequential * add the new wasmtime-wizer rayon feature flag to CI micro_checks
1 parent 666d337 commit 7981473

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ jobs:
431431
-p wasmtime-wizer --no-default-features --features wasmtime
432432
-p wasmtime-wizer --no-default-features --features wasmprinter
433433
-p wasmtime-wizer --no-default-features --features component-model
434+
-p wasmtime-wizer --no-default-features --features rayon
434435
-p wasmtime-wizer --no-default-features --all-features
435436
runs-on: ubuntu-latest
436437
steps:

crates/wizer/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ harness = false
2626

2727
[dependencies]
2828
log = { workspace = true }
29-
rayon = { workspace = true }
29+
rayon = { workspace = true, optional = true }
3030
clap = { workspace = true, optional = true }
3131
wasm-encoder = { workspace = true, features = ['wasmparser'] }
3232
wasmparser = { workspace = true, features = ['validate', 'features'] }
@@ -43,6 +43,10 @@ wasmtime-wasi = { workspace = true, features = ["p1"] }
4343
tokio = { workspace = true, features = ['macros'] }
4444

4545
[features]
46+
default = ["rayon"]
47+
# Enables parallel snapshotting.
48+
# Needs to be disabled if compiling wizer to single-threaded targets like wasm32-unknown-unknown
49+
rayon = ["dep:rayon"]
4650
# Enable this dependency to get messages with WAT disassemblies when certain
4751
# internal panics occur.
4852
wasmprinter = ['dep:wasmprinter']

crates/wizer/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub use wasmtime::*;
1919
mod component;
2020
#[cfg(feature = "component-model")]
2121
pub use component::*;
22+
#[cfg(not(feature = "rayon"))]
23+
mod rayoff;
2224

2325
pub use crate::info::ModuleContext;
2426
pub use crate::snapshot::SnapshotVal;

crates/wizer/src/rayoff.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::ops::Range;
2+
3+
pub trait ParallelIterator: Iterator {}
4+
5+
impl<T: Iterator> ParallelIterator for T {}
6+
7+
pub trait IntoParallelIterator {
8+
type Item;
9+
type Iter: Iterator<Item = Self::Item> + ParallelIterator;
10+
11+
fn into_par_iter(self) -> Self::Iter;
12+
}
13+
14+
impl IntoParallelIterator for Range<usize> {
15+
type Item = usize;
16+
type Iter = Range<usize>;
17+
18+
fn into_par_iter(self) -> Self::Iter {
19+
self
20+
}
21+
}
22+
23+
pub trait ParallelExtend<T> {
24+
fn par_extend<I>(&mut self, par_iter: I)
25+
where
26+
I: IntoIterator<Item = T>;
27+
}
28+
29+
impl<T> ParallelExtend<T> for Vec<T> {
30+
fn par_extend<I>(&mut self, par_iter: I)
31+
where
32+
I: IntoIterator<Item = T>,
33+
{
34+
self.extend(par_iter);
35+
}
36+
}

crates/wizer/src/snapshot.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use crate::InstanceState;
22
use crate::info::ModuleContext;
3+
#[cfg(not(feature = "rayon"))]
4+
use crate::rayoff::{IntoParallelIterator, ParallelExtend};
5+
#[cfg(feature = "rayon")]
36
use rayon::iter::{IntoParallelIterator, ParallelExtend, ParallelIterator};
47
use std::convert::TryFrom;
58
use std::ops::Range;

0 commit comments

Comments
 (0)