Stream: wasmtime

Topic: ✔ Some questions regarding Component::serialize


view this post on Zulip Friedrich Vandenberghe (Apr 30 2024 at 07:01):

Hey, I have 3 questions surrounding Component::serialize:

  1. If I'm comprehending the documentation correctly, this function isn't that different from Engine::precompile_module. Am I correct to assume this?
  2. When I try to do Component::from_file on a component compiled with wasmtime compile, I get the following errror: Error: failed to parse led.cwasm: input bytes aren't valid utf-8. After looking at the sourcecode for the compile command, I see that it calls precompile_module, does this mean that a compiled component should be loaded with Component::deserialize_file instead?
  3. In the documentation of Engine::precompile_module it states the following:

    The output of this method is safe to send to another host machine for later execution. As the output is already a compiled module, translation and code generation will be skipped and this will improve the performance of constructing a Module from the output of this method.

    Does this also apply if the other host machine has a different architecture? I suppose so, but I'm not 100% certain.

A fast and secure runtime for WebAssembly. Contribute to bytecodealliance/wasmtime development by creating an account on GitHub.

view this post on Zulip Lann Martin (Apr 30 2024 at 12:41):

If I'm comprehending the documentation correctly, this function isn't that different from Engine::precompile_module. Am I correct to assume this?

Correct, with the minor correction that the matching function would be Engine::precompile_component (component vs module); Component/Module::serialize should produce the same output as Engine::precompile_component/_module.

view this post on Zulip Lann Martin (Apr 30 2024 at 12:45):

When I try to do Component::from_file on a component compiled with wasmtime compile, I get the following [...] does this mean that a compiled component should be loaded with Component::deserialize_file instead?

Yep: from_file is for loading "regular" binary or text (WAT) wasm from a file. deserialize_file is for Wasmtime/Cranelift-precompiled "cwasm".

view this post on Zulip Lann Martin (Apr 30 2024 at 12:54):

In the documentation of Engine::precompile_module it states the following: The output of this method is safe to send to another host machine for later execution. [...] Does this also apply if the other host machine has a different architecture?

It is "safe" in the sense that it will fail to load rather than execute incorrectly, but a precompiled cwasm contains native, non-portable binary code which will not work on a different architecture, and might not even work across different processor models with the same architecture due to different features/configuration. See Module::deserialize for details.

It should be possible to cross-compile cwasm from one architecture to another through careful configuration of, e.g. Config::target, but this is really an advanced use case and not something that I have personal experience with.

view this post on Zulip Friedrich Vandenberghe (May 03 2024 at 14:31):

Big thanks for the explanation! It all makes sense now.

view this post on Zulip Notification Bot (May 03 2024 at 14:31):

Friedrich Vandenberghe has marked this topic as resolved.


Last updated: Dec 23 2024 at 13:07 UTC