[FEATURE]: Optional native Rust FFI offload (#native) for memory-heavy and CPU-hot paths in @opencode/core
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:
- Compact
ModelsStorearena & lazy snapshot loading (models-dev): avoids pinning the 9.8 MBmodels-dev/snapshot.txtstring in JS module memory and supportsopenCatalogFile/getProviderModelbacked by a contiguous Rust byte arena (~12 MB RSS delta vs ~107 MB JS heap objects). - Compact
FileIndexarena (filesystem/search.ts): replaces tens of thousands of JSfuzzysort.Preparedobjects with a contiguous byte arena + 32-bit char-mask prefilter (nativeRustLayer, useful especially whenfffis disabled, unavailable, on Android, non-VCS roots, or home directories). - In-process directory/content/skill scanning (
ripgrep.ts,skill-scan.ts): skipsrgsubprocess spawns for local environments usingignore+grep-searcher. - Native
ShellScan&tree-sitterCodeAst(ast/index.ts): faster AST extraction & shell command scanning. - Diff/patch offload (
countPatch, LCSalignLines) &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 MBfile-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 MBscan-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 MBcodebase-ast-dir: 1.78x (176.9 ms → 99.7 ms), Peak RSS 207.21 MB → 180.27 MBdiff-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)
packages/native-rscrate skeleton +#nativeloader +diff_ops/malloc_trim- In-process file/content/skill scanning (
ripgrep.ts,skill-scan.ts) - Arena
FileIndex(filesystem/search.ts) models-devlazy snapshot + nativeModelsStorearenatree-sitterCodeAstmodule- Native
ShellScanport - Clean-environment benchmark harness (
bench/,packages/core/bench/)
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
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 |