Skip to content

perf: release finished scanner scratch buffers - #95

Open
Kinfe123 wants to merge 1 commit into
mainfrom
perf/release-finished-scanner-buffers
Open

Kinfe123 wants to merge 1 commit into
mainfrom
perf/release-finished-scanner-buffers

Conversation

@Kinfe123

Copy link
Copy Markdown
Collaborator

Completed standalone scanners retain their Wasm input allocation and the largest patch-buffer capacity until disposal or garbage collection. Applications that keep finished scanners for their values or field queries continue to hold that scratch space.

Release the Wasm input buffer on finalization and shrink unused patch capacity after successful validation. The parser handle, final patches, values, and field queries remain usable, including repeated finish() calls and trailing-whitespace pushes. The runtime change is two Rust lines plus the regenerated embedded Wasm; no JavaScript API changes.

Added a memory regression test that fails on main and lifecycle tests for numeric completion patches, snapshot modes, repeated operations, and terminal errors. Added a reproducible bench:finished-memory command.

Memory validation

Compared with main at 3965515, retaining 32 completed scanners with 2,000 nested records each (219,790 bytes per input):

Input delivery Baseline median / p95 This change median / p95
4,096-character chunks 3.5625 / 3.5625 MiB 1.25 / 1.25 MiB
One 219,790-character chunk 156.625 / 156.625 MiB 10.0625 / 10.0625 MiB

Both live and immutable modes produced these results. The metric is Wasm linear-memory high-water allocation, not JS heap or process RSS. Pooled readers already dispose completed parsers; the retained-memory benefit applies to standalone scanners kept alive after finish().

Environment: Node 23.11.0, macOS arm64, Apple M1. Three alternating trials per implementation, snapshot mode, and delivery size, each in a fresh process with zero warmups. The p95 is the maximum of the three trials. The benchmark checks final values, field queries, and subsequent operations.

Validation

  • pnpm check: 199 JavaScript tests, 11 Rust tests, type/syntax checks, formatting and clippy.
  • pnpm pack:check.
  • pnpm test:browser with the documented installed-Chrome override.
  • pnpm bench:finished-memory /tmp/streamfold-finished-buffers-baseline-3965515/packages/core/src/index.js.
  • pnpm bench and pnpm bench:sdk.

Also combined this branch with #94 in an isolated test copy: all 123 core tests passed. The PRs remain independent changes based on main.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant