opencode icon indicating copy to clipboard operation
opencode copied to clipboard

[FEATURE]: Optional native Rust FFI offload (#native) for memory-heavy and CPU-hot paths in @opencode/core

Open Baran3575 opened this issue 2 days ago • 2 comments

Feature hasn't been suggested before.

  • [x] I have verified this feature I'm about to request hasn't been suggested before.

Describe the enhancement you want to request

Follow-up from #67118 (@opencodejr suggestion to open an issue for maintainer alignment before splitting into smaller focused PRs).

Summary

Add an optional, zero-required-runtime-dependency Rust FFI crate (packages/native-rs, loaded via #native in @opencode/core with automatic JS/Effect fallbacks via OPENCODE_DISABLE_NATIVE=1, Node/workerd stubs, or missing shared library) to reduce JS heap retention and speed up CPU/IO hot paths in @opencode/core:

  1. Compact ModelsStore arena & lazy snapshot loading (models-dev): avoids pinning the 9.8 MB models-dev/snapshot.txt string in JS module memory and supports openCatalogFile / getProviderModel backed by a contiguous Rust byte arena (~12 MB RSS delta vs ~107 MB JS heap objects).
  2. Compact FileIndex arena (filesystem/search.ts): replaces tens of thousands of JS fuzzysort.Prepared objects with a contiguous byte arena + 32-bit char-mask prefilter (nativeRustLayer, useful especially when fff is disabled, unavailable, on Android, non-VCS roots, or home directories).
  3. In-process directory/content/skill scanning (ripgrep.ts, skill-scan.ts): skips rg subprocess spawns for local environments using ignore + grep-searcher.
  4. Native ShellScan & tree-sitter CodeAst (ast/index.ts): faster AST extraction & shell command scanning.
  5. Diff/patch offload (countPatch, LCS alignLines) & malloc_trim(0).

Clean-Environment Benchmark (bun run bench, Linux/Android aarch64, Bun 1.4.5)

  • file-index-ram: 9.84x (72.6 ms → 7.4 ms), Peak RSS 204.92 MB → 182.44 MB
  • file-index-20k-ram: 6.73x (238.3 ms → 35.4 ms), Peak RSS 285.38 MB → 176.63 MB (deltaRSS: 112.03 MB → 1.81 MB)
  • models-dev-ram: 4.16x (347.9 ms → 83.5 ms), Peak RSS 275.72 MB → 193.32 MB, JS Heap 70.36 MB → 30.85 MB
  • scan-find: 5.14x (721.5 ms → 140.4 ms)
  • scan-grep: 3.32x (1299.3 ms → 391.0 ms)
  • shell-ast: 2.10x (41.0 ms → 19.6 ms)
  • code-ast: 1.72x (106.0 ms → 61.8 ms), Peak RSS 181.72 MB → 177.02 MB
  • codebase-ast-dir: 1.78x (176.9 ms → 99.7 ms), Peak RSS 207.21 MB → 180.27 MB
  • diff-patch: 2.41x (77.6 ms → 32.2 ms), Peak RSS 206.08 MB → 187.10 MB
  • Full log: https://mclo.gs/gUE88Xy

Proposed Split (7 Focused Stacked PRs)

  1. packages/native-rs crate skeleton + #native loader + diff_ops/malloc_trim
  2. In-process file/content/skill scanning (ripgrep.ts, skill-scan.ts)
  3. Arena FileIndex (filesystem/search.ts)
  4. models-dev lazy snapshot + native ModelsStore arena
  5. tree-sitter CodeAst module
  6. Native ShellScan port
  7. Clean-environment benchmark harness (bench/, packages/core/bench/)

Baran3575 avatar Nov 29 '26 11:11 Baran3575

Thanks for following up on #67118 with an issue, @Baran3575, and for the detailed benchmarks and proposed split.

To restate the request: you're proposing an optional native Rust crate (packages/native-rs, loaded through #native with JS fallbacks) to take memory-heavy and CPU-heavy work in @opencode/core off the JS side: the file index, the models-dev snapshot, scanning, the shell/tree-sitter AST and diff/patch. You'd deliver it as seven stacked PRs.

Maintainers will consider whether they want a native Rust dependency in core and, if so, how they'd like to take the work. I can't promise anything about the outcome.

Related:

  • #67118: the original combined PR, closed so it could be split into smaller ones
  • #20695: the earlier Memory Megathread (closed), which collected memory reports and heap snapshots

opencodejr[bot] avatar Nov 29 '26 11:11 opencodejr[bot]

Stacked PRs opened (first 3 open due to the 3 concurrent open-PR limit for contributors; branches 4–7 are pushed on the fork and ready once earlier PRs merge):

# PR / Status Scope Isolated Commit / Diff
1 #67120 (Open) feat/native-1-skeleton-diff 92e9f7c7
2 #67121 (Open, builds on #67120) feat/native-2-scanner f8a18681
3 #67122 (Open, builds on #67121) feat/native-3-file-index 2d759ec4
4 Queued (fork branch ready; 3 open-PR limit) Baran3575:feat/native-4-models-store 1404219a
5 Queued (fork branch ready) Baran3575:feat/native-5-code-ast efdc341d
6 Queued (fork branch ready) Baran3575:feat/native-6-shell-scan cddff09a
7 Queued (fork branch ready) Baran3575:feat/native-7-bench 15caff46

Baran3575 avatar Nov 29 '26 11:11 Baran3575