Skip to content

fix(rust): cut retained runtime-install memory by ~99% - #2676

Open
mohamedmansour wants to merge 21 commits into
github:mainfrom
mohamedmansour:mohamedmansour-sdk-runtime-memory
Open

mohamedmansour wants to merge 21 commits into
github:mainfrom
mohamedmansour:mohamedmansour-sdk-runtime-memory

Conversation

@mohamedmansour

@mohamedmansour mohamedmansour commented Sep 15, 2026

Copy link
Copy Markdown

Problem: The Rust runtime installer repeatedly loaded entire native files into memory for extraction and comparison, even when the runtime was already installed. The buffers were freed, but the allocator retained the memory after installation. Fixes #2675.

Fix: Stream extraction and compare existing files in fixed-size chunks. Stage only missing or changed files, validate the archive, then replace files atomically. Matching files are reused without requiring a writable cache.

Proof: Matched SDK-only measurements on macOS arm64, runtime 1.0.85, five alternating pairs per case:

Retained physical memory Before After Reduction
Cold install 156.19 MiB 1.98 MiB 98.7%
Warm install 175.84 MiB 1.86 MiB 98.9%

All 68 installed files remain byte-identical. Regression tests cover corruption repair, archive integrity, concurrent installation and read-only caches. Native Windows/Linux validation remains outstanding.

mohamedmansour and others added 2 commits September 15, 2026 12:25
Compare runtime assets in bounded chunks, stage changed entries without whole-image buffers, and validate the archive before atomic publication. Preserve read-only warm caches, repair unreadable or corrupt files, and reject duplicate normalized destinations.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Exercise the public installer in isolated cold, warm, corrupt and truncated-cache processes. Record matched release-build memory and latency measurements, output identities, separate allocator diagnostics, and native-platform limitations.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@mohamedmansour
mohamedmansour requested a review from a team as a code owner September 15, 2026 19:28
Copilot AI balanced review requested due to automatic review settings September 15, 2026 19:28

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.

🟡 Changes recommended

Destination deduplication misses aliases on case-insensitive filesystems, allowing later entries to overwrite required runtime artifacts.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Bounds Rust bundled-runtime installation memory through streamed comparison, staged writes, and atomic per-file publication.

Changes:

  • Streams runtime extraction and comparison with bounded buffers.
  • Adds extensive installer regression tests.
  • Adds a reproducible macOS benchmark and documentation.
File summaries
File Description
rust/src/embeddedcli.rs Implements streaming installation and tests.
rust/README.md Documents installer behavior.
rust/examples/runtime_install.rs Adds installer probe.
rust/Cargo.toml Packages benchmark assets.
rust/benchmarks/runtime_install.py Adds measurement harness.
rust/benchmarks/runtime_install.md Documents results and reproduction.
Review details
  • Files reviewed: 6/6 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread rust/src/embeddedcli.rs Outdated
mohamedmansour and others added 3 commits September 15, 2026 12:57
Keep the small installer example and runtime behavior documentation, while retaining measurement evidence outside the repository.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Normalize separators and deduplicate case-insensitively before artifact selection. Reject non-portable archive names rather than approximating filesystem-specific Unicode, DOS short-name, or trailing-dot aliases. Cover cold, warm and repair installs through the real installer.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Remove the README additions and standalone measurement example. Preserve reproduction details and measurements in the pull request rather than shipping a benchmark surface.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@mohamedmansour
mohamedmansour requested a balanced review from Copilot September 15, 2026 20:24

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

Cross-platform filesystem and replacement behavior remains unverified natively on Windows and Linux.

Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@mohamedmansour mohamedmansour changed the title fix(rust): bound bundled runtime installation memory perf(rust): bound bundled runtime installation memory Sep 15, 2026
@stephentoub

Copy link
Copy Markdown
Collaborator

There is one P2 behavioral regression that should be fixed before merge.

On Unix, existing_runtime_file requires the installed file's entire 0o777 mode to exactly match the tar header. That breaks valid immutable warm caches: a common chmod -R a-w changes 0644 to 0444 and 0755 to 0555 while preserving contents and the wrapper's executable bits. These files are then treated as stale, and the installer tries to create staging files in the non-writable cache and fails. The previous implementation compared contents only and reused such a cache.

The new read-only-cache test only removes write permission from the directories, so it does not exercise this case. Please make warm validation ignore irrelevant write-bit differences (while still requiring the executable bits needed by the wrapper), and extend the test to remove write bits from the installed files as well.

I did not find another P0/P1/P2 issue. The streaming design and archive/publish hardening otherwise look directionally sound.

Generated by Copilot

Ignore write-bit differences when validating installed runtime files, while retaining read and execute permission checks. Cover read-only installed files and repair of each missing wrapper execute bit.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mohamedmansour

Copy link
Copy Markdown
Author

@stephentoub Accepted and fixed in 61cb61c: I reproduced the Permission denied failure from your report, changed Unix warm validation from 0o777 to 0o555 to ignore write-bit differences while retaining read/execute checks, and extended regression coverage to read-only installed files and each missing wrapper execute bit. The regression now passes, along with 486 non-replay tests, 25 default-feature installer tests, 30 doctests, formatting, and Clippy; full replay E2E validation remains blocked by local setup (initially missing tsx, then the macOS socket-path limit with project-local scratch paths).

Comment thread rust/src/embeddedcli.rs Outdated

@stephentoub stephentoub left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approved with the remaining permission-mode concern noted inline.

Generated by Copilot

Generate per-file SHA256, size and mode alongside the unchanged runtime archive. Verify valid warm caches without reading compressed bytes, and perform cold or repair extraction in one bounded forward traversal with manifest and gzip integrity checks before publication.

Use sha2 0.11 runtime-detected acceleration with a software fallback, preserving the existing miniz_oxide decompression backend. Cover generated output identity, zero archive reads, immutable cache reuse and mixed-cache repair failures.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@mohamedmansour mohamedmansour changed the title perf(rust): bound bundled runtime installation memory perf(rust): stream runtime installation and skip warm decompression Sep 16, 2026
mohamedmansour and others added 6 commits September 15, 2026 22:11
Verify readability through bounded hashing and executable access using effective process credentials rather than requiring every permission class to match the archive. Reuse locked rustix for a safe Unix access check and cover owner-only cache modes without re-extraction.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Compare runtime assets in bounded chunks, stage changed entries without whole-image buffers, and validate the archive before atomic publication. Preserve read-only warm caches, repair unreadable or corrupt files, and reject duplicate normalized destinations.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Exercise the public installer in isolated cold, warm, corrupt and truncated-cache processes. Record matched release-build memory and latency measurements, output identities, separate allocator diagnostics, and native-platform limitations.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Keep the small installer example and runtime behavior documentation, while retaining measurement evidence outside the repository.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Normalize separators and deduplicate case-insensitively before artifact selection. Reject non-portable archive names rather than approximating filesystem-specific Unicode, DOS short-name, or trailing-dot aliases. Cover cold, warm and repair installs through the real installer.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Remove the README additions and standalone measurement example. Preserve reproduction details and measurements in the pull request rather than shipping a benchmark surface.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
mohamedmansour and others added 3 commits September 15, 2026 22:16
Ignore write-bit differences when validating installed runtime files, while retaining read and execute permission checks. Cover read-only installed files and repair of each missing wrapper execute bit.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Generate per-file SHA256, size and mode alongside the unchanged runtime archive. Verify valid warm caches without reading compressed bytes, and perform cold or repair extraction in one bounded forward traversal with manifest and gzip integrity checks before publication.

Use sha2 0.11 runtime-detected acceleration with a software fallback, preserving the existing miniz_oxide decompression backend. Cover generated output identity, zero archive reads, immutable cache reuse and mixed-cache repair failures.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Verify readability through bounded hashing and executable access using effective process credentials rather than requiring every permission class to match the archive. Reuse locked rustix for a safe Unix access check and cover owner-only cache modes without re-extraction.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@mohamedmansour
mohamedmansour force-pushed the mohamedmansour-sdk-runtime-memory branch from 4c21738 to 523f7b5 Compare September 16, 2026 05:16
@mohamedmansour mohamedmansour changed the title perf(rust): stream runtime installation and skip warm decompression perf(rust): reduce warm-install memory by 99% and latency by 46% Sep 16, 2026
Remove the generated hash manifest and runtime sha2/rustix dependencies. Compare installed files directly against streamed archive entries with fixed-size buffers, and retain the original content-based warm-cache permission behavior.

Keep atomic staging, archive integrity checks and regression coverage for immutable native libraries and multi-chunk corruption. Restore Cargo.toml, Cargo.lock and the build generator exactly to the PR base.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@mohamedmansour mohamedmansour changed the title perf(rust): reduce warm-install memory by 99% and latency by 46% fix(rust): cut retained runtime-install memory by ~99% Sep 16, 2026
Comment thread rust/src/embeddedcli.rs
Check only the runtime wrapper with POSIX faccessat and effective credentials, preserving readable native libraries and owner-only executable caches. Repair inaccessible wrappers through the existing staged publish path and reject persistent execute denial before returning.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

@stephentoub stephentoub left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The remaining runtime-wrapper execute-access issue is correctly addressed, and I found no new P0/P1/P2 concerns in the update. Approving the code; the current-head workflows still need to be authorized and pass before merge.

Generated by Copilot

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rust bundled runtime installation retains large native-image buffers on cold and warm startup

3 participants