Skip to content

Commit

Permalink
wasmparser: Make TypeId fit in 8 bytes
Browse files Browse the repository at this point in the history
Doesn't actually need bit packing yet, but with eventual canonicalization of
reference types we probably will need to start doing that.
  • Loading branch information
fitzgen committed Sep 27, 2023
1 parent 0379c5e commit 044b3cf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/wasmparser/src/validator/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ impl Module {
.collect();

for (id, ty) in idx_types {
self.check_subtype(id.index as u32, ty.clone(), features, types, offset)?;
self.check_subtype(id.index() as u32, ty.clone(), features, types, offset)?;
if !features.gc {
self.types.push(id);
}
Expand Down
21 changes: 15 additions & 6 deletions crates/wasmparser/src/validator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ fn push_primitive_wasm_types(ty: &PrimitiveValType, lowered_types: &mut LoweredT
#[repr(C)] // use fixed field layout to ensure minimal size
pub struct TypeId {
/// The index into the global list of types.
pub(crate) index: usize,
index: u32,

/// A unique integer assigned to this type.
///
/// The purpose of this field is to ensure that two different `TypeId`
Expand All @@ -155,10 +156,16 @@ pub struct TypeId {
unique_id: u32,
}

impl TypeId {
pub(crate) fn index(&self) -> usize {
usize::try_from(self.index).unwrap()
}
}

// The size of `TypeId` was seen to have a large-ish impact in #844, so this
// assert ensures that it stays relatively small.
const _: () = {
assert!(std::mem::size_of::<TypeId>() <= 16);
assert!(std::mem::size_of::<TypeId>() <= 8);
};

/// Metadata about a type and its transitive structure.
Expand Down Expand Up @@ -1072,7 +1079,7 @@ impl<'a> TypesRef<'a> {
///
/// Returns `None` if the type id is unknown.
pub fn get(&self, id: TypeId) -> Option<&'a Type> {
self.list.get(id.index)
self.list.get(id.index())
}

/// Gets a core WebAssembly type id from a type index.
Expand Down Expand Up @@ -1891,7 +1898,7 @@ impl<T> Index<TypeId> for SnapshotList<T> {

#[inline]
fn index(&self, id: TypeId) -> &T {
self.get(id.index).unwrap()
self.get(id.index()).unwrap()
}
}

Expand Down Expand Up @@ -2012,6 +2019,7 @@ impl TypeAlloc {
/// hash-equivalent to anything else.
pub fn push_ty(&mut self, ty: Type) -> TypeId {
let index = self.list.len();
let index = u32::try_from(index).unwrap();
self.list.push(ty);
TypeId {
index,
Expand Down Expand Up @@ -3239,17 +3247,18 @@ impl Index<TypeId> for SubtypeArena<'_> {
type Output = Type;

fn index(&self, id: TypeId) -> &Type {
if id.index < self.types.len() {
if id.index() < self.types.len() {
&self.types[id]
} else {
&self.list[id.index - self.types.len()]
&self.list[id.index() - self.types.len()]
}
}
}

impl Remap for SubtypeArena<'_> {
fn push_ty(&mut self, ty: Type) -> TypeId {
let index = self.list.len() + self.types.len();
let index = u32::try_from(index).unwrap();
self.list.push(ty);
TypeId {
index,
Expand Down

0 comments on commit 044b3cf

Please sign in to comment.