near / wasmtime

Standalone JIT-style runtime for WebAssembly, using Cranelift
https://wasmtime.dev/
Apache License 2.0
3 stars 4 forks source link

Speed up execution of zkAsm code from Rust #227

Open akashin opened 5 months ago

akashin commented 5 months ago

Discussed in https://github.com/orgs/0xPolygonHermez/discussions/66

Originally posted by **akashin** February 7, 2024 # Context As a part of ZK WASM compiler (https://github.com/near/wasmtime) written in Rust we are compiling WASM code (tests like [this one](https://github.com/near/wasmtime/blob/main/cranelift/zkasm_data/add.wat)) to zkAsm and want to execute the resulting zkAsm code. At the moment, we use a nodejs interpreter to run each zkAsm file using [this script](https://github.com/near/wasmtime/blob/main/tests/zkasm/run-tests-zkasm.js). This provides the necessary functionality, but the startup time of `npm run` is around 700ms which would likely be too slow when we have more than 100 tests. We want to find a way to run these tests faster. We can't easily batch the execution into a single invocation of `npm run` as zkAsm files are generated on the fly by Rust tests that run in parallel. One of the ideas was to use https://deno.com/ runtime to run the JS code. This way it could be possible to [embed](https://crates.io/crates/deno_core) Deno runtime into Rust binary which would remove the overheads of starting a subprocess. I did a [quick prototype](https://github.com/near/wasmtime/pull/210) but it would require some changes to zkAsm codebase. See https://near.zulipchat.com/#narrow/stream/295306-pagoda.2Fcontract-runtime/topic/Running.20zkAsm.20code.20from.20Rust for full discussion. # Next steps - [ ] Measure the current execution time for tests - [ ] Try [`bun` nodejs runner](https://bun.sh/docs) - [ ] Try bundling Nodejs code before executing it - [ ] Try using Deno for execution