Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Support for reserving address space? #4

Closed as not planned
Closed as not planned
@aardappel

Description

@aardappel

With a 64-bit address space, we don't just get to break the 4GB barrier, but potentially new programming techniques become available, in particular techniques that rely on using larger parts of the address space, i.e. the ability to reserve memory that does not cause physical memory to be used unless touched (possible on native platforms with mmap or VirtualAlloc).

This is particularly useful in working with large data sets, being able to reserve large arrays, and being able to work with them and:

  1. avoid the cost of realloc / memcpy as it grows.
  2. allowing internal pointers that don't get invalidated by those reallocs.
  3. can usually omit bounds checking at each pointer bump / push_back.

This can be particularly useful for implementing programming languages, that like to manage the implemented language's objects in a single contiguous blocks, and for whom the above 2 downsides of realloc would be prohibitive.

Efficient coroutine implementations may rely on it.
I can see it also useful for databases, larger caches etc.

We already have memory.grow that can extend memory without realloc, but that only allows one such large array, and assumes we have complete control over the allocator and other runtime code that may be using memory, which may be impractical.

Question is, how could we allow for this functionality?

  • Radical but simple: we could enable this by default, by simply specifying that untouched pages of Wasm memory do not cause physical memory pressure. This would require all implementations to use mmap underneath for all Wasm memory, which may be unpractical.
    There is also the question to what extend memory reserved with mmap can typically fail after being reserved, which to my knowledge it won't if not called with MAP_NORESERVE, but I'm no expert.
  • Add an explicit new instruction, like memory.reserve, which would guarantee address space above the limit that memory.grow has been called. The limit established by memory.grow would all be committed memory, and above that, uncommitted until touched. This would also be helpful in the case that reserving can fail and/or not all implementations want to support it (if memory.reserve fails, the program can fall back on a different memory strategy).
  • Add an actual mmap like instruction. That is likely unpractical since we may not want to have to deal with "holes" in our memories.

I realize browsers may be hesitant to hand out larger parts of address space, but I feel this feature could be important in the future, or for non-browser environments.

@binji @jakobkummerow @sunfishcode

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions