Stream: wasmtime

Topic: Specify initial memory allocation size


view this post on Zulip spino17 (Nov 09 2024 at 08:23):

How can I specify initial memory allocation size to be 4 pages instead of default 20 pages and then how to later grow it if the memory is insufficient while some module variable allocations.

view this post on Zulip Chris Fallin (Nov 09 2024 at 18:11):

This is a function of the memory declaration in the Wasm module -- e.g., (memory 4) vs. (memory 20). To grow, you'll want to use the memory.grow opcode.

view this post on Zulip spino17 (Nov 09 2024 at 18:54):

Chris Fallin said:

This is a function of the memory declaration in the Wasm module -- e.g., (memory 4) vs. (memory 20). To grow, you'll want to use the memory.grow opcode.

thanks @Chris Fallin for the response. So let's say I am using cargo to build my rust code for target wasm32-unknown-unknown, what should I add in the Rust code to make it import memory with the size 4 and then export the same memory

view this post on Zulip spino17 (Nov 09 2024 at 18:56):

I am confused whether this specification of memory being 4 pages goes on the host side using some wasmtime APIs or on the module side using some constructs provided by the language or both!

view this post on Zulip Chris Fallin (Nov 09 2024 at 18:56):

Oh, sorry, my answer was at the level of raw WebAssembly -- I assumed you were working at that level as that's the abstraction level at which memory size is handled. If you're targeting Wasm with a toolchain like Rust, it's up to the toolchain to decide the initial memory size -- wasm-ld in this case. There is an option to set the shadow stack size and turning it down may decrease the initial size (I don't recall it offhand) but fundamentally, you cannot dial in a manual size because it needs to be large enough to contain the data

view this post on Zulip Chris Fallin (Nov 09 2024 at 18:56):

the host side doesn't specify memory size -- Wasmtime will do whatever the module says

view this post on Zulip spino17 (Nov 09 2024 at 18:57):

ohh then what's the 18 default pages size ? is it not set by wasmtime ?

view this post on Zulip Chris Fallin (Nov 09 2024 at 18:58):

no; Wasmtime doesn't set a default number of pages

view this post on Zulip Chris Fallin (Nov 09 2024 at 18:58):

if you dump your module with, e.g., wasm2wat, you should see a declaration like (memory N) -- that's where the initial size comes from

view this post on Zulip Chris Fallin (Nov 09 2024 at 18:59):

think of it like a native binary (ELF or whatever) -- the program has a certain size for its .data and .bss sections. One can't tell LInux "please run this binary but only give it 4KiB of .data" -- that would break execution

view this post on Zulip spino17 (Nov 09 2024 at 18:59):

ohh so I can only set the maximum size using limiters and not the initial size. So why then wasmtime exposes Memory creation APIs ? why then host has control to create memory with min and max size ?

view this post on Zulip Chris Fallin (Nov 09 2024 at 19:00):

that's for memories you define and a guest module imports

view this post on Zulip spino17 (Nov 09 2024 at 19:00):

Chris Fallin said:

think of it like a native binary (ELF or whatever) -- the program has a certain size for its .data and .bss sections. One can't tell LInux "please run this binary but only give it 4KiB of .data" -- that would break execution

ohh yeah this makes sense! it's information coming completely from the module and not runtime dependent so makes sense.

view this post on Zulip Chris Fallin (Nov 09 2024 at 19:00):

in a sense, via the host APIs you can also do the things a Wasm module does; some other module could import this memory, just like some other module could import a memory exported by a module that specifies that memory's size

view this post on Zulip spino17 (Nov 09 2024 at 19:01):

Chris Fallin said:

that's for memories you define and a guest module imports

so this memory won't be used by the module stack and heap ?

view this post on Zulip Chris Fallin (Nov 09 2024 at 19:01):

Right, usually at least. I'm trying to be careful to make a distinction between standard and convention here; the convention for today's Wasm toolchains is that where they need stack and heap in their compilation model, they define their own memory, they don't import it

view this post on Zulip spino17 (Nov 09 2024 at 19:02):

ohh this clears up a lot of my doubts! so that means the stack heap memory can only be exported and the size comes purely from the binary and has no role of runtime to manipulate that

view this post on Zulip spino17 (Nov 09 2024 at 19:03):

so this means I should search for ways on the wasm compiler toolchain side to specify memory size rather than wasmtime ?

view this post on Zulip Chris Fallin (Nov 09 2024 at 19:04):

yes, exactly

view this post on Zulip Chris Fallin (Nov 09 2024 at 19:05):

the only fully configurable option I'm aware of for Rust is shadow stack size, and location -- see e.g. https://users.rust-lang.org/t/increasing-rust-wasm32-stack-size/73605

Quoting Make stack size configurable · Issue #479 · rustwasm/wasm-pack · GitHub [target.wasm32-unknown-unknown] rustflags = [ "-C", "link-args=-z stack-size=1500000", ] Is that all that is required to increase the wasm32 stack size, or is something else required ?

view this post on Zulip Chris Fallin (Nov 09 2024 at 19:06):

other than that, wasm-ld will I think pack things in like any other linker would, linearly in the address space

view this post on Zulip spino17 (Nov 09 2024 at 19:06):

ohh okay, much thanks @Chris Fallin for clearing up this. I don't know since how long I have been scratching my head around this memory management business of wasm

view this post on Zulip Chris Fallin (Nov 09 2024 at 19:06):

if you want to reduce memory usage my recommendations would be (i) reduce stack size and (ii) look for and find ways to remove any static data, e.g. from library functionality you don't use

view this post on Zulip spino17 (Nov 09 2024 at 19:06):

Chris Fallin said:

the only fully configurable option I'm aware of for Rust is shadow stack size, and location -- see e.g. https://users.rust-lang.org/t/increasing-rust-wasm32-stack-size/73605

thanks for sharing this, will check it out

view this post on Zulip Chris Fallin (Nov 09 2024 at 19:06):

best of luck!

view this post on Zulip spino17 (Nov 09 2024 at 19:07):

Chris Fallin said:

if you want to reduce memory usage my recommendations would be (i) reduce stack size and (ii) look for and find ways to remove any static data, e.g. from library functionality you don't use

will try to follow this advice and continue this investigation :smile:

view this post on Zulip spino17 (Nov 09 2024 at 19:07):

Chris Fallin said:

best of luck!

thanks!


Last updated: Nov 22 2024 at 17:03 UTC