Four native Rust optimizations to eliminate hot-path bottlenecks: 1. deriveState raw content (gsd_parser.rs, state.ts): - Added rawContent field to ParsedGsdFile in batch parser - Eliminates 43-line frontmatter re-serialization loop in state.ts - Batch cache now stores original file content directly 2. JSONL streaming parser (gsd_parser.rs, session-forensics.ts): - Added parseJsonlTail() — reads from file tail with constant memory - Handles arbitrary file sizes (no more 10MB OOM risk) - synthesizeCrashRecovery and readLastActivityLog use native first 3. Native directory tree index (gsd_parser.rs, paths.ts): - Added scanGsdTree() — walks .gsd/ tree once, returns all entries - paths.ts builds lookup map from native scan - cachedReaddirWithTypes/cachedReaddir check native cache first - Eliminates 20-50 readdirSync calls per dispatch 4. Native plan/summary parsers (gsd_parser.rs, files.ts): - Added parsePlanFile() — parses tasks, must-haves, estimates - Added parseSummaryFile() — parses frontmatter, sections, files - files.ts calls native first, falls back to JS regex parsers - 3-5x faster per file, ~20 files per deriveState All optimizations follow the established pattern: native-first with JS fallback when native module unavailable. |
||
|---|---|---|
| .. | ||
| .cargo | ||
| crates | ||
| npm | ||
| scripts | ||
| .gitignore | ||
| .npmignore | ||
| Cargo.lock | ||
| Cargo.toml | ||
| README.md | ||
GSD Native Engine
Rust N-API addon providing high-performance native modules for GSD.
Architecture
JS (packages/native) -> N-API -> Rust crates
├── engine/ (N-API bindings, cdylib)
└── grep/ (ripgrep internals, pure Rust lib)
Inspired by Oh My Pi's pi-natives, adapted for GSD's Node.js runtime.
Prerequisites
- Rust (stable, 1.70+): https://rustup.rs
- Node.js (20.6+)
Build
# Release build (optimized)
npm run build:native
# Debug build (fast compile, no optimizations)
npm run build:native:dev
The build script compiles the Rust code and copies the .node shared library to native/addon/.
Test
# Rust unit tests
cd native && cargo test
# Node.js integration tests
npm run test:native
Modules
grep
Ripgrep-backed regex search using the grep-regex, grep-searcher, and grep-matcher crates.
Functions:
search(content, options)— Search in-memory Buffer/Uint8Array contentgrep(options)— Search files on disk with glob filtering and .gitignore support
TypeScript usage:
import { grep, searchContent } from "@gsd/native";
// Search files
const result = grep({
pattern: "TODO",
path: "./src",
glob: "*.ts",
ignoreCase: true,
maxCount: 100,
});
// Search content
const contentResult = searchContent(Buffer.from(fileContent), {
pattern: "function\\s+\\w+",
contextBefore: 2,
contextAfter: 2,
});
Adding New Modules
- Create a new crate in
native/crates/(pure Rust library) - Add N-API bindings in
native/crates/engine/src/ - Add TypeScript wrapper in
packages/native/src/ - Add the crate to
engine/Cargo.tomldependencies