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)

@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 from b6b7862 to 6f38ece Compare November 25, 2026 18:58
Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
@ludfjig
ludfjig force-pushed the ll/memory-reset-strategy branch from 6f38ece to 2c579b0 Compare November 25, 2026 19:03
@hyperlight-gh-bot

Copy link
Copy Markdown

Benchmark Results

Measured commit: 2c579b0ef97c
Baseline commit: c46735cd076c

kvm / amd (Linux) (❌ 1.67x)

Top regressions

  • payload_allocation/slot_pool_segmented/262144 — ❌ 1.67x slower

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 297.97 ns (➖ 1.10x slower)
vec_bytes 78.16 ns (➖ 1.00x faster)
371.79 µs (➖ 1.00x slower)

payload_allocation

slot_pool_segmented
65536 162.52 ns (➖ 1.13x slower)
262144 878.93 ns (❌ 1.67x slower)

sandboxes

create_initialized_and_drop
medium 13.56 ms (➖ 1.01x faster)

slot_pool

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

snapshot_files

load_snapshot_unverified
small 108.05 µs (➖ 1.02x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 1.93 µs (➖ 1.08x faster) 2.05 µs (➖ 1.07x slower)
262144 7.04 µs (➖ 1.01x slower) 7.08 µs (➖ 1.03x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 6.08 µs (➖ 1.03x faster) 6.16 µs (➖ 1.07x faster)
262144 27.14 µs (➖ 1.01x faster)
8192 1.02 µs (➖ 1.02x faster) 1.06 µs (➖ 1.05x slower)
kvm / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 264.21 ns (➖ 1.05x faster)
vec_bytes 80.97 ns (➖ 1.03x faster)
659.22 µs (➖ 1.02x faster)

payload_allocation

slot_pool_segmented
65536 134.74 ns (➖ 1.02x faster)
262144 497.69 ns (➖ 1.02x faster)

sandboxes

create_initialized_and_drop
medium 15.29 ms (➖ 1.00x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
6.91 ns (➖ 1.01x faster) 6.89 ns (➖ 1.02x faster) 6.89 ns (➖ 1.02x faster)

snapshot_files

load_snapshot_unverified
small 54.21 µs (➖ 1.01x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.13 µs (➖ 1.01x faster) 2.11 µs (➖ 1.03x faster)
262144 7.49 µs (➖ 1.02x faster) 7.46 µs (➖ 1.02x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 7.01 µs (➖ 1.03x faster) 7.01 µs (➖ 1.04x faster)
262144 28.99 µs (➖ 1.03x faster)
8192 700.07 ns (➖ 1.02x faster) 720.65 ns (➖ 1.02x faster)
mshv3 / amd (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 381.64 ns (➖ 1.08x slower)
vec_bytes 110.78 ns (➖ 1.14x slower)
265.14 µs (➖ 1.00x slower)

payload_allocation

slot_pool_segmented
65536 253.73 ns (➖ 1.31x slower)
262144 734.40 ns (➖ 1.00x slower)

sandboxes

create_initialized_and_drop
medium 62.08 ms (➖ 1.09x faster)

slot_pool

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

snapshot_files

load_snapshot_unverified
small 97.08 µs (➖ 1.00x slower)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.24 µs (➖ 1.01x slower) 2.58 µs (➖ 1.06x slower)
262144 9.77 µs (➖ 1.04x slower) 11.30 µs (➖ 1.01x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.15 µs (➖ 1.19x faster) 8.56 µs (➖ 1.13x faster)
262144 36.24 µs (➖ 1.03x faster)
8192 1.25 µs (➖ 1.05x slower) 1.23 µs (➖ 1.01x faster)
mshv3 / intel (Linux) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 359.23 ns (➖ 1.08x faster)
vec_bytes 92.67 ns (➖ 1.09x slower)
643.85 µs (➖ 1.01x faster)

payload_allocation

slot_pool_segmented
65536 171.50 ns (➖ 1.00x faster)
262144 630.48 ns (➖ 1.01x faster)

sandboxes

create_initialized_and_drop
medium 74.12 ms (➖ 1.01x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
8.68 ns (➖ 1.00x slower) 11.71 ns (➖ 1.29x slower) 8.84 ns (➖ 1.09x faster)

snapshot_files

load_snapshot_unverified
small 51.58 µs (➖ 1.02x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.28 µs (➖ 1.02x slower) 2.26 µs (➖ 1.00x slower)
262144 8.04 µs (➖ 1.00x slower) 7.91 µs (➖ 1.00x faster)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 7.36 µs (➖ 1.01x faster) 7.48 µs (➖ 1.05x faster)
262144 38.26 µs (➖ 1.04x faster)
8192 890.87 ns (➖ 1.01x faster) 918.27 ns (➖ 1.01x slower)
hyperv-ws2025 / amd (Windows) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 554.10 ns (➖ 1.02x faster)
vec_bytes 165.80 ns (➖ 1.02x slower)
1.96 ms (➖ 1.04x slower)

payload_allocation

slot_pool_segmented
65536 227.72 ns (➖ 1.03x faster)
262144 793.56 ns (➖ 1.03x faster)

sandboxes

create_initialized_and_drop
medium 13.40 ms (➖ 1.02x slower)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
10.35 ns (➖ 1.01x faster) 11.25 ns (➖ 1.10x slower) 10.36 ns (➖ 1.01x faster)

snapshot_files

load_snapshot_unverified
small 685.30 µs (➖ 1.08x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.33 µs (➖ 1.01x faster) 2.34 µs (➖ 1.00x slower)
262144 9.14 µs (➖ 1.02x faster) 9.11 µs (➖ 1.01x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.73 µs (➖ 1.01x faster) 8.99 µs (➖ 1.01x slower)
262144 41.23 µs (➖ 1.05x slower)
8192 1.26 µs (➖ 1.00x faster) 1.27 µs (➖ 1.02x slower)
hyperv-ws2025 / intel (Windows) (➖ stable)

No benchmark improved or regressed.

Benchmark Results

function_call_codec

encode_control decode_vec_bytes_copy
byte_chunks 582.52 ns (➖ 1.01x faster)
vec_bytes 167.33 ns (➖ 1.09x faster)
3.09 ms (➖ 1.01x faster)

payload_allocation

slot_pool_segmented
65536 211.83 ns (➖ 1.00x faster)
262144 765.51 ns (➖ 1.01x faster)

sandboxes

create_initialized_and_drop
medium 14.45 ms (➖ 1.06x faster)

slot_pool

alloc_dealloc_4096 alloc_dealloc_1500 alloc_dealloc_128
10.16 ns (➖ 1.01x slower) 9.77 ns (➖ 1.00x slower) 9.89 ns (➖ 1.00x faster)

snapshot_files

load_snapshot_unverified
small 591.24 µs (➖ 1.01x faster)

virtq_readonly

slot_pool_segmented slot_pool_segmented_fragmented
65536 2.31 µs (➖ 1.01x slower) 2.26 µs (➖ 1.03x faster)
262144 7.82 µs (➖ 1.01x slower) 8.01 µs (➖ 1.02x slower)

virtq_readwrite

slot_pool_segmented slot_pool_segmented_fragmented
65536 8.45 µs (➖ 1.00x slower) 8.53 µs (➖ 1.04x slower)
262144 41.56 µs (➖ 1.28x faster)
8192 1.05 µs (➖ 1.14x faster) 1.18 µs (➖ 1.11x slower)

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

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

None yet

Development

Successfully merging this pull request may close these issues.

2 participants