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.
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.
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 thememory.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
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!
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
the host side doesn't specify memory size -- Wasmtime will do whatever the module says
ohh then what's the 18 default pages size ? is it not set by wasmtime ?
no; Wasmtime doesn't set a default number of pages
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
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 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 ?
that's for memories you define and a guest module imports
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.
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
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 ?
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
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
so this means I should search for ways on the wasm compiler toolchain side to specify memory size rather than wasmtime ?
yes, exactly
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
other than that, wasm-ld will I think pack things in like any other linker would, linearly in the address space
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
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
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
best of luck!
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:
Chris Fallin said:
best of luck!
thanks!
Last updated: Dec 23 2024 at 13:07 UTC