From ec1f692f151bb3c00060aaa37198d702dba4ed7e Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Wed, 4 Mar 2026 15:44:31 -0800 Subject: [PATCH] Rename our OOM-handling `PrimaryMap` to `TryPrimaryMap` --- crates/environ/src/collections.rs | 2 +- crates/environ/src/collections/primary_map.rs | 26 ++++++++-------- crates/environ/src/compile/module_environ.rs | 4 +-- crates/environ/src/module.rs | 18 +++++------ crates/environ/src/module_artifacts.rs | 30 +++++++++---------- crates/environ/src/module_types.rs | 7 ++--- crates/environ/src/prelude.rs | 2 +- crates/wasmtime/src/runtime/store.rs | 4 +-- crates/wasmtime/src/runtime/type_registry.rs | 20 ++++++------- 9 files changed, 56 insertions(+), 57 deletions(-) diff --git a/crates/environ/src/collections.rs b/crates/environ/src/collections.rs index 5a3e9a838420..f61d970d9788 100644 --- a/crates/environ/src/collections.rs +++ b/crates/environ/src/collections.rs @@ -13,7 +13,7 @@ pub use entity_set::TryEntitySet; pub use hash_map::TryHashMap; pub use hash_set::TryHashSet; pub use index_map::TryIndexMap; -pub use primary_map::PrimaryMap; +pub use primary_map::TryPrimaryMap; pub use secondary_map::SecondaryMap; pub use wasmtime_core::{ alloc::{ diff --git a/crates/environ/src/collections/primary_map.rs b/crates/environ/src/collections/primary_map.rs index 27b7ac666c5f..a44dd4b8b718 100644 --- a/crates/environ/src/collections/primary_map.rs +++ b/crates/environ/src/collections/primary_map.rs @@ -10,14 +10,14 @@ use serde::{Serialize, ser::SerializeSeq}; /// Like [`cranelift_entity::PrimaryMap`] but enforces fallible allocation for /// all methods that allocate. #[derive(Hash, PartialEq, Eq)] -pub struct PrimaryMap +pub struct TryPrimaryMap where K: EntityRef, { inner: cranelift_entity::PrimaryMap, } -impl Default for PrimaryMap +impl Default for TryPrimaryMap where K: EntityRef, { @@ -28,7 +28,7 @@ where } } -impl fmt::Debug for PrimaryMap +impl fmt::Debug for TryPrimaryMap where K: EntityRef + fmt::Debug, V: fmt::Debug, @@ -38,7 +38,7 @@ where } } -impl From> for PrimaryMap +impl From> for TryPrimaryMap where K: EntityRef, { @@ -49,7 +49,7 @@ where } } -impl serde::ser::Serialize for PrimaryMap +impl serde::ser::Serialize for TryPrimaryMap where K: EntityRef, V: Serialize, @@ -66,7 +66,7 @@ where } } -impl<'de, K, V> serde::de::Deserialize<'de> for PrimaryMap +impl<'de, K, V> serde::de::Deserialize<'de> for TryPrimaryMap where K: EntityRef, V: serde::de::Deserialize<'de>, @@ -80,7 +80,7 @@ where } } -impl PrimaryMap +impl TryPrimaryMap where K: EntityRef, { @@ -218,7 +218,7 @@ where } } -impl TryExtend for PrimaryMap +impl TryExtend for TryPrimaryMap where K: EntityRef, { @@ -237,7 +237,7 @@ where } } -impl Index for PrimaryMap +impl Index for TryPrimaryMap where K: EntityRef, { @@ -248,7 +248,7 @@ where } } -impl IndexMut for PrimaryMap +impl IndexMut for TryPrimaryMap where K: EntityRef, { @@ -257,7 +257,7 @@ where } } -impl IntoIterator for PrimaryMap +impl IntoIterator for TryPrimaryMap where K: EntityRef, { @@ -268,7 +268,7 @@ where } } -impl<'a, K, V> IntoIterator for &'a PrimaryMap +impl<'a, K, V> IntoIterator for &'a TryPrimaryMap where K: EntityRef, { @@ -279,7 +279,7 @@ where } } -impl<'a, K, V> IntoIterator for &'a mut PrimaryMap +impl<'a, K, V> IntoIterator for &'a mut TryPrimaryMap where K: EntityRef, { diff --git a/crates/environ/src/compile/module_environ.rs b/crates/environ/src/compile/module_environ.rs index b328275206bb..2ecacbb21d71 100644 --- a/crates/environ/src/compile/module_environ.rs +++ b/crates/environ/src/compile/module_environ.rs @@ -10,7 +10,7 @@ use crate::{ ModuleInternedTypeIndex, ModuleTypesBuilder, PanicOnOom as _, PrimaryMap, SizeOverflow, StaticMemoryInitializer, StaticModuleIndex, TableIndex, TableInitialValue, Tag, TagIndex, Tunables, TypeConvert, TypeIndex, WasmError, WasmHeapTopType, WasmHeapType, WasmResult, - WasmValType, WasmparserTypeConverter, collections, + WasmValType, WasmparserTypeConverter, }; use cranelift_entity::SecondaryMap; use cranelift_entity::packed_option::ReservedValue; @@ -1107,7 +1107,7 @@ impl ModuleTranslation<'_> { // memory initialization image is built here from the page data and then // it's converted to a single initializer. let data = mem::replace(&mut self.data, Vec::new()); - let mut map = collections::PrimaryMap::with_capacity(info.len()).panic_on_oom(); + let mut map = TryPrimaryMap::with_capacity(info.len()).panic_on_oom(); let mut module_data_size = 0u32; for (memory, info) in info.iter() { // Create the in-memory `image` which is the initialized contents of diff --git a/crates/environ/src/module.rs b/crates/environ/src/module.rs index f983aa304d01..c2f52c2f60a5 100644 --- a/crates/environ/src/module.rs +++ b/crates/environ/src/module.rs @@ -82,7 +82,7 @@ pub enum MemoryInitialization { /// /// The offset, range base, and range end are all guaranteed to be page /// aligned to the page size passed in to `try_static_init`. - map: collections::PrimaryMap>, + map: TryPrimaryMap>, }, } @@ -226,7 +226,7 @@ pub struct TableInitialization { /// initialization. For example table initializers to a table that are all /// in-bounds will get removed from `segment` and moved into /// `initial_values` here. - pub initial_values: collections::PrimaryMap, + pub initial_values: TryPrimaryMap, /// Element segments present in the initial wasm module which are executed /// at instantiation time. @@ -325,7 +325,7 @@ pub struct Module { pub passive_data_map: BTreeMap>, /// Types declared in the wasm module. - pub types: collections::PrimaryMap, + pub types: TryPrimaryMap, /// Number of imported or aliased functions in the module. pub num_imported_funcs: usize, @@ -353,22 +353,22 @@ pub struct Module { pub num_escaped_funcs: usize, /// Types of functions, imported and local. - pub functions: collections::PrimaryMap, + pub functions: TryPrimaryMap, /// WebAssembly tables. - pub tables: collections::PrimaryMap, + pub tables: TryPrimaryMap, /// WebAssembly linear memory plans. - pub memories: collections::PrimaryMap, + pub memories: TryPrimaryMap, /// WebAssembly global variables. - pub globals: collections::PrimaryMap, + pub globals: TryPrimaryMap, /// WebAssembly global initializers for locally-defined globals. - pub global_initializers: collections::PrimaryMap, + pub global_initializers: TryPrimaryMap, /// WebAssembly exception and control tags. - pub tags: collections::PrimaryMap, + pub tags: TryPrimaryMap, } /// Initialization routines for creating an instance, encompassing imports, diff --git a/crates/environ/src/module_artifacts.rs b/crates/environ/src/module_artifacts.rs index 1632defc3de2..6d6960b1ea60 100644 --- a/crates/environ/src/module_artifacts.rs +++ b/crates/environ/src/module_artifacts.rs @@ -4,7 +4,7 @@ use crate::prelude::*; use crate::{ EntityRef, FilePos, FuncIndex, FuncKey, FuncKeyIndex, FuncKeyKind, FuncKeyNamespace, Module, - PanicOnOom as _, collections::PrimaryMap, + PanicOnOom as _, }; use core::ops::Range; use core::{fmt, u32}; @@ -78,13 +78,13 @@ impl CompiledFunctionsTableBuilder { pub fn new() -> Self { Self { inner: CompiledFunctionsTable { - namespaces: PrimaryMap::new(), - func_loc_starts: PrimaryMap::new(), - sparse_starts: PrimaryMap::new(), - src_loc_starts: PrimaryMap::new(), - sparse_indices: PrimaryMap::new(), - func_locs: PrimaryMap::new(), - src_locs: PrimaryMap::new(), + namespaces: TryPrimaryMap::new(), + func_loc_starts: TryPrimaryMap::new(), + sparse_starts: TryPrimaryMap::new(), + src_loc_starts: TryPrimaryMap::new(), + sparse_indices: TryPrimaryMap::new(), + func_locs: TryPrimaryMap::new(), + src_locs: TryPrimaryMap::new(), }, } } @@ -289,7 +289,7 @@ pub struct CompiledFunctionsTable { /// their associated `NamespaceIndex`. That `NamespaceIndex` can then be /// used to find the range of other entity indices that are specific to that /// namespace. - namespaces: PrimaryMap, + namespaces: TryPrimaryMap, /// `self.func_loc_starts[i]..self.func_loc_starts[i+1]` describes the range /// within `self.func_locs` whose entries are associated with the namespace @@ -297,7 +297,7 @@ pub struct CompiledFunctionsTable { /// /// When `self.func_loc_starts[i+1]` is out of bounds, then the range is to /// the end of `self.func_locs`. - func_loc_starts: PrimaryMap, + func_loc_starts: TryPrimaryMap, /// `self.sparse_starts[i]..self.sparse_starts[i+1]` describes the range /// within `self.sparse_indices` whose entries are associated with the @@ -307,7 +307,7 @@ pub struct CompiledFunctionsTable { /// the end of `self.sparse_indices`. /// /// Entries are only valid for sparse, non-dense namespaces. - sparse_starts: PrimaryMap, + sparse_starts: TryPrimaryMap, /// `self.src_loc_starts[i]..self.src_loc_starts[i+1]` describes the range /// within `self.src_loc_indices` whose entries are associated with the @@ -318,7 +318,7 @@ pub struct CompiledFunctionsTable { /// /// Entries are only valid for namespaces whose functions have source /// locations. - src_loc_starts: PrimaryMap, + src_loc_starts: TryPrimaryMap, /// `self.sparse_indices[i]` contains the index part of /// `FuncKey::from_parts(ns, index)` where `ns` is determined by @@ -327,7 +327,7 @@ pub struct CompiledFunctionsTable { /// the namespace's start index.) /// /// This is sorted to allow for binary searches. - sparse_indices: PrimaryMap, + sparse_indices: TryPrimaryMap, /// `self.func_locs[i]` contains the location within the text section of /// `FuncKey::from_parts(self.namespaces[ns], i - start)`'s function, where @@ -338,14 +338,14 @@ pub struct CompiledFunctionsTable { /// /// The absence of a function location (for gaps in dense namespaces) is /// represented with `FunctionLoc::none()`. - func_locs: PrimaryMap, + func_locs: TryPrimaryMap, /// `self.src_locs[i]` contains the initial source location of /// `FuncKey::from_parts(self.namespaces[ns], i - start)`'s function, where /// `ns` and `start` are determined by `self.src_loc_starts`. /// /// The absence of a source location is represented by `FilePos::none()`. - src_locs: PrimaryMap, + src_locs: TryPrimaryMap, } impl CompiledFunctionsTable { diff --git a/crates/environ/src/module_types.rs b/crates/environ/src/module_types.rs index 0da4fb721d59..e99a5cd45ee7 100644 --- a/crates/environ/src/module_types.rs +++ b/crates/environ/src/module_types.rs @@ -1,7 +1,6 @@ use crate::{ ModuleInternedRecGroupIndex, ModuleInternedTypeIndex, PanicOnOom as _, TypeTrace, WasmSubType, - collections::{PrimaryMap, SecondaryMap}, - packed_option::PackedOption, + collections::SecondaryMap, packed_option::PackedOption, prelude::*, }; use core::ops::{Index, Range}; use serde_derive::{Deserialize, Serialize}; @@ -12,8 +11,8 @@ use serde_derive::{Deserialize, Serialize}; /// implementations for this type. #[derive(Default, Serialize, Deserialize)] pub struct ModuleTypes { - rec_groups: PrimaryMap>, - wasm_types: PrimaryMap, + rec_groups: TryPrimaryMap>, + wasm_types: TryPrimaryMap, trampoline_types: SecondaryMap>, } diff --git a/crates/environ/src/prelude.rs b/crates/environ/src/prelude.rs index 8c5a3bfb2339..51348e7e295e 100644 --- a/crates/environ/src/prelude.rs +++ b/crates/environ/src/prelude.rs @@ -21,7 +21,7 @@ pub use crate::collections::{ TryClone, TryCollect, TryEntitySet, TryExtend, TryFromIterator, TryHashMap, TryHashSet, - TryIndexMap, TryNew, TryString, TryToOwned, TryVec, try_new, try_vec, + TryIndexMap, TryNew, TryPrimaryMap, TryString, TryToOwned, TryVec, try_new, try_vec, }; pub use crate::error::{Context, Error, Result, bail, ensure, format_err}; pub use alloc::borrow::ToOwned; diff --git a/crates/wasmtime/src/runtime/store.rs b/crates/wasmtime/src/runtime/store.rs index 97e2fd5ba2ae..af2bf238b11c 100644 --- a/crates/wasmtime/src/runtime/store.rs +++ b/crates/wasmtime/src/runtime/store.rs @@ -473,7 +473,7 @@ pub struct StoreOpaque { #[cfg(feature = "stack-switching")] continuations: Vec>, - instances: wasmtime_environ::collections::PrimaryMap, + instances: TryPrimaryMap, signal_handler: Option, modules: ModuleRegistry, @@ -744,7 +744,7 @@ impl Store { vm_store_context: Default::default(), #[cfg(feature = "stack-switching")] continuations: Vec::new(), - instances: wasmtime_environ::collections::PrimaryMap::new(), + instances: TryPrimaryMap::new(), signal_handler: None, gc_store: None, gc_roots: RootSet::default(), diff --git a/crates/wasmtime/src/runtime/type_registry.rs b/crates/wasmtime/src/runtime/type_registry.rs index 50773fe86a3e..b2a37375951e 100644 --- a/crates/wasmtime/src/runtime/type_registry.rs +++ b/crates/wasmtime/src/runtime/type_registry.rs @@ -25,7 +25,7 @@ use wasmtime_core::slab::{Id as SlabId, Slab}; use wasmtime_environ::{ EngineOrModuleTypeIndex, EntityRef, GcLayout, ModuleInternedTypeIndex, ModuleTypes, TypeTrace, Undo, VMSharedTypeIndex, WasmRecGroup, WasmSubType, - collections::{PrimaryMap, SecondaryMap, TryCow}, + collections::{SecondaryMap, TryCow}, iter_entity_range, packed_option::{PackedOption, ReservedValue}, }; @@ -89,7 +89,7 @@ use wasmtime_environ::{ pub struct TypeCollection { engine: Engine, rec_groups: TryVec, - types: PrimaryMap, + types: TryPrimaryMap, trampolines: SecondaryMap>, } @@ -184,7 +184,7 @@ impl TypeCollection { /// /// This is used for looking up module shared type indexes during module /// instantiation. - pub fn as_module_map(&self) -> &PrimaryMap { + pub fn as_module_map(&self) -> &TryPrimaryMap { &self.types } @@ -682,7 +682,7 @@ impl TypeRegistryInner { ) -> Result< ( TryVec, - PrimaryMap, + TryPrimaryMap, ), OutOfMemory, > { @@ -693,7 +693,7 @@ impl TypeRegistryInner { // The map from a module type index to an engine type index for these // module types. - let mut map = PrimaryMap::::with_capacity( + let mut map = TryPrimaryMap::::with_capacity( types.wasm_types().len(), )?; @@ -751,7 +751,7 @@ impl TypeRegistryInner { fn register_rec_group( &mut self, gc_runtime: Option<&dyn GcRuntime>, - map: &PrimaryMap, + map: &TryPrimaryMap, range: Range, types: impl ExactSizeIterator>, ) -> Result { @@ -809,7 +809,7 @@ impl TypeRegistryInner { fn register_new_rec_group( &mut self, gc_runtime: Option<&(dyn GcRuntime + 'static)>, - map: &PrimaryMap, + map: &TryPrimaryMap, range: Range, hash_consing_key: WasmRecGroup, mut non_canon_types: TryVec<(ModuleInternedTypeIndex, WasmSubType)>, @@ -1258,7 +1258,7 @@ impl TypeRegistryInner { /// this registry. fn canonicalize_entry_types_for_runtime_usage<'a>( &self, - map: &PrimaryMap, + map: &TryPrimaryMap, entry: &RecGroupEntry, sub_tys: impl ExactSizeIterator, range: Range, @@ -1272,7 +1272,7 @@ impl TypeRegistryInner { /// Canonicalize one type for runtime usage within this registry. fn canonicalize_type_for_runtime_usage( &self, - map: &PrimaryMap, + map: &TryPrimaryMap, entry: &RecGroupEntry, engine_index: VMSharedTypeIndex, ty: &mut WasmSubType, @@ -1346,7 +1346,7 @@ impl TypeRegistryInner { // This type doesn't have any module-level type references, since it is // already canonicalized for runtime usage in this registry, so an empty // map suffices. - let map = PrimaryMap::default(); + let map = TryPrimaryMap::default(); // This must have `range.len() == 1`, even though we know this type // doesn't have any intra-group type references, to satisfy