zkbitcoin.com
zkbitcoin.com

Reputation: 49

how to overcome memory limit on vector allocation in rust code assembled to WASM binaries

Update: created a small test app (react calling wasm rust code) but cant recreate rust exception, however this one liner is an extract from bit bigger function and I am still on to find what particular combo is leading to this exception

https://github.com/zkbitcoin/react-rust-wasm-vector-test

instructions:

a) clone b) rustup component add rust-src --toolchain nightly-2022-11-17-x86_64-apple-darwin c) yarn build:wasm d) yarn build e) yarn start

I compiled this sample rust code to WASM (note this one liner runs fine in standard non WASM environment)

let s = vec![vec![1usize; 131072]; 38];

This code fails in Chrome with:

rust_alloc_error_handler ... at alloc::vec::from_elem

Does anyone know how to overcome it? I tried both default and wee_alloc, both are failing.

Upvotes: 1

Views: 612

Answers (1)

zkbitcoin.com
zkbitcoin.com

Reputation: 49

In the original code I moved vec![vec![1usize; n]; p.columns.len()] size assignment to be first in the function, this got rid of mem alloc exception.

Resolved all my issues, this was also helpful (added --max-memory=4294967296 to RUSTFLAGS)

Only 1/4th of max memory available when rust wasm compiled with +atomics flag webassembly

Upvotes: 0

Related Questions