-
Notifications
You must be signed in to change notification settings - Fork 293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce wasmi::Engine
memory consumption for storing function artifacts
#982
Comments
Depends on new |
Using the following definitions for Wasmi's pub struct UncompiledFuncEntity {
func_index: u32,
bytes: SmallByteSlice,
module: ModuleHeader,
validate: Option<wasmparser::ValidatorResources>,
}
pub enum SmallByteSlice {
Small {
len: u8,
bytes: [u8; 22],
},
Big(Box<[u8]>),
}
All in all this nets us the following improvements in memory consumption:
Therefore, 40 bytes savings per function processed by the |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
wasmi::Engine
stores the compiled functions and for lazy compilation also the information necessary for lazy compilation for each function in parsedwasmi::Module
s. However, the memory consumption is non-optimal and could easily be improved.wasmi/crates/wasmi/src/engine/code_map.rs
Line 150 in 44987c1
The root cause is that each
UncompiledFuncEntity
stores redundant information:func_to_validate: FuncToValidate<..>
field internally stores thefunc_idx
which could be eliminated. The new version ofwasmparser
allows to destructure theFuncToValidate
type to do this.FuncToValidate
type holds aWasmFeatures
which currently weighs in with a whopping 23 bytes. Again, the new version allows to extract thisWasmFeatures
and store it once shared for all uncompiled function entities.With these two changes we can eliminate roughly 32 bytes (alignment) per Wasm function in the
wasmi::Engine
. For a Wasm blob with 1000 functions this equals to roughly 32kB less memory consumption which can be big. In total this would shrink theUncompiledFuncEntity
type to 20 bytes on a 64-bit platform or 12 bytes on a 32-bit platform.Also the
CompiledFunctionEntity
type can be shrinked:wasmi/crates/wasmi/src/engine/code_map.rs
Line 259 in 44987c1
We could store raw-pointers instead of
Box<[T]>
for bothinstrs
andconsts
fields and store the lengths inu16
fields instead. On 64-bit platforms this would shrink the type from 34 bytes (40 bytes aligned) down to 22 bytes (24 bytes aligned). Additionally this would make thelen_cells
method a lot more efficient.This could have a big impact especially on Wasmi's new lazy compilation functionality when a malicious attacker uses a Wasm with tons of (near) empty functions in order to stretch the worst-case for lazy Wasmi's compilation.
The text was updated successfully, but these errors were encountered: