Bulk Memory
Copying or clearing a block of memory byte by byte in a loop works, but it's slow and bloats the code. The
bulk memory instructions (standard since WebAssembly 2.0) do it in one instruction that engines turn into an
optimised memcpy/memset.
| Instruction | Operands | Like C's |
|---|---|---|
memory.fill |
dest, value, count |
memset(dest, value, count) |
memory.copy |
dest, src, count |
memmove(dest, src, count) (overlap is safe) |
memory.init $seg |
dest, offset_in_segment, count |
copy from a passive data segment |
data.drop $seg |
free a passive segment |
memory.fill
(module
(import "console" "str" (func $print (param i32 i32)))
(memory (export "memory") 1)
(func (export "main")
(memory.fill (i32.const 0) (i32.const 61) (i32.const 10)) ;; ten '=' characters
(call $print (i32.const 0) (i32.const 10))
(memory.fill (i32.const 3) (i32.const 35) (i32.const 4)) ;; overwrite the middle with '#'
(call $print (i32.const 0) (i32.const 10))))==========
===####===
Only the low 8 bits of the value are used: memory.fill sets bytes.
memory.copy
memory.copy copies count bytes from src to dest. It behaves as if the bytes were first copied to a temporary
buffer, so overlapping regions work correctly in both directions:
(module
(import "console" "str" (func $print (param i32 i32)))
(memory (export "memory") 1)
(data (i32.const 0) "ABCDEFGHIJ")
(func (export "main")
(memory.copy (i32.const 20) (i32.const 0) (i32.const 10)) ;; plain copy
(call $print (i32.const 20) (i32.const 10))
(memory.copy (i32.const 22) (i32.const 20) (i32.const 6)) ;; shift right by 2, overlapping
(call $print (i32.const 20) (i32.const 10))
(memory.copy (i32.const 20) (i32.const 23) (i32.const 6)) ;; shift left by 3, overlapping
(call $print (i32.const 20) (i32.const 10))))ABCDEFGHIJ
ABABCDEFIJ
BCDEFIEFIJ
Bounds are checked first
A bulk instruction checks the whole range before writing anything. If any byte is out of bounds it traps, and memory is left unchanged (unlike a byte-by-byte loop, which would write the first part):
(module
(import "console" "log" (func $log (param i32)))
(memory 1)
(func (export "main")
(call $log (i32.load8_u (i32.const 65530)))
(memory.fill (i32.const 65530) (i32.const 1) (i32.const 10)) ;; 6 bytes fit, 4 don't
(call $log (i32.const 999)))) ;; never reached0
RuntimeError: memory access out of bounds
A count of zero is fine at any in-bounds address, including exactly at the end of memory.
Passive data segments and memory.init
An active segment is copied at instantiation. A passive segment (no address) is kept aside, and the program
copies it with memory.init whenever it wants, possibly several times. data.drop then releases it.
(module
(import "console" "str" (func $print (param i32 i32)))
(import "console" "log" (func $log (param i32)))
(memory (export "memory") 1)
(data $banner "*** Wasm ***") ;; passive: 12 bytes, not in memory yet
(func (export "main")
(call $log (i32.load8_u (i32.const 100))) ;; still zero: nothing copied yet
(memory.init $banner (i32.const 100) (i32.const 0) (i32.const 12))
(call $print (i32.const 100) (i32.const 12))
(memory.init $banner (i32.const 200) (i32.const 4) (i32.const 4)) ;; just "Wasm"
(call $print (i32.const 200) (i32.const 4))
(data.drop $banner)
(memory.init $banner (i32.const 300) (i32.const 0) (i32.const 1)))) ;; segment is gone0
*** Wasm ***
Wasm
RuntimeError: memory access out of bounds
After data.drop, the segment behaves as if it had length 0, so any non-empty memory.init from it traps.
Passive segments are useful for multi-threaded programs (each thread's memory initialised on demand) and for
resetting state.
Comparing a loop with bulk memory
Here's a hand-written byte copy next to memory.copy. Both produce the same result, but the bulk version is a
single instruction:
(module
(import "console" "str" (func $print (param i32 i32)))
(memory (export "memory") 1)
(data (i32.const 0) "copy me, please")
(func $copy_loop (param $dst i32) (param $src i32) (param $n i32)
(block $done
(loop $next
(br_if $done (i32.eqz (local.get $n)))
(i32.store8 (local.get $dst) (i32.load8_u (local.get $src)))
(local.set $dst (i32.add (local.get $dst) (i32.const 1)))
(local.set $src (i32.add (local.get $src) (i32.const 1)))
(local.set $n (i32.sub (local.get $n) (i32.const 1)))
(br $next))))
(func (export "main")
(call $copy_loop (i32.const 100) (i32.const 0) (i32.const 15))
(memory.copy (i32.const 200) (i32.const 0) (i32.const 15))
(call $print (i32.const 100) (i32.const 15))
(call $print (i32.const 200) (i32.const 15))))copy me, please
copy me, please
Tip: With clang, pass
-mbulk-memory(enabled by default in recent versions) somemcpy/memsetcompile to these instructions instead of library loops.
Test yourself
What are the operands of memory.copy?
- src, dest, count
- dest, src, count
- dest, value, count
- count, src, dest
What happens if part of a memory.fill range is out of bounds?
- The in-bounds part is filled, then it traps
- It traps without writing anything
- It silently stops at the end of memory
- Memory grows automatically
What is a passive data segment?
- A segment that is read-only
- A segment copied only when
memory.initis executed - A segment stored in a table
- A segment that is compressed