-
Notifications
You must be signed in to change notification settings - Fork 695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: An extremely simple solution to the WebAssembly memory issue #1543
Comments
This is similar to the |
Indeed. Adding a webassembly instruction to match the functionality of clearPages() (i.e At a glance, the two proposals don't seem mutually exclusive, but I believe separating memory discarding from the larger memory control proposal would encourage vendors to implement specifically the discard functionality quicker, without waiting to complete the full memory control proposal. |
The linked memory.discard proposal has a memoryObj.discard() API as well as instruction. Similar to how we already have a memoryObj.grow() method and memory.grow instruction.
I believe your proposal is basically identical to the memory.discard proposal (correct me if I'm wrong). SpiderMonket has a complete implementation of it, so we generally support the idea. The biggest blocker for moving it forward is having users experiment with the instruction and show that it has value. |
I'll definitely check it out, I didn't realise it was this close to being ready. |
#1427, #1439
This proposal defines one new function to (almost) completely address WebAssembly's linear memory problem
What does it do?
clearPages
clears the pages in the range [start
,start+count
), setting every byte to0
The magic appears in the function's guarantees, namely, that the underlying memory is decommitted and returned to the OS
An implementation could:
VirtualFree
/munmap
/ etc)Most OSes already make allocate-on-demand guarantees about memory pages (e.g
VirtualAlloc(MEM_RESERVE | MEM_COMMIT)
will not actually "commit" memory until a page fault occurs), however even if the page is filled again with zero bytes, it will stay in physical memory until decommited via for exampleVirtualFree
. This can be trivially worked around even if the page is still needed by uncommiting and recommiting it.An implicit consequence of this proposal is that memory initially allocated by
WebAssembly.Memory
should also be in this uncommitted state (this is already somewhat the case in V8, although currently accessing some page N will commit all pages <= N at the same time)To make full use of this, we could create new memory with 65536 pages (4GB, the full address space) and physical pages would only be allocated as accessed, and freed on
clearPages
, providing functionality almost identical to native page allocation (minus memory protection). Javascript could then expose this control to a wasm module via amalloc
/free
interface that guarantees low memory fragmentation and proper memory decommitting. A more strict implementation could also start with a minimal memory object and .grow() as new pages are neededNote that with this proposal, a
Memory
's size, and the.grow()
API take on a more semantic meaning: they now only define the arbitrary limit at which pages should stop being allocated, rather than the true number of pages to be allocated (analogous to howVirtualAlloc
andmmap
do not actually allocate pages but tell the OS where it is appropriate to allocate pages on-demand)The text was updated successfully, but these errors were encountered: