Skip to content

Commit

Permalink
Reexport wasmtime::hash_{map,set} from hashbrown internally
Browse files Browse the repository at this point in the history
Some modules were using `std::collections::{HashMap, HashSet}` when they knew
that they were only compiled when the `std` feature was enabled. But we need to
use `hashbrown` in certain other places (Cranelift's e-graphs, pooling
allocator's index allocator, etc.), so we should simply use `hashbrown`
everywhere. This should reduce binary size some small amount, but also means
that we never have to ask ourselves "which hash map should I import?" when
adding a new hash map import: always `use crate::hash_map::HashMap` et al.
  • Loading branch information
fitzgen committed Sep 28, 2024
1 parent 5c8cd22 commit b99f788
Show file tree
Hide file tree
Showing 12 changed files with 19 additions and 12 deletions.
4 changes: 3 additions & 1 deletion crates/wasmtime/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
//! functions. It is up to the caller to serialize the relevant parts of the
//! `Artifacts` into the ELF file.

use crate::hash_map::HashMap;
use crate::hash_set::HashSet;
use crate::prelude::*;
use crate::Engine;
use std::{
any::Any,
borrow::Cow,
collections::{btree_map, BTreeMap, BTreeSet, HashMap, HashSet},
collections::{btree_map, BTreeMap, BTreeSet},
mem,
};

Expand Down
3 changes: 2 additions & 1 deletion crates/wasmtime/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::hash_map::HashMap;
use crate::hash_set::HashSet;
use crate::prelude::*;
use alloc::sync::Arc;
use bitflags::Flags;
use core::fmt;
use core::str::FromStr;
use hashbrown::{HashMap, HashSet};
use serde_derive::{Deserialize, Serialize};
#[cfg(any(feature = "cache", feature = "cranelift", feature = "winch"))]
use std::path::Path;
Expand Down
2 changes: 2 additions & 0 deletions crates/wasmtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ pub(crate) mod prelude {
pub use wasmtime_environ::prelude::*;
}

pub(crate) use hashbrown::{hash_map, hash_set};

/// A helper macro to safely map `MaybeUninit<T>` to `MaybeUninit<U>` where `U`
/// is a field projection within `T`.
///
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmtime/src/runtime/component/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use crate::component::types;
use crate::component::{
Component, ComponentNamedList, Instance, InstancePre, Lift, Lower, ResourceType, Val,
};
use crate::hash_map::HashMap;
use crate::prelude::*;
use crate::{AsContextMut, Engine, Module, StoreContextMut};
use alloc::sync::Arc;
use core::future::Future;
use core::marker;
use core::pin::Pin;
use hashbrown::hash_map::HashMap;
use wasmtime_environ::component::{NameMap, NameMapIntern};
use wasmtime_environ::PrimaryMap;

Expand Down
2 changes: 1 addition & 1 deletion crates/wasmtime/src/runtime/component/resource_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ impl ResourceTable {
#[cfg(feature = "std")]
pub fn iter_entries<'a, T>(
&'a mut self,
map: std::collections::HashMap<u32, T>,
map: crate::hash_map::HashMap<u32, T>,
) -> impl Iterator<Item = (Result<&'a mut dyn Any, ResourceTableError>, T)> {
map.into_iter().map(move |(k, v)| {
let item = self
Expand Down
3 changes: 2 additions & 1 deletion crates/wasmtime/src/runtime/coredump.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::hash_map::HashMap;
use crate::prelude::*;
use crate::{
store::StoreOpaque, AsContextMut, FrameInfo, Global, HeapType, Instance, Memory, Module,
StoreContextMut, Val, ValType, WasmBacktrace,
};
use std::{collections::HashMap, fmt};
use std::fmt;

/// Representation of a core dump of a WebAssembly module
///
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmtime/src/runtime/linker.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::func::HostFunc;
use crate::hash_map::{Entry, HashMap};
use crate::instance::InstancePre;
use crate::store::StoreOpaque;
use crate::{prelude::*, IntoFunc};
Expand All @@ -13,7 +14,6 @@ use core::future::Future;
use core::marker;
#[cfg(feature = "async")]
use core::pin::Pin;
use hashbrown::hash_map::{Entry, HashMap};
use log::warn;

/// Structure used to link wasm modules/instances together.
Expand Down
5 changes: 3 additions & 2 deletions crates/wasmtime/src/runtime/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
//! contents of `StoreOpaque`. This is an invariant that we, as the authors of
//! `wasmtime`, must uphold for the public interface to be safe.

use crate::hash_set::HashSet;
use crate::instance::InstanceData;
use crate::linker::Definition;
use crate::module::RegisteredModuleId;
Expand Down Expand Up @@ -324,7 +325,7 @@ pub struct StoreOpaque {
gc_roots: RootSet,
gc_roots_list: GcRootsList,
// Types for which the embedder has created an allocator for.
gc_host_alloc_types: hashbrown::HashSet<RegisteredType>,
gc_host_alloc_types: HashSet<RegisteredType>,

// Numbers of resources instantiated in this store, and their limits
instance_count: usize,
Expand Down Expand Up @@ -542,7 +543,7 @@ impl<T> Store<T> {
gc_store: None,
gc_roots: RootSet::default(),
gc_roots_list: GcRootsList::default(),
gc_host_alloc_types: hashbrown::HashSet::default(),
gc_host_alloc_types: HashSet::default(),
modules: ModuleRegistry::default(),
func_refs: FuncRefs::default(),
host_globals: Vec::new(),
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmtime/src/runtime/type_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! Helps implement fast indirect call signature checking, reference type
//! downcasting, and etc...

use crate::hash_set::HashSet;
use crate::prelude::*;
use crate::sync::RwLock;
use crate::vm::GcRuntime;
Expand All @@ -20,7 +21,6 @@ use core::{
Ordering::{AcqRel, Acquire},
},
};
use hashbrown::HashSet;
use wasmtime_environ::{
iter_entity_range, packed_option::PackedOption, EngineOrModuleTypeIndex, GcLayout,
ModuleInternedTypeIndex, ModuleTypes, PrimaryMap, SecondaryMap, TypeTrace, VMSharedTypeIndex,
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmtime/src/runtime/vm/gc/enabled/drc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

use super::free_list::FreeList;
use super::{VMArrayRef, VMGcObjectDataMut, VMStructRef};
use crate::hash_set::HashSet;
use crate::prelude::*;
use crate::runtime::vm::{
ExternRefHostDataId, ExternRefHostDataTable, GarbageCollection, GcHeap, GcHeapObject,
Expand All @@ -57,7 +58,6 @@ use core::{
num::NonZeroUsize,
ptr::{self, NonNull},
};
use hashbrown::HashSet;
use wasmtime_environ::drc::DrcTypeLayouts;
use wasmtime_environ::{GcArrayLayout, GcStructLayout, GcTypeLayouts, VMGcKind, VMSharedTypeIndex};

Expand Down
2 changes: 1 addition & 1 deletion crates/wasmtime/src/runtime/vm/gc/enabled/free_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ fn round_usize_down_to_pow2(value: usize, divisor: usize) -> usize {
mod tests {
use super::*;
use proptest::prelude::*;
use std::{collections::HashMap, num::NonZeroUsize};
use std::num::NonZeroUsize;

fn free_list_block_len_and_size(free_list: &FreeList) -> (usize, Option<usize>) {
let len = free_list.free_block_index_to_len.len();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Index/slot allocator policies for the pooling allocator.

use crate::hash_map::{Entry, HashMap};
use crate::prelude::*;
use crate::runtime::vm::CompiledModuleId;
use std::collections::hash_map::{Entry, HashMap};
use std::mem;
use std::sync::Mutex;
use wasmtime_environ::DefinedMemoryIndex;
Expand Down

0 comments on commit b99f788

Please sign in to comment.