A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Rename our OOM-handling `SecondaryMap` to `TrySecondaryMap` (#12727)
Nick Fitzgeraldcd2cba7a3901
diff --git a/crates/environ/src/collections.rs b/crates/environ/src/collections.rsindex f61d970d97..c477563265 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.rsindex 4b5733c544..a4d909a7f9 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<K, V>+pub struct TrySecondaryMap<K, V>whereK: EntityRef,V: Clone,@@ -11,7 +11,7 @@ whereinner: Inner<K, V>,}-impl<K, V> fmt::Debug for SecondaryMap<K, V>+impl<K, V> fmt::Debug for TrySecondaryMap<K, V>whereK: EntityRef + fmt::Debug,V: fmt::Debug + Clone,@@ -21,7 +21,7 @@ where}}-impl<K, V> SecondaryMap<K, V>+impl<K, V> TrySecondaryMap<K, V>whereK: EntityRef,V: Clone,@@ -125,19 +125,19 @@ where}}-impl<K, V> Default for SecondaryMap<K, V>+impl<K, V> Default for TrySecondaryMap<K, V>whereK: EntityRef,V: Clone + Default,{- fn default() -> SecondaryMap<K, V> {- SecondaryMap::new()+ fn default() -> TrySecondaryMap<K, V> {+ TrySecondaryMap::new()}}// NB: no `IndexMut` implementation because it requires allocation but the trait// doesn't allow for fallibility.-impl<K, V> Index<K> for SecondaryMap<K, V>+impl<K, V> Index<K> for TrySecondaryMap<K, V>whereK: EntityRef,V: Clone,@@ -149,7 +149,7 @@ where}}-impl<K, V> From<TryVec<V>> for SecondaryMap<K, V>+impl<K, V> From<TryVec<V>> for TrySecondaryMap<K, V>whereK: EntityRef,V: Clone + Default,@@ -161,7 +161,7 @@ where}}-impl<K, V> serde::ser::Serialize for SecondaryMap<K, V>+impl<K, V> serde::ser::Serialize for TrySecondaryMap<K, V>whereK: EntityRef,V: Clone + PartialEq + serde::ser::Serialize,@@ -197,7 +197,7 @@ where}}-impl<'de, K, V> serde::de::Deserialize<'de> for SecondaryMap<K, V>+impl<'de, K, V> serde::de::Deserialize<'de> for TrySecondaryMap<K, V>whereK: EntityRef,V: Clone + serde::de::Deserialize<'de>,@@ -206,7 +206,7 @@ wherewhereD: serde::Deserializer<'de>,{- struct Visitor<K, V>(core::marker::PhantomData<fn() -> SecondaryMap<K, V>>)+ struct Visitor<K, V>(core::marker::PhantomData<fn() -> TrySecondaryMap<K, V>>)whereK: EntityRef,V: Clone;@@ -216,7 +216,7 @@ whereK: EntityRef,V: Clone + serde::de::Deserialize<'de>,{- type Value = SecondaryMap<K, V>;+ type Value = TrySecondaryMap<K, V>;fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {f.write_str("struct SecondaryMap")@@ -234,7 +234,7 @@ wherereturn Err(serde::de::Error::custom("Default value required"));};- let mut map = SecondaryMap::<K, V>::with_default(default.clone());+ let mut map = TrySecondaryMap::<K, V>::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::<K, u32>::with_default(99);+ let mut map = TrySecondaryMap::<K, u32>::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<K, u32> = postcard::from_bytes(&bytes)?;+ let map2: TrySecondaryMap<K, u32> = 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.rsindex e99a5cd45e..79af3abde9 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<ModuleInternedRecGroupIndex, Range<ModuleInternedTypeIndex>>,wasm_types: TryPrimaryMap<ModuleInternedTypeIndex, WasmSubType>,- trampoline_types: SecondaryMap<ModuleInternedTypeIndex, PackedOption<ModuleInternedTypeIndex>>,+ trampoline_types:+ TrySecondaryMap<ModuleInternedTypeIndex, PackedOption<ModuleInternedTypeIndex>>,}impl TypeTrace for ModuleTypes {diff --git a/crates/environ/src/prelude.rs b/crates/environ/src/prelude.rsindex 51348e7e29..02d68966e3 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.rsindex b2a3737595..3149987922 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<RecGroupEntry>,types: TryPrimaryMap<ModuleInternedTypeIndex, VMSharedTypeIndex>,- trampolines: SecondaryMap<VMSharedTypeIndex, PackedOption<ModuleInternedTypeIndex>>,+ trampolines: TrySecondaryMap<VMSharedTypeIndex, PackedOption<ModuleInternedTypeIndex>>,}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<VMSharedTypeIndex, Option<RecGroupEntry>>,+ type_to_rec_group: TrySecondaryMap<VMSharedTypeIndex, Option<RecGroupEntry>>,// 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<VMSharedTypeIndex, Option<Box<[VMSharedTypeIndex]>>>,+ type_to_supertypes: TrySecondaryMap<VMSharedTypeIndex, Option<Box<[VMSharedTypeIndex]>>>,// 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<VMSharedTypeIndex, PackedOption<VMSharedTypeIndex>>,+ type_to_trampoline: TrySecondaryMap<VMSharedTypeIndex, PackedOption<VMSharedTypeIndex>>,// 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<VMSharedTypeIndex, Option<GcLayout>>,+ type_to_gc_layout: TrySecondaryMap<VMSharedTypeIndex, Option<GcLayout>>,// 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