-
Notifications
You must be signed in to change notification settings - Fork 702
Description
A wasm application should declare:
- A minimum usable heap size. The runtime would not start the application unless this could be allocated, and might use this to defer the start of the app until resources are available, or use the size in planning etc.
- A requested maximum heap size. The runtime can choose to allocate a size between the minimum and this maximum inclusive.
- Request the use of on-the-fly heap resizing. The runtime can accept or decline this request.
- Request an ability to grow or shrink the heap after exiting the wasm execution context. The wasm app would be re-entered after the size has changed, and the module might have been re-compiled. The runtime could decline this request. Global variable state would probably want to be preserved.
The runtime can:
- Allocate a size between the minimum and maximum. This allows the runtime to round up the size to better support bounds checking and to allocate a larger initial size where there is ample address space. For example, the runtime can round up the size to a page boundary. For example, on ARM 32-bit the runtime can round up the size to a value that can be encoded as an immediate value in bounds check instructions.
- Choose if on-the-fly heap resizing is supported. For example, a runtime that has degraded performance when using on-the-fly heap resizing but has ample address space can decline the request for on-the-fly heap resizing and allocate a large fixed heap size and optimize for this. For example, a device with limited cpu power and limit memory could run all-in on one application, giving it all the memory available and optimizing performance for a fixed heap size.
- Emit optimized asm.js code when decoding, avoiding the heap resizing code if this has been declined so that the host asm.js compiler emits optimal code.
- Emit constant globals with the accepted sizes and choices. For example: a constant length if a fixed heap size is chosen. Perhaps also index masking constants if a fixed heap size is chosen (but this can be another issue).
Alternatives to the on-the-fly resizing should be considered for the MVP. The motivation for the on-the-fly resizing appears to be legacy applications that hit OOM in deeply nested call contexts and need to expand the heap while preserving this call context. If applications can exit execution while resizing and re-enter when done then the runtime has far more options and this approach should be advocated for new applications and legacy applications should be encouraged to be reworked to use this approach.
The specification should warn developers that the use of on-the-fly heap resizing limits optimization of the code and does not perform well across browsers and limits the usability of the application by limiting the ability of the runtime to manage resources.
There should be an interface for applications to call to request a larger heap size after exiting the wasm context. This interface could also include a minimum and maximum size. If the runtime could not allocate the minimum then than app would enter an OOM state. The runtime can choose to resize between the minimum and maximum requested sizes, rounding for performance. A runtime could choose to re-compile a module with a new fixed size if warranted to affect a heap size increase.