Refactor and optimize the flat type calculations (#4708)
* Optimize flat type representation calculations Previously calculating the flat type representation would be done recursively for an entire type tree every time it was visited. Additionally the flat type representation was entirely built only to be thrown away if it was too large at the end. This chiefly presented a source of recursion based on the type structure in the component model which fuzzing does not like as it reports stack overflows. This commit overhauls the representation of flat types in Wasmtime by caching the representation for each type in the compile-time `ComponentTypesBuilder` structure. This avoids recalculating each time the flat representation is queried and additionally allows opportunity to have more short-circuiting to avoid building overly-large vectors. * Remove duplicate flat count calculation in wasmtime Roughly share the infrastructure in the `wasmtime-environ` crate, namely the non-recursive and memoizing nature of the calculation. * Fix component fuzz build * Fix example compile
This commit is contained in:
@@ -60,7 +60,7 @@ pub enum FlagsSize {
|
||||
/// Flags can fit in a u16
|
||||
Size2,
|
||||
/// Flags can fit in a specified number of u32 fields
|
||||
Size4Plus(usize),
|
||||
Size4Plus(u8),
|
||||
}
|
||||
|
||||
impl FlagsSize {
|
||||
@@ -73,7 +73,11 @@ impl FlagsSize {
|
||||
} else if count <= 16 {
|
||||
FlagsSize::Size2
|
||||
} else {
|
||||
FlagsSize::Size4Plus(ceiling_divide(count, 32))
|
||||
let amt = ceiling_divide(count, 32);
|
||||
if amt > (u8::MAX as usize) {
|
||||
panic!("too many flags");
|
||||
}
|
||||
FlagsSize::Size4Plus(amt as u8)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user