Skip to content

Should memory.discard take an address/size in units of pages instead of units of bytes? #28

Open

Description

@fitzgen

This would match how memory.grow takes operands in units of pages, not bytes.

This would also avoid the need for the instruction to do any rounding up/down to the next multiple of the page size. This, in turn, avoids the issue of spec'ing behavior for what to do when rounding up to the next multiple of the page size overflows to zero, which the current explainer is a little ambiguous on: should implementations trap on the overflow or succeed with a discard of size zero? I assume the intent is probably the former -- such sizes will be out of bounds for default-page-size memories (although not for 1-byte-page-size memories with the custom-page-sizes proposal!) -- but operating on units of pages lets us un-ask the whole question.

Activity

  1. bvisness commented on Nov 9, 2026

    @bvisness
    Collaborator

    I'd probably be fine with this. I went back and forth on bytes vs. pages but somewhat arbitrarily went with bytes, I think to hew more closely to native APIs. But the overflow point is a good one I hadn't considered.

    That said, taking an address in pages also feels quite strange. 100% of callers will have an address that they need to translate to a page index in order to use this instruction, which makes me feel like the instruction should take bytes. I also don't love the idea of memory allocators needing to be precisely aware of their page size just to do a routine free operation, but I admit I can't justify this position because they'll need to be aware of the page size to responsibly use it in the first place.

    Out of curiosity, what's the plan for exposing the page size in C/C++ and other toolchains? Now that custom page sizes are entering the picture, I'm curious what the plan is for integration into dlmalloc etc.

  2. fitzgen commented on Nov 9, 2026

    @fitzgen
    ContributorAuthor

    That said, taking an address in pages also feels quite strange. 100% of callers will have an address that they need to translate to a page index in order to use this instruction, which makes me feel like the instruction should take bytes.

    Yeah, that's true. I guess a third option is to take a byte address and page count, but mixing units like that feels perhaps the worst of all options.

    Out of curiosity, what's the plan for exposing the page size in C/C++ and other toolchains? Now that custom page sizes are entering the picture, I'm curious what the plan is for integration into dlmalloc etc.

    You can pass --page-size=1 to LLVM's wasm-ld to enable single-byte page sizes. The linker binds the __wasm_first_page_end symbol to the byte after the first page (aka the first byte of the second page if present aka the page size). Allocators should use this symbol instead of hard-coding 64KiB. This symbol's value an i32.const in the generated Wasm binary and therefore easily optimizable by binaryen and engines. (I don't know off-hand whether LLVM will see that constant during its LTO or if it is too late in the pipeline for that though.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions