Skip to content

Commit 73bcd58

Browse files
authored
use BTreeMap instead of HashMap in async_support::FutureState (bytecodealliance#1339)
As of this writing, initializing the default hasher for `HashMap` requires calling `wasi_snapshot_preview1:random_get`, which requires initializing the `wasi_snapshot_preview1` adapter when targeting `wasm32-wasip2` and later, and that's expensive enough that we'd prefer to avoid it for apps which otherwise make no use of the adapter. Signed-off-by: Joel Dice <[email protected]>
1 parent 5ab27c5 commit 73bcd58

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

crates/guest-rust/rt/src/async_support.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
extern crate std;
88
use core::sync::atomic::{AtomicBool, Ordering};
99
use std::boxed::Box;
10-
use std::collections::HashMap;
10+
use std::collections::BTreeMap;
1111
use std::ffi::c_void;
1212
use std::future::Future;
1313
use std::mem;
@@ -68,7 +68,14 @@ struct FutureState {
6868

6969
/// State of all waitables in `waitable_set`, and the ptr/callback they're
7070
/// associated with.
71-
waitables: HashMap<u32, (*mut c_void, unsafe extern "C" fn(*mut c_void, u32))>,
71+
//
72+
// Note that this is a `BTreeMap` rather than a `HashMap` only because, as
73+
// of this writing, initializing the default hasher for `HashMap` requires
74+
// calling `wasi_snapshot_preview1:random_get`, which requires initializing
75+
// the `wasi_snapshot_preview1` adapter when targeting `wasm32-wasip2` and
76+
// later, and that's expensive enough that we'd prefer to avoid it for apps
77+
// which otherwise make no use of the adapter.
78+
waitables: BTreeMap<u32, (*mut c_void, unsafe extern "C" fn(*mut c_void, u32))>,
7279

7380
/// Raw structure used to pass to `cabi::wasip3_task_set`
7481
wasip3_task: cabi::wasip3_task,
@@ -89,7 +96,7 @@ impl FutureState {
8996
waker,
9097
tasks: [future].into_iter().collect(),
9198
waitable_set: None,
92-
waitables: HashMap::new(),
99+
waitables: BTreeMap::new(),
93100
wasip3_task: cabi::wasip3_task {
94101
// This pointer is filled in before calling `wasip3_task_set`.
95102
ptr: ptr::null_mut(),

0 commit comments

Comments
 (0)