Nyxus: Inside the Retained Heap

What the viewer will understand

Where do native values in a Store module go when a collection allocates or grows? The viewer already knows how a service borrows its module's private Data. Here they follow an allocation into one Wasm page and back out to the provider. They should be able to explain how Nyxus and the runtime claim whole pages from one provider and how an empty page becomes available to either. Allow roughly three minutes, with time to read the page and slot boundaries.

The worked example

Begin with Shop's Inventory, whose private Data contains a Nyxus Map<u64, Item>. For an isolated look at allocation, use the native test retained_growth_is_independent_of_default_global_and_thread. It checks an ordinary vector containing 128 copies of 11 and a vector constructed with Vec::new_in(Nyxus). The Nyxus vector starts with [1, 2, 3], reaches 20,000 items, and is then dropped. Its first and last values are 1 and 19999. The test verifies retained backing at 10,000 items. Inventory memory declaration · Native Nyxus allocator test

The native test uses a System-backed Nyxus heap and the process Global allocator. Use its assertions for allocator choice and values. Draw shared page ownership and reuse in a separate Wasm illustration based on the page provider. The native nine-to-sixteen-byte resize supplies the unchanged pointer and byte values. Its slot geometry comes from the common heap's size-class calculation. Native allocator · Wasm runtime memory · Heap implementation · Page pool reuse test

The story in one pass

Start with the item map inside the familiar inventory. Follow its allocation path down into main memory, then use two vectors to see which allocator owns their backing. Zoom into a page to see what a small allocation occupies, and back out when a growing buffer needs a run of whole pages. Drop the buffer, let the provider reuse an empty page, and return to the module view as a second memory band appears for Hylix.

Components and views

Use the shared visual language. Reuse the Shop MemoryModule for Inventory and put a vector header with its backing-buffer edge in the allocator test view. Draw MemoryPage, MemorySpan, and MemorySlot in their existing nested arrangement. Within the Wasm illustration, each page has one owner indication at a time, with the runtime subdued, Nyxus in the retained color, and free pages showing the paper background. Keep the address order fixed as ownership changes.

View Camera anchor and visible machinery
inventory_data Start at lesson two's final branch_slot framing on Inventory's Data.items. Follow the route down into the main-memory band with the attachment to Branches still in view.
vector_pair The native probe. Both headers, allocator names, and backing bytes are visible together.
page_provider Main-memory strip, one instance of each allocator, and their routes to the common provider. Keep the newest claim and its nearest live neighbor in view.
small_slot The same selected page enlarged, with the page header, buddy spans, and the exact live slot in frame.

Scene heap_owners — Follow the backing allocation

Enters with The Store owner and its private inventory Data from lesson two.

Beat follow_inventory — From an item field to native memory

View and focus inventory_data, on items and the path to its native backing.

Narration

Follow Inventory's item map into main memory. Nyxus allocates its native B-tree nodes there. An allocator test makes the same choice with a simpler collection, a vector.

Cue and animation On “Follow Inventory's item map”, follow the route below the cell to the Nyxus-owned page in the shared main-memory band. Keep the attachment and the private Data in view throughout the pan.

Leaves with A visible route from the owner to a retained allocation.

Beat two_vectors — Identify the allocator from the call

View and focus vector_pair, centered on Vec::new_in(Nyxus).

Narration

The native test starts with two vectors. An ordinary Rust vector gets its buffer from Global. The second constructor names Nyxus. The retained buffer starts with one, two, three.

Cue and animation On “An ordinary Rust vector”, show vec![11u64; 128] and light its Global route. On “The second constructor”, light the Nyxus route and reveal the three initialized values. Give this view the small label native allocator test.

Leaves with Two live vectors and two visible backing routes. Global and Nyxus each still own their own allocations.

Beat shared_provider — Draw two owners on the same strip

View and focus page_provider, centered on the provider and two distinct pages.

Narration

In a canister, the runtime and Nyxus ask one provider for sixty-four kibibyte pages. It tracks which allocator owns each claim. As either client needs space, it can claim available pages from that same pool.

Cue and animation On “In a canister”, clear both native vector drawings and cut to the strip labeled Wasm main memory. At “one provider”, light the Runtime and Nyxus claims on the same strip with an available page beside them.

Leaves with One claimed Nyxus page in focus and the provider in view.

Scene heap_growth — Take a slot, then a page run

Enters with The Wasm provider has a claimed Nyxus page in view.

Beat span_slots — Fit a small request into a slot

View and focus small_slot, centered on a span within the Nyxus page.

Narration

Inside a page, the allocator divides space into spans. A span serves one size class, with slots of equal size. A nine-byte request aligned to eight bytes takes a sixteen-byte slot.

Cue and animation On “divides space into spans”, enlarge the same page to show the span and free slots. On “takes a sixteen-byte slot”, seat the nine-byte request in one slot, mark its used width and hold on the enclosing span.

Leaves with A live slot inside the Nyxus slab and its neighbor fixed in place.

Beat grow_in_place — Extend a buffer that still fits

View and focus small_slot, on the buffer and its unchanged start address.

Narration

Both layouts fit the same slot, so growing to sixteen keeps the same address. The test requests zeroed growth. The first nine bytes keep their values and the seven added bytes start at zero.

Cue and animation On “growing to sixteen”, extend the used-width mark and keep the start tick pinned. At “requests zeroed growth”, label the byte row grow_zeroed and fill the seven added positions with 00 after the nine 37 hex bytes from the native test. The surrounding span depicts the allocator's source-derived class geometry.

Leaves with Sixteen initialized bytes and the original start address in view.

Beat grow_into_run — Move to a wider allocation

View and focus Begin at vector_pair, then re-establish page_provider for the page-run explanation.

Narration

Give the vector more elements and it eventually needs a larger buffer. The native test reaches twenty thousand integers. For a large allocation in Wasm, the allocator claims a run of whole pages. When resizing moves a buffer, the existing elements follow it into the new space.

Cue and animation At “twenty thousand integers”, show the native test at length 20000 with 1 and 19999 at its ends. At “For a large allocation in Wasm”, cut back to the page diagram. Highlight the adjacent claimed pages and follow the preserved prefix only at “resizing moves a buffer”. The original small-slot example stays in its own page.

Leaves with A live Nyxus page run in the Wasm illustration.

Scene heap_reuse — Give the provider space back

Enters with A live retained buffer and a separate runtime allocation visible in the illustration.

Beat release_page — Drop and make the run available

View and focus page_provider, with the released range and a live neighbor in frame.

Narration

Dropping the vector releases its buffer. A whole-page run goes straight back to the provider. Other live allocations stay where they are.

Cue and animation On “goes straight back to the provider”, change the whole illustrated run's ownership marks to available in place. Hold on the live neighbor and the small-slot page.

Leaves with The run available again. The small-slot page is still claimed by Nyxus.

Beat release_slot — Reuse a slot, then the page

View and focus Return to small_slot, then widen to page_provider as it releases the page.

Narration

On the smaller page, a freed slot can take another request of its class. When the last allocation goes, the whole page becomes available. Either allocator can claim it next. The Wasm memory extent stays the same.

Cue and animation On “a freed slot”, revisit the earlier span and release one slot with a neighbor still live. At “the last allocation goes”, retire the remaining live slot and return the now-empty page. On “Either allocator can claim it next”, show Runtime claim that page without moving its address or resizing the strip.

Leaves with The main-memory strip at the same extent and a free page visibly reused across owners.

Beat introduce_stable_band — Stay inside the canister

View and focus Widen from page_provider to the module and below the main-memory strip.

Narration

In the next canister, Payloads uses Nyxus and Records uses Hylix. Both attach to the same root. Records keeps a small descriptor in main memory. Its data lives in stable memory.

Cue and animation At “In the next canister”, cut to the engine-composition probe after seed(256). Use composed_root with payloads on the left and records on the right, both at revision 0. Preserve the shared main-memory strip and owner colors. On “Its data lives in stable memory”, reveal the stable-memory band below it and the route from the retained Record<Data> descriptor.

Leaves with Both bands established for lesson four, with one root and both owners in the engine-composition probe. Probe wiring

Review notes

  • Check vector_pair against the initial values, 10,000-item retained backing check, and 20,000-item endpoint. The first push after [1, 2, 3] is another 3. Show the checked endpoints at full length. The final drop is in the test, and the resulting page release follows the Wasm allocator code. Check grow_in_place against its separate nine-to-sixteen-byte source test.
  • Inspect the frame before and after a Wasm page is claimed or returned. The page color can change while the address and strip extent stay fixed. A free slot inside a page leaves that page in its allocator's ownership.
  • Keep native probe events separate from illustrative Wasm addresses and size classes when preparing capture data. Any shown allocation address, capacity or number of resized buffers needs a fresh trace for the chosen test and target.
  • Confirm the owner above the memory strip remains legible during the opening pan and that the closing view has room for Record<Data> and its stable route.