From cbd184ad1e66c941f58f949494da62a399f8855c Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Wed, 4 Mar 2026 15:47:35 -0800 Subject: [PATCH] Rename our OOM-handling `SecondaryMap` to `TrySecondaryMap` --- crates/environ/src/collections.rs | 2 +- .../environ/src/collections/secondary_map.rs | 30 +++++++++---------- crates/environ/src/module_types.rs | 5 ++-- crates/environ/src/prelude.rs | 3 +- crates/wasmtime/src/runtime/type_registry.rs | 14 ++++----- 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/crates/environ/src/collections.rs b/crates/environ/src/collections.rs index f61d970d9788..c4775632652b 100644 --- a/crates/environ/src/collections.rs +++ b/crates/environ/src/collections.rs @@ -14,7 +14,7 @@ pub use hash_map::TryHashMap; pub use hash_set::TryHashSet; pub use index_map::TryIndexMap; pub use primary_map::TryPrimaryMap; -pub use secondary_map::SecondaryMap; +pub use secondary_map::TrySecondaryMap; pub use wasmtime_core::{ alloc::{ TryClone, TryCollect, TryCow, TryExtend, TryFromIterator, TryNew, TryString, TryToOwned, diff --git a/crates/environ/src/collections/secondary_map.rs b/crates/environ/src/collections/secondary_map.rs index 4b5733c54445..a4d909a7f900 100644 --- a/crates/environ/src/collections/secondary_map.rs +++ b/crates/environ/src/collections/secondary_map.rs @@ -3,7 +3,7 @@ use core::{fmt, ops::Index}; use cranelift_entity::{EntityRef, SecondaryMap as Inner}; /// Like [`cranelift_entity::SecondaryMap`] but all allocation is fallible. -pub struct SecondaryMap +pub struct TrySecondaryMap where K: EntityRef, V: Clone, @@ -11,7 +11,7 @@ where inner: Inner, } -impl fmt::Debug for SecondaryMap +impl fmt::Debug for TrySecondaryMap where K: EntityRef + fmt::Debug, V: fmt::Debug + Clone, @@ -21,7 +21,7 @@ where } } -impl SecondaryMap +impl TrySecondaryMap where K: EntityRef, V: Clone, @@ -125,19 +125,19 @@ where } } -impl Default for SecondaryMap +impl Default for TrySecondaryMap where K: EntityRef, V: Clone + Default, { - fn default() -> SecondaryMap { - SecondaryMap::new() + fn default() -> TrySecondaryMap { + TrySecondaryMap::new() } } // NB: no `IndexMut` implementation because it requires allocation but the trait // doesn't allow for fallibility. -impl Index for SecondaryMap +impl Index for TrySecondaryMap where K: EntityRef, V: Clone, @@ -149,7 +149,7 @@ where } } -impl From> for SecondaryMap +impl From> for TrySecondaryMap where K: EntityRef, V: Clone + Default, @@ -161,7 +161,7 @@ where } } -impl serde::ser::Serialize for SecondaryMap +impl serde::ser::Serialize for TrySecondaryMap where K: EntityRef, V: Clone + PartialEq + serde::ser::Serialize, @@ -197,7 +197,7 @@ where } } -impl<'de, K, V> serde::de::Deserialize<'de> for SecondaryMap +impl<'de, K, V> serde::de::Deserialize<'de> for TrySecondaryMap where K: EntityRef, V: Clone + serde::de::Deserialize<'de>, @@ -206,7 +206,7 @@ where where D: serde::Deserializer<'de>, { - struct Visitor(core::marker::PhantomData SecondaryMap>) + struct Visitor(core::marker::PhantomData TrySecondaryMap>) where K: EntityRef, V: Clone; @@ -216,7 +216,7 @@ where K: EntityRef, V: Clone + serde::de::Deserialize<'de>, { - type Value = SecondaryMap; + type Value = TrySecondaryMap; fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str("struct SecondaryMap") @@ -234,7 +234,7 @@ where return Err(serde::de::Error::custom("Default value required")); }; - let mut map = SecondaryMap::::with_default(default.clone()); + let mut map = TrySecondaryMap::::with_default(default.clone()); if let Some(n) = size_hint { map.resize(n).map_err(|oom| serde::de::Error::custom(oom))?; @@ -279,7 +279,7 @@ mod tests { #[test] fn serialize_deserialize() -> Result<()> { - let mut map = SecondaryMap::::with_default(99); + let mut map = TrySecondaryMap::::with_default(99); map.insert(k(0), 33)?; map.insert(k(1), 44)?; map.insert(k(2), 55)?; @@ -287,7 +287,7 @@ mod tests { map.insert(k(4), 99)?; let bytes = postcard::to_allocvec(&map)?; - let map2: SecondaryMap = postcard::from_bytes(&bytes)?; + let map2: TrySecondaryMap = postcard::from_bytes(&bytes)?; for i in 0..10 { assert_eq!(map[k(i)], map2[k(i)]); diff --git a/crates/environ/src/module_types.rs b/crates/environ/src/module_types.rs index e99a5cd45ee7..79af3abde9bb 100644 --- a/crates/environ/src/module_types.rs +++ b/crates/environ/src/module_types.rs @@ -1,6 +1,6 @@ use crate::{ ModuleInternedRecGroupIndex, ModuleInternedTypeIndex, PanicOnOom as _, TypeTrace, WasmSubType, - collections::SecondaryMap, packed_option::PackedOption, prelude::*, + packed_option::PackedOption, prelude::*, }; use core::ops::{Index, Range}; use serde_derive::{Deserialize, Serialize}; @@ -13,7 +13,8 @@ use serde_derive::{Deserialize, Serialize}; pub struct ModuleTypes { rec_groups: TryPrimaryMap>, wasm_types: TryPrimaryMap, - trampoline_types: SecondaryMap>, + trampoline_types: + TrySecondaryMap>, } impl TypeTrace for ModuleTypes { diff --git a/crates/environ/src/prelude.rs b/crates/environ/src/prelude.rs index 51348e7e295e..02d68966e3bf 100644 --- a/crates/environ/src/prelude.rs +++ b/crates/environ/src/prelude.rs @@ -21,7 +21,8 @@ pub use crate::collections::{ TryClone, TryCollect, TryEntitySet, TryExtend, TryFromIterator, TryHashMap, TryHashSet, - TryIndexMap, TryNew, TryPrimaryMap, TryString, TryToOwned, TryVec, try_new, try_vec, + TryIndexMap, TryNew, TryPrimaryMap, TrySecondaryMap, 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/type_registry.rs b/crates/wasmtime/src/runtime/type_registry.rs index b2a37375951e..3149987922f3 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::{SecondaryMap, TryCow}, + collections::TryCow, iter_entity_range, packed_option::{PackedOption, ReservedValue}, }; @@ -90,7 +90,7 @@ pub struct TypeCollection { engine: Engine, rec_groups: TryVec, types: TryPrimaryMap, - trampolines: SecondaryMap>, + trampolines: TrySecondaryMap>, } impl Debug for TypeCollection { @@ -148,7 +148,7 @@ impl Engine { // module-index in a compiled module, and doing this engine-to-module // resolution now means we don't need to do it on the function call hot // path. - let mut trampolines = SecondaryMap::with_capacity(types.len())?; + let mut trampolines = TrySecondaryMap::with_capacity(types.len())?; for (module_ty, module_trampoline_ty) in module_types.trampoline_types() { let shared_ty = types[module_ty]; let trampoline_shared_ty = registry.trampoline_type(shared_ty); @@ -607,7 +607,7 @@ struct TypeRegistryInner { // A map that lets you walk backwards from a `VMSharedTypeIndex` to its // `RecGroupEntry`. - type_to_rec_group: SecondaryMap>, + type_to_rec_group: TrySecondaryMap>, // A map from a registered type to its complete list of supertypes. // @@ -618,7 +618,7 @@ struct TypeRegistryInner { // Types without any supertypes are omitted from this map. This means that // we never allocate any backing storage for this map when Wasm GC is not in // use. - type_to_supertypes: SecondaryMap>>, + type_to_supertypes: TrySecondaryMap>>, // A map from each registered function type to its trampoline type. // @@ -628,14 +628,14 @@ struct TypeRegistryInner { // backing storage for this map. As a nice bonus, this also avoids cycles (a // function type referencing itself) that our naive reference counting // doesn't play well with. - type_to_trampoline: SecondaryMap>, + type_to_trampoline: TrySecondaryMap>, // A map from each registered GC type to its layout. // // Function types do not have an entry in this map. Similar to the // `type_to_{supertypes,trampoline}` maps, we completely omit the `None` // entries for these types as a memory optimization. - type_to_gc_layout: SecondaryMap>, + type_to_gc_layout: TrySecondaryMap>, // An explicit stack of entries that we are in the middle of dropping. Used // to avoid recursion when dropping a type that is holding the last