Use memory addresses instead of creating static arrays in Rust template #138
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.