Skip to content

Commit c13148b

Browse files
authored
Use fallible collection helpers in TypeRegistry (#12508)
* Use fallible collection helpers in `TypeRegistry` Merging together #12500 and #12505 of a sorts. * Review comments
1 parent 6c76af8 commit c13148b

2 files changed

Lines changed: 37 additions & 42 deletions

File tree

crates/environ/src/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ pub use alloc::string::{String, ToString};
2828
pub use alloc::vec;
2929
pub use alloc::vec::Vec;
3030
pub use wasmparser::collections::{IndexMap, IndexSet};
31+
pub use wasmtime_core::alloc::{TryCollect, TryExtend, TryFromIterator};

crates/wasmtime/src/runtime/type_registry.rs

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ use core::{
2323
Ordering::{AcqRel, Acquire, Release},
2424
},
2525
};
26-
use wasmtime_core::alloc::{
27-
boxed_slice_write_iter, new_boxed_slice_from_iter_with_len, new_uninit_boxed_slice,
28-
};
2926
use wasmtime_core::slab::{Id as SlabId, Slab};
3027
use wasmtime_environ::{
3128
EngineOrModuleTypeIndex, EntityRef, GcLayout, ModuleInternedTypeIndex, ModuleTypes, TypeTrace,
@@ -761,22 +758,19 @@ impl TypeRegistryInner {
761758
// potential engine canonicalization eventuality.
762759
let mut non_canon_types = Vec::with_capacity(types.len())?;
763760
let hash_consing_key = WasmRecGroup {
764-
types: new_boxed_slice_from_iter_with_len(
765-
types.len(),
766-
types
767-
.zip(iter_entity_range(range.clone()))
768-
.map(|(mut ty, module_index)| {
769-
non_canon_types
770-
.push((module_index, ty.clone()))
771-
.expect("reserved capacity");
772-
ty.canonicalize_for_hash_consing(range.clone(), &mut |idx| {
773-
debug_assert!(idx < range.clone().start);
774-
map[idx]
775-
});
776-
ty
777-
}),
778-
)
779-
.map_err(|e| e.unwrap_oom())?,
761+
types: types
762+
.zip(iter_entity_range(range.clone()))
763+
.map(|(mut ty, module_index)| {
764+
non_canon_types
765+
.push((module_index, ty.clone()))
766+
.expect("reserved capacity");
767+
ty.canonicalize_for_hash_consing(range.clone(), &mut |idx| {
768+
debug_assert!(idx < range.clone().start);
769+
map[idx]
770+
});
771+
ty
772+
})
773+
.try_collect()?,
780774
};
781775

782776
// Any references in the hash-consing key to types outside of this rec
@@ -819,7 +813,8 @@ impl TypeRegistryInner {
819813
// much as possible.
820814
let num_types = non_canon_types.len();
821815
self.reserve_capacity_for_rec_group(num_types)?;
822-
let shared_type_indices = new_uninit_boxed_slice(num_types)?;
816+
let mut shared_type_indices = Vec::new();
817+
shared_type_indices.reserve_exact(num_types)?;
823818
let entry_inner = RecGroupEntry::new_inner()?;
824819

825820
// Assign a `VMSharedTypeIndex` to each type.
@@ -966,14 +961,11 @@ impl TypeRegistryInner {
966961

967962
let supertype = supertype.unwrap_engine_type_index();
968963
let supers_supertypes = self.supertypes(supertype);
969-
let supertypes = new_boxed_slice_from_iter_with_len(
970-
supers_supertypes.len() + 1,
971-
supers_supertypes
972-
.iter()
973-
.copied()
974-
.chain(iter::once(supertype)),
975-
)
976-
.map_err(|e| e.unwrap_oom())?;
964+
let supertypes = supers_supertypes
965+
.iter()
966+
.copied()
967+
.chain(iter::once(supertype))
968+
.try_collect()?;
977969

978970
self.type_to_supertypes
979971
.insert(ty, Some(supertypes))
@@ -1155,25 +1147,27 @@ impl TypeRegistryInner {
11551147
fn assign_shared_type_indices(
11561148
&mut self,
11571149
non_canon_types: &[(ModuleInternedTypeIndex, WasmSubType)],
1158-
shared_type_indices: Box<[core::mem::MaybeUninit<VMSharedTypeIndex>]>,
1150+
mut shared_type_indices: Vec<VMSharedTypeIndex>,
11591151
) -> Box<[VMSharedTypeIndex]> {
1160-
debug_assert_eq!(non_canon_types.len(), shared_type_indices.len());
1152+
debug_assert_eq!(non_canon_types.len(), shared_type_indices.capacity());
1153+
debug_assert!(shared_type_indices.is_empty());
11611154
debug_assert!(
11621155
self.types.capacity() - self.types.len() >= non_canon_types.len(),
11631156
"should have reserved capacity"
11641157
);
1165-
boxed_slice_write_iter(
1166-
shared_type_indices,
1167-
non_canon_types.iter().map(|(module_index, ty)| {
1168-
let engine_index =
1169-
slab_id_to_shared_type_index(self.types.alloc(None).expect("have capacity"));
1170-
log::trace!(
1171-
"reserved {engine_index:?} for {module_index:?} = non-canonical {ty:?}"
1172-
);
1173-
engine_index
1174-
}),
1175-
)
1176-
.expect("iterator is same length as slice")
1158+
for (module_index, ty) in non_canon_types.iter() {
1159+
let engine_index =
1160+
slab_id_to_shared_type_index(self.types.alloc(None).expect("have capacity"));
1161+
log::trace!("reserved {engine_index:?} for {module_index:?} = non-canonical {ty:?}");
1162+
shared_type_indices
1163+
.push(engine_index)
1164+
.expect("reserved capacity");
1165+
}
1166+
1167+
debug_assert_eq!(shared_type_indices.len(), shared_type_indices.capacity());
1168+
shared_type_indices
1169+
.into_boxed_slice()
1170+
.expect("capacity should be exact")
11771171
}
11781172

11791173
/// For each cross-rec group type reference inside `entry`, increment the

0 commit comments

Comments
 (0)