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:
Alex Crichton
2022-08-16 13:31:47 -05:00
committed by GitHub
parent 3c1490dd59
commit bc8e36a6af
14 changed files with 561 additions and 279 deletions

View File

@@ -1015,7 +1015,7 @@ fn expand_flags(flags: &Flags) -> Result<TokenStream> {
});
}
FlagsSize::Size4Plus(n) => {
count = n;
count = usize::from(n);
as_array = TokenStream::new();
bitor = TokenStream::new();
bitor_assign = TokenStream::new();
@@ -1072,7 +1072,7 @@ fn expand_flags(flags: &Flags) -> Result<TokenStream> {
.map(|i| {
let field = format_ident!("__inner{}", i);
let init = if index / 32 == i {
let init = if index / 32 == usize::from(i) {
1_u32 << (index % 32)
} else {
0