Skip to content

Commit 6bd500d

Browse files
authored
wasmparser: Allow to drop hash-based dependencies entirely (#1866)
* remove unnecessary cfgs in reserve method impl * remove no longer needed cfg guard * re-design some crate features for wasmparser - Adds `hash-collections` crate feature. - Adds `prefer-btree-collections` crate feature. - Removes `no-hash-maps` crate feature. Advantages of the new design: - This allows to get rid of `ahash`, `hashbrown` and `indexmap` dependencies entirely when the `hash-collections` crate feature is disabled. - This still allows to use btree-collections instead of hash-collections when `prefer-btree-collections` is enabled, similar to `no-hash-maps`. * apply rustfmt * remove commented-out code * add note to hash-collections comment * add hash-collections to default feature set This is to keep the same semantics as previously. * fix CI for new crate features
1 parent 1620954 commit 6bd500d

File tree

8 files changed

+70
-33
lines changed

8 files changed

+70
-33
lines changed

.github/workflows/main.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,16 @@ jobs:
5656
name: bins-${{ matrix.build }}
5757
path: dist
5858

59-
test-no-hash-maps:
59+
test-prefer-btree-collections:
6060
name: Test (no-hash-maps)
6161
runs-on: ubuntu-latest
6262
steps:
6363
- uses: actions/checkout@v4
6464
with:
6565
submodules: true
6666
- uses: bytecodealliance/wasmtime/.github/actions/[email protected]
67-
- name: Test (no-hash-maps)
68-
run: cargo test --workspace --locked --features no-hash-maps
67+
- name: Test (prefer-btree-collections)
68+
run: cargo test --workspace --locked --features prefer-btree-collections
6969

7070
test:
7171
name: Test
@@ -238,14 +238,14 @@ jobs:
238238
- run: cargo check --no-default-features -p wasmparser
239239
- run: cargo check --no-default-features -p wasmparser --target x86_64-unknown-none
240240
- run: cargo check --no-default-features -p wasmparser --target x86_64-unknown-none --features validate,serde
241-
- run: cargo check --no-default-features -p wasmparser --target x86_64-unknown-none --features validate,serde,no-hash-maps
241+
- run: cargo check --no-default-features -p wasmparser --target x86_64-unknown-none --features validate,serde,prefer-btree-collections
242242
- run: cargo check --no-default-features -p wasmparser --features std
243243
- run: cargo check --no-default-features -p wasmparser --features validate
244244
- run: cargo check --no-default-features -p wasmparser --features features
245245
- run: cargo check --no-default-features -p wasmparser --features features,validate
246-
- run: cargo check --no-default-features -p wasmparser --features no-hash-maps
246+
- run: cargo check --no-default-features -p wasmparser --features prefer-btree-collections
247247
- run: cargo check --no-default-features -p wasmparser --features serde
248-
- run: cargo check --no-default-features -p wasmparser --features serde,no-hash-maps
248+
- run: cargo check --no-default-features -p wasmparser --features serde,prefer-btree-collections
249249
- run: cargo check --no-default-features -p wasmparser --features component-model
250250
- run: cargo check --no-default-features -p wasmparser --features component-model,validate
251251
- run: cargo check --no-default-features -p wasmparser --features std,component-model
@@ -315,7 +315,7 @@ jobs:
315315
- verify-publish
316316
- test_capi
317317
- test_extra_features
318-
- test-no-hash-maps
318+
- test-prefer-btree-collections
319319
- clippy
320320
if: always()
321321

crates/wasmparser/Cargo.toml

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,30 @@ name = "benchmark"
4343
harness = false
4444

4545
[features]
46-
default = ['std', 'validate', 'serde', 'features', 'component-model']
46+
default = ['std', 'validate', 'serde', 'features', 'component-model', 'hash-collections']
4747

4848
# A feature which enables implementations of `std::error::Error` as appropriate
4949
# along with other convenience APIs. This additionally uses the standard
5050
# library's source of randomness for seeding hash maps.
5151
std = ['indexmap/std']
5252

53-
# Tells the wasmparser crate to avoid using hash based maps and sets.
53+
# Tells the `wasmparser` crate to provide (and use) hash-based collections internally.
5454
#
55-
# Some embedded environments cannot provide a random source which is required
56-
# to properly initialize hashmap based data structures for resilience against
57-
# malious actors that control their inputs.
58-
no-hash-maps = []
55+
# Disabling this crate feature allows to drop `hashbrown`, `indexmap` and `ahash` dependencies
56+
# entirely, reducing compilation times and shrink binary sizes.
57+
hash-collections = [
58+
'dep:hashbrown',
59+
'dep:indexmap',
60+
'dep:ahash',
61+
]
62+
# Tells the `wasmparser` crate to prefer using its built-in btree-based collections
63+
# even if `hash-collections` is enabled.
64+
prefer-btree-collections = []
5965

6066
# A feature that enables validating WebAssembly files. This is enabled by
6167
# default but not required if you're only parsing a file, for example, as
6268
# opposed to validating all of its contents.
63-
validate = [
64-
'dep:indexmap',
65-
'dep:semver',
66-
'dep:hashbrown',
67-
'dep:ahash',
68-
]
69+
validate = ['dep:semver']
6970

7071
# Enable Serialize/Deserialize implementations for types in
7172
# `wasmparser::collections`

crates/wasmparser/src/collections/index_map.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,7 @@ where
109109
/// Reserves capacity for at least `additional` more elements to be inserted in the [`IndexMap`].
110110
#[inline]
111111
pub fn reserve(&mut self, additional: usize) {
112-
#[cfg(not(feature = "no-hash-maps"))]
113112
self.inner.reserve(additional);
114-
#[cfg(feature = "no-hash-maps")]
115-
let _ = additional;
116113
}
117114

118115
/// Returns true if `key` is contains in the [`IndexMap`].

crates/wasmparser/src/collections/index_map/detail.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! An ordered map based on a B-Tree that keeps insertion order of elements.
22
3-
#[cfg(not(feature = "no-hash-maps"))]
3+
#[cfg(all(
4+
feature = "hash-collections",
5+
not(feature = "prefer-btree-collections")
6+
))]
47
mod impls {
58
use crate::collections::hash;
69
use indexmap::IndexMap;
@@ -17,7 +20,10 @@ mod impls {
1720
pub type ValuesMutImpl<'a, K, V> = indexmap::map::ValuesMut<'a, K, V>;
1821
}
1922

20-
#[cfg(feature = "no-hash-maps")]
23+
#[cfg(any(
24+
not(feature = "hash-collections"),
25+
feature = "prefer-btree-collections"
26+
))]
2127
mod impls {
2228
pub type IndexMapImpl<K, V> = super::IndexMap<K, V>;
2329
pub type EntryImpl<'a, K, V> = super::Entry<'a, K, V>;

crates/wasmparser/src/collections/map.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
use core::fmt::Debug;
44
use core::{borrow::Borrow, hash::Hash, iter::FusedIterator, ops::Index};
55

6-
#[cfg(not(feature = "no-hash-maps"))]
6+
#[cfg(all(
7+
feature = "hash-collections",
8+
not(feature = "prefer-btree-collections")
9+
))]
710
mod detail {
811
use crate::collections::hash;
912
use hashbrown::hash_map;
@@ -22,7 +25,10 @@ mod detail {
2225
pub type IntoValuesImpl<K, V> = hash_map::IntoValues<K, V>;
2326
}
2427

25-
#[cfg(feature = "no-hash-maps")]
28+
#[cfg(any(
29+
not(feature = "hash-collections"),
30+
feature = "prefer-btree-collections"
31+
))]
2632
mod detail {
2733
use alloc::collections::btree_map;
2834

@@ -155,9 +161,15 @@ where
155161
/// Reserves capacity for at least `additional` more elements to be inserted in the [`Map`].
156162
#[inline]
157163
pub fn reserve(&mut self, additional: usize) {
158-
#[cfg(not(feature = "no-hash-maps"))]
164+
#[cfg(all(
165+
feature = "hash-collections",
166+
not(feature = "prefer-btree-collections")
167+
))]
159168
self.inner.reserve(additional);
160-
#[cfg(feature = "no-hash-maps")]
169+
#[cfg(any(
170+
not(feature = "hash-collections"),
171+
feature = "prefer-btree-collections"
172+
))]
161173
let _ = additional;
162174
}
163175

crates/wasmparser/src/collections/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
//! [`BTreeMap`]: alloc::collections::BTreeMap
1515
//! [`BTreeSet`]: alloc::collections::BTreeSet
1616
17+
// Which collections will be used feature matrix:
18+
//
19+
// `hash-collections` | `prefer-btree-collections` | usage
20+
// ------------------ | -------------------------- | -------------------
21+
// false | false | btree
22+
// true | false | hash
23+
// false | true | btree
24+
// true | true | btree
25+
26+
#[cfg(feature = "hash-collections")]
1727
pub mod hash;
1828
pub mod index_map;
1929
pub mod index_set;

crates/wasmparser/src/collections/set.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use core::{
88
ops::{BitAnd, BitOr, BitXor, Sub},
99
};
1010

11-
#[cfg(not(feature = "no-hash-maps"))]
11+
#[cfg(all(
12+
feature = "hash-collections",
13+
not(feature = "prefer-btree-collections")
14+
))]
1215
mod detail {
1316
use crate::collections::hash;
1417
use hashbrown::hash_set;
@@ -23,7 +26,10 @@ mod detail {
2326
pub type UnionImpl<'a, T> = hash_set::Union<'a, T, hash::RandomState>;
2427
}
2528

26-
#[cfg(feature = "no-hash-maps")]
29+
#[cfg(any(
30+
not(feature = "hash-collections"),
31+
feature = "prefer-btree-collections"
32+
))]
2733
mod detail {
2834
use alloc::collections::btree_set;
2935

@@ -105,9 +111,15 @@ where
105111
/// Reserves capacity for at least `additional` more elements to be inserted in the [`Set`].
106112
#[inline]
107113
pub fn reserve(&mut self, additional: usize) {
108-
#[cfg(not(feature = "no-hash-maps"))]
114+
#[cfg(all(
115+
feature = "hash-collections",
116+
not(feature = "prefer-btree-collections")
117+
))]
109118
self.inner.reserve(additional);
110-
#[cfg(feature = "no-hash-maps")]
119+
#[cfg(any(
120+
not(feature = "hash-collections"),
121+
feature = "prefer-btree-collections"
122+
))]
111123
let _ = additional;
112124
}
113125

crates/wasmparser/src/validator/component.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1697,7 +1697,6 @@ impl ComponentState {
16971697
}
16981698

16991699
let mut set = Set::default();
1700-
#[cfg(not(feature = "no-hash-maps"))] // TODO: remove when unified map type is available
17011700
set.reserve(core::cmp::max(ty.params.len(), ty.results.type_count()));
17021701

17031702
let params = ty

0 commit comments

Comments
 (0)