-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Use memory addresses instead of creating static arrays in Rust template #138
base: main
Are you sure you want to change the base?
Conversation
Static arrays will increase the size of the cartridge, because at runtime they will be initialized with the values used in the source (which is then ignored by the allocator).
✔️ Deploy Preview for wasm4 ready! 🔨 Explore the source changes: a5f9c23 🔍 Inspect the deploy log: https://app.netlify.com/sites/wasm4/deploys/6182b27249fc800007f2ec39 😎 Browse the preview: https://deploy-preview-138--wasm4.netlify.app |
You're right that the 20 KB of zero bytes seems end up in the .wasm when using heap allocation. Luckily there's a flag to After investigating more, it looks like Rust also includes DWARF debug info even in release builds, there's another flag called Using these flags shrinks wasm-opt minesweeper.wasm -o minesweeper-opt.wasm -Oz --strip-dwarf --strip-producers --zero-filled-memory ( |
I can only get it down to 41 KiB with that command, but it does indeed remove those data items from the cartridge. Should |
Yeah, let's add some documentation about |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You cannot just define some random pointers and pretend there's a heap. It's just bad. There's #255 because of this.
Instead of this i would recommend to use MaybeUninit
(assuming allocator doesn't require zeroed memory) to reduce cartridge size by eliminating a bunch of zeroes. It would place the heap into the bss
section, which shouldn't be initiated thus no initialization zeroes. The only problem is llvm currently fills bss with zeroes too. Maybe we should fix this?
Static arrays will increase the size of the cartridge, because at runtime they will be initialized with the values used in the source (which is then ignored by the allocator).
If we start from an empty WASM-4 Rust project (the one created with
w4 new --rust
) and we add just these two lines:the cartridge size goes from 509 B to 27 KiB (actually to 64 KiB, because debug information are also retained, but those can be removed with wasm-gc and wasm-opt).
About 7 KiB of that seem to be due to buddy-alloc functions, but 20 KiB are allocated for the two static arrays
FAST_HEAP
andHEAP
defined inalloc.rs
(itemdata[2]
).If we change the values of
FAST_HEAP_SIZE
andHEAP_SIZE
, the size ofdata[2]
changes accordingly.I think it is because static array must be initialized at runtime with their value specified in the source, which must be carried along in the cartridge.
If instead we bypass those arrays entirely and just use raw memory addresses, this does not happen.
I placed the two heap areas at the end of the memory (
0x10000 - HEAP_SIZE - FAST_HEAP_SIZE
), because if I put them right after the framebuffer I they just cause complete corruption (probably because the stack is growing from that side?).Hopefully I am not missing out any obvious issue in using raw memory addresses.
I tested on my games and they seem to work the same.