Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/environ/src/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 15 additions & 15 deletions crates/environ/src/collections/secondary_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ 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>
where
K: EntityRef,
V: Clone,
{
inner: Inner<K, V>,
}

impl<K, V> fmt::Debug for SecondaryMap<K, V>
impl<K, V> fmt::Debug for TrySecondaryMap<K, V>
where
K: EntityRef + fmt::Debug,
V: fmt::Debug + Clone,
Expand All @@ -21,7 +21,7 @@ where
}
}

impl<K, V> SecondaryMap<K, V>
impl<K, V> TrySecondaryMap<K, V>
where
K: EntityRef,
V: Clone,
Expand Down Expand Up @@ -125,19 +125,19 @@ where
}
}

impl<K, V> Default for SecondaryMap<K, V>
impl<K, V> Default for TrySecondaryMap<K, V>
where
K: 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>
where
K: EntityRef,
V: Clone,
Expand All @@ -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>
where
K: EntityRef,
V: Clone + Default,
Expand All @@ -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>
where
K: EntityRef,
V: Clone + PartialEq + serde::ser::Serialize,
Expand Down Expand Up @@ -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>
where
K: EntityRef,
V: Clone + serde::de::Deserialize<'de>,
Expand All @@ -206,7 +206,7 @@ where
where
D: 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>>)
where
K: EntityRef,
V: Clone;
Expand All @@ -216,7 +216,7 @@ where
K: 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")
Expand All @@ -234,7 +234,7 @@ where
return 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))?;
Expand Down Expand Up @@ -279,15 +279,15 @@ 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)?;
map.insert(k(3), 99)?;
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)]);
Expand Down
5 changes: 3 additions & 2 deletions crates/environ/src/module_types.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion crates/environ/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions crates/wasmtime/src/runtime/type_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
//
Expand All @@ -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.
//
Expand All @@ -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
Expand Down