Skip to content

Add configurable scratch memory reset strategies - #2100

Open
ludfjig wants to merge 1 commit into
hyperlight-dev:mainfrom
ludfjig:ll/memory-reset-strategy
Open

ludfjig wants to merge 1 commit into
hyperlight-dev:mainfrom
ludfjig:ll/memory-reset-strategy

Conversation

@ludfjig

@ludfjig ludfjig commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Add SandboxBuilder::memory_reset_strategy to choose in-place zeroing or discard.

This provides a manual workaround for the Windows performance regression discussed in #1766. In-place zeroing can be faster for small sandboxes. Discard can speed up larger restores, but may slow subsequent guest calls through page faults.

Document platform support and keep existing defaults.

Note that mshv supports movable pages in later kernel versions so might consider madvise dont_need in the future (but need to double check kernel version that supporst this first. I know latest 6.6 and 6.18 both work). In the future we can also add some probing method to dynamically select the better for a given sandbox size by running some perf testse on the machine

@ludfjig
ludfjig requested a review from danbugs as a code owner November 24, 2026 23:07
@ludfjig ludfjig added the C-enhancement For PRs adding features, improving functionality, docs, tests, etc. label Nov 24, 2026
@ludfjig
ludfjig requested a review from devigned as a code owner November 24, 2026 23:07
@ludfjig ludfjig added A-perf Addresses performance A-api Related to the API or public interface S-needs-review PR is ready for (re-)review labels Nov 24, 2026
Copilot AI balanced review requested due to automatic review settings November 24, 2026 23:07
@ludfjig
ludfjig force-pushed the ll/memory-reset-strategy branch from 632ead4 to b6b7862 Compare November 24, 2026 23:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The WHP in-place reset and reordered scratch remapping need Windows runtime validation to confirm guest-visible zeroing and backing lifetimes through mapping failures.

No open issues 🎉

What changed in this PR

Adds configurable scratch-memory reset strategies to Hyperlight’s host sandbox API. This provides manual performance tuning for the Windows regression discussed in #1766 while preserving existing backend defaults.

Changes:

  • Exposes PlatformDefault, ZeroInPlace, and Discard through sandbox configuration and the builder.
  • Coordinates scratch clearing and VM mappings during restore, with backend validation and KVM fallback behavior.
  • Documents platform support and adds tests for restore strategies, resizing, recreation, and failures.
File Description
src/​hyperlight_host/​src/​sandbox/​uninitialized.rs Clarifies configurable settings in a snapshot-construction comment.
src/​hyperlight_host/​src/​sandbox/​uninitialized_evolve.rs Validates strategy support during initialization.
src/​hyperlight_host/​src/​sandbox/​snapshot/​mod.rs Adapts snapshot tests to staged restoration.
src/​hyperlight_host/​src/​sandbox/​mod.rs Re-exports MemoryResetStrategy.
src/​hyperlight_host/​src/​sandbox/​initialized.rs Applies reset strategies and tests restore behavior and failures.
src/​hyperlight_host/​src/​sandbox/​config.rs Defines strategies and configuration accessors.
src/​hyperlight_host/​src/​sandbox/​builder.rs Exposes strategy selection and documents platform tradeoffs.
src/​hyperlight_host/​src/​mem/​shared_mem.rs Separates zeroing and discard operations, with tests.
src/​hyperlight_host/​src/​mem/​mgr.rs Splits restoration around scratch reset and mapping updates.
src/​hyperlight_host/​src/​lib.rs Exposes the strategy type at the crate root.
src/​hyperlight_host/​src/​hypervisor/​virtual_machine/​mod.rs Resolves backend defaults and rejects unsupported strategies.
src/​hyperlight_host/​src/​error.rs Adds an unsupported-strategy error.
CHANGELOG.md Records the public configuration feature.

Review effort: Balanced

@hyperlight-gh-bot

This comment has been minimized.

@ludfjig
ludfjig force-pushed the ll/memory-reset-strategy branch 2 times, most recently from 6f38ece to 2c579b0 Compare November 25, 2026 19:03
@hyperlight-gh-bot

This comment has been minimized.

@syntactically

Copy link
Copy Markdown
Member

and 6.18 both work

At least on some 6.18 builds it is a runtime tunable knob whether enabled or not I think.

@danbugs danbugs added this to the v0.20.0 milestone Nov 30, 2026
@ludfjig
ludfjig force-pushed the ll/memory-reset-strategy branch from 2c579b0 to 8df0641 Compare November 30, 2026 18:58
@ludfjig

ludfjig commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

and 6.18 both work

At least on some 6.18 builds it is a runtime tunable knob whether enabled or not I think.

correct, relevant to #2112

@hyperlight-gh-bot

This comment has been minimized.

@ludfjig
ludfjig force-pushed the ll/memory-reset-strategy branch from 8df0641 to 6be2550 Compare December 1, 2026 20:53
@ludfjig ludfjig linked an issue Dec 1, 2026 that may be closed by this pull request
@ludfjig
ludfjig force-pushed the ll/memory-reset-strategy branch 2 times, most recently from 4839c2a to 9b500e9 Compare December 1, 2026 21:20
Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
@ludfjig
ludfjig force-pushed the ll/memory-reset-strategy branch from 9b500e9 to f679fa2 Compare December 1, 2026 21:23
@hyperlight-gh-bot

Copy link
Copy Markdown

Benchmark Results

Measured commit: f679fa2669a7
Baseline commit: 54ae6d89eb42

kvm / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 307.48 ns (➖ 1.04x slower)
vec_bytes 92.52 ns (➖ 1.02x faster)
374.18 µs (➖ 1.01x faster)

payload_allocation

slot_pool_segmented
65536 142.04 ns (➖ 1.00x faster)
262144 521.11 ns (➖ 1.00x faster)

sandboxes

create_initialized_and_drop
medium 13.77 ms (➖ 1.02x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
7.68 ns (➖ 1.00x faster) 7.68 ns (➖ 1.00x faster) 7.69 ns (➖ 1.00x slower)

snapshot_files

load_snapshot_unverified
small 107.90 µs (➖ 1.00x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 1.91 µs (➖ 1.10x faster) 2.08 µs (➖ 1.09x slower)
262144 7.14 µs (➖ 1.02x faster) 8.31 µs (➖ 1.18x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 6.14 µs (➖ 1.07x faster) 6.25 µs (➖ 1.02x faster)
262144 28.63 µs (➖ 1.05x slower)
8192 1.04 µs (➖ 1.01x slower) 1.04 µs (➖ 1.04x slower)
kvm / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 282.64 ns (➖ 1.02x faster)
vec_bytes 85.35 ns (➖ 1.06x faster)
644.69 µs (➖ 1.02x faster)

payload_allocation

slot_pool_segmented
65536 144.17 ns (➖ 1.01x slower)
262144 533.68 ns (➖ 1.00x slower)

sandboxes

create_initialized_and_drop
medium 14.87 ms (➖ 1.08x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
7.14 ns (➖ 1.00x faster) 7.26 ns (➖ 1.01x faster) 7.22 ns (➖ 1.01x slower)

snapshot_files

load_snapshot_unverified
small 54.40 µs (➖ 1.04x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.09 µs (➖ 1.01x slower) 2.11 µs (➖ 1.00x slower)
262144 7.48 µs (➖ 1.00x slower) 7.46 µs (➖ 1.00x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 6.94 µs (➖ 1.01x faster) 7.08 µs (➖ 1.00x slower)
262144 29.02 µs (➖ 1.04x faster)
8192 713.34 ns (➖ 1.01x slower) 716.30 ns (➖ 1.03x faster)
mshv3 / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 392.65 ns (➖ 1.05x slower)
vec_bytes 117.76 ns (➖ 1.00x slower)
345.35 µs (➖ 1.00x slower)

payload_allocation

slot_pool_segmented
65536 193.99 ns (➖ 1.03x faster)
262144 749.04 ns (➖ 1.00x slower)

sandboxes

create_initialized_and_drop
medium 69.04 ms (➖ 1.01x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
9.87 ns (➖ 1.00x faster) 9.88 ns (➖ 1.01x faster) 9.88 ns (➖ 1.01x faster)

snapshot_files

load_snapshot_unverified
small 109.81 µs (➖ 1.06x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.22 µs (➖ 1.00x faster) 2.41 µs (➖ 1.01x faster)
262144 9.52 µs (➖ 1.01x faster) 10.20 µs (➖ 1.04x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 9.03 µs (➖ 1.08x faster) 8.32 µs (➖ 1.17x faster)
262144 38.23 µs (➖ 1.00x slower)
8192 1.25 µs (➖ 1.03x slower) 1.22 µs (➖ 1.11x faster)
mshv3 / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 378.50 ns (➖ 1.06x slower)
vec_bytes 95.58 ns (➖ 1.01x slower)
797.14 µs (➖ 1.03x slower)

payload_allocation

slot_pool_segmented
65536 172.39 ns (➖ 1.00x faster)
262144 635.78 ns (➖ 1.00x slower)

sandboxes

create_initialized_and_drop
medium 70.44 ms (➖ 1.04x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
8.73 ns (➖ 1.01x faster) 9.13 ns (➖ 1.02x faster) 8.75 ns (➖ 1.00x slower)

snapshot_files

load_snapshot_unverified
small 53.83 µs (➖ 1.00x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.29 µs (➖ 1.01x slower) 2.25 µs (➖ 1.01x faster)
262144 7.92 µs (➖ 1.02x faster) 7.89 µs (➖ 1.00x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 7.43 µs (➖ 1.06x faster) 7.38 µs (➖ 1.05x faster)
262144 38.98 µs (➖ 1.07x faster)
8192 887.07 ns (➖ 1.03x faster) 885.46 ns (➖ 1.04x faster)
hyperv-ws2025 / amd (Windows) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 560.51 ns (➖ 1.01x slower)
vec_bytes 168.82 ns (➖ 1.00x slower)
7.26 ms (➖ 1.03x slower)

payload_allocation

slot_pool_segmented
65536 235.99 ns (➖ 1.03x slower)
262144 791.88 ns (➖ 1.14x faster)

sandboxes

create_initialized_and_drop
medium 13.60 ms (➖ 1.03x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
10.36 ns (➖ 1.00x faster) 10.07 ns (➖ 1.04x faster) 10.58 ns (➖ 1.02x slower)

snapshot_files

load_snapshot_unverified
small 811.98 µs (➖ 1.16x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.37 µs (➖ 1.01x slower) 2.36 µs (➖ 1.02x slower)
262144 9.13 µs (➖ 1.01x faster) 9.17 µs (➖ 1.00x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 9.05 µs (➖ 1.09x faster) 8.82 µs (➖ 1.02x faster)
262144 41.84 µs (➖ 1.07x slower)
8192 1.27 µs (➖ 1.02x faster) 1.26 µs (➖ 1.03x faster)
hyperv-ws2025 / intel (Windows) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 577.49 ns (➖ 1.02x faster)
vec_bytes 168.29 ns (➖ 1.02x slower)
3.14 ms (➖ 1.05x slower)

payload_allocation

slot_pool_segmented
65536 215.11 ns (➖ 1.01x slower)
262144 780.11 ns (➖ 1.01x slower)

sandboxes

create_initialized_and_drop
medium 15.04 ms (➖ 1.02x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
10.11 ns (➖ 1.05x faster) 10.97 ns (➖ 1.08x slower) 9.87 ns (➖ 1.22x faster)

snapshot_files

load_snapshot_unverified
small 581.79 µs (➖ 1.01x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.29 µs (➖ 1.04x faster) 2.28 µs (➖ 1.02x slower)
262144 7.95 µs (➖ 1.01x faster) 7.65 µs (➖ 1.01x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.35 µs (➖ 1.17x faster) 8.80 µs (➖ 1.04x slower)
262144 42.66 µs (➖ 1.23x faster)
8192 1.06 µs (➖ 1.01x slower) 1.03 µs (➖ 1.02x faster)

Reported by cargo ci bench-report --candidate run:46748284927 --config-file bench_report.toml.

@ludfjig

ludfjig commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@syntactically mind taking a look? I'd like to get this into upcoming release

This branch has not been deployed

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

Labels

A-api Related to the API or public interface A-perf Addresses performance C-enhancement For PRs adding features, improving functionality, docs, tests, etc. S-needs-review PR is ready for (re-)review

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Heuristic-based scratch zeroing strategy for snapshot restore

4 participants