Skip to content

Conversation

@joseph-gio
Copy link
Member

@joseph-gio joseph-gio commented Dec 30, 2025

Objective

The current asset loading pipeline on WASM looks like this:

  1. Make an HTTP request for the asset via the fetch web API.
  2. Acquire the response body as a JS ArrayBuffer, and make a JS UInt8Array view into it.
  3. Allocate a rust Vec<u8> and copy the contents of the JS Uint8Array into it.
  4. Return the contents of the vector as a VecReader, which implements AsyncRead.
  5. (For most asset types) Call .read_to_end() on the type-erased VecReader to copy the bytes into another Vec.
  6. Parse the asset type from the final vec.

Step (3.) above is unnecessary, as we can read from the Uint8Array directly.

Solution

Add a new UInt8ArrayReader type, which wraps a JS byte buffer to implement AsyncRead.

  • In order to implement read_to_end() for this type, I had to fork stackfuture to add support for non-send futures.

NOTE: before we can merge this PR, we will either have to wait until the following stackfuture PR is merged, or publish a fork: microsoft/stackfuture#34

Testing

  • Ran the many_foxes example as a smoke test, which did not appear to have any problems.
  • Tested a very similar change against the 0.15.3 release, for the WASM app at my workplace.
  • Still need to test the AsyncSeek functionality -- as far as I can tell this functionality is not used by any of our AssetLoaders.
@alice-i-cecile alice-i-cecile added A-Assets Load files from disk to use for things like images, models, and sounds C-Performance A change motivated by improving speed, memory usage or compile times S-Blocked This cannot move forward until something else changes labels Dec 30, 2025
Copy link
Contributor

@andriyDev andriyDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a big fan of getting rid of these copies!! Thank you!

The use of unsafe is a little uncomfy, but it's a pretty obvious optimization and avoids assigning a bunch of zeroes for no reason.

@github-actions
Copy link
Contributor

The generated examples/README.md is out of sync with the example metadata in Cargo.toml or the example readme template. Please run cargo run -p build-templated-pages -- update examples to update it, and commit the file change.

Copy link
Contributor

@andriyDev andriyDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving though this is of course blocked on the git dependency.

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

Labels

A-Assets Load files from disk to use for things like images, models, and sounds C-Performance A change motivated by improving speed, memory usage or compile times S-Blocked This cannot move forward until something else changes

4 participants