Skip to content

feat: compileModule() without the bodies of compiled functions - #252

Open
andreas-karlsson wants to merge 1 commit into
bytecodealliance:mainfrom
andreas-karlsson:compile-module
Open

andreas-karlsson wants to merge 1 commit into
bytecodealliance:mainfrom
andreas-karlsson:compile-module

Conversation

@andreas-karlsson

Copy link
Copy Markdown
Collaborator

Fixes #249. An alternative to #251.

Adds MachineFactoryCompiler.Builder.compileModule(), which returns a CompiledModule: the machine factory plus a copy of the module without the parsed bodies of compiled functions. It keeps only the bodies the interpreter may still run: the interpreted functions, plus any function they reach through tail calls. A new Instance.builder(CompiledModule) creates instances from it.

var compiled = MachineFactoryCompiler.builder(module).compileModule();
var instance = Instance.builder(compiled).build();

The module passed in is left unchanged, and compile() works as before. Nothing is dropped if an indirect tail call makes the reachable functions unknown, or when the compilation is loaded from the cache. Accessing a dropped body throws an IllegalStateException.

Heap after compile, keeping only the CompiledModule:

Data section main This PR
3.5 MB module 3.2 MB 15 MB 5 MB
14 MB module, 5k functions 12.4 MB 72 MB 18 MB

Parse and compile times are unchanged.

Compared with #251, this keeps WasmModule immutable, at the cost of a new compile method and an Instance.builder overload. Both are additive, so binary compatibility is kept.

A possible follow-up: most of the remaining heap is data segments. If the compiled module kept the segments next to the module copy instead of inside it, instances wouldn't pin them through their module, and they could be freed once nothing needs them.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Runtime compiler retains excessive memory

1 participant