Skip to content

perf(kernel): replace module-level WeakMaps with intrusive symbol properties; add trimmed benchmark stats#79

Open
Copilot wants to merge 3 commits into
mainfrom
copilot/reduce-kernel-allocations
Open

perf(kernel): replace module-level WeakMaps with intrusive symbol properties; add trimmed benchmark stats#79
Copilot wants to merge 3 commits into
mainfrom
copilot/reduce-kernel-allocations

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 30, 2026

  • Add --trim N flag to perf-stats.ts
  • Run 20 baseline (HEAD) iterations with --trim 3perf-stats-baseline.json
  • Run 20 claude-branch (claude/optimize-allocations-cW1Jm @ d42fb15) iterations → perf-stats-claude-branch-v2.json
  • Compare and report results

Results: baseline vs updated claude/optimize-allocations-cW1Jm (d42fb15)

20 runs, trim 3 each end, n=14

Benchmark Baseline Claude branch Δ Significant?
create rows (1k) 61.1ms ±0.3 61.4ms ±0.8 +0.6% ~noise
replace all rows 71.9ms ±1.0 72.1ms ±0.8 +0.3% ~noise
partial update 93.1ms ±3.1 91.7ms ±2.4 −1.5% ~noise
select row 8.4ms ±0.6 8.2ms ±0.7 −2.6% ~noise
swap rows 27.0ms ±0.4 27.0ms ±0.5 +0.1% ~noise
create many (10k) 846.2ms ±6.8 849.4ms ±5.1 +0.4% ~noise
append (1k+1k) 66.8ms ±4.3 65.6ms ±5.2 −1.8% ~noise
clear rows 45.8ms ±1.5 46.2ms ±1.1 +0.8% ~noise
TOTAL 1220.1ms ±8.9 1221.8ms ±7.7 +0.1% ~noise
Weighted 666.4 667.2 +0.1%

Verdict: neutral / safe to merge. Every benchmark is within noise. The revert of the version-tagged elementCache fully resolved the create-many-10k regression (+6.7% → +0.4% noise). The remaining structural refactors (combined For loops, consolidated node lookups, shared array mutators, hoisted Array.isArray, copyArrayInto) are all performance-neutral at this scale.

Copilot AI and others added 3 commits April 30, 2026 22:13
…hod closures

Agent-Logs-Url: https://github.com/commoncurriculum/supergrain/sessions/4e14a25c-2299-4dc9-8bb3-750006c22799

Co-authored-by: scottmessinger <100121+scottmessinger@users.noreply.github.com>
…perties on targets

Agent-Logs-Url: https://github.com/commoncurriculum/supergrain/sessions/4e14a25c-2299-4dc9-8bb3-750006c22799

Co-authored-by: scottmessinger <100121+scottmessinger@users.noreply.github.com>
… vs optimized)

Agent-Logs-Url: https://github.com/commoncurriculum/supergrain/sessions/b2961eb9-de52-469f-8c59-8eaa8db08492

Co-authored-by: scottmessinger <100121+scottmessinger@users.noreply.github.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes @supergrain/kernel hot paths by replacing several module-level WeakMap caches with intrusive, non-enumerable symbol properties on the proxied targets, and updates the js-krauset benchmark tooling to support trimmed statistics and Node compatibility.

Changes:

  • Store $PROXY and per-array $MUTATORS caches directly on raw targets (with WeakMap fallback for sealed/non-extensible objects).
  • Refactor reactive Map/Set implementations to use intrusive proxy caching and stable pre-created method wrappers.
  • Update js-krauset perf scripts for Node compatibility and add --trim N to compute trimmed stats; adjust Vite aliases.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
packages/kernel/src/read.ts Adds $MUTATORS intrusive per-array wrapper cache; switches proxy caching to $PROXY with WeakMap fallback.
packages/kernel/src/core.ts Introduces $MUTATORS well-known symbol.
packages/kernel/src/collections.ts Removes dead keySignals WeakMap; adds $PROXY intrusive caching for Map/Set and pre-created stable method wrappers.
packages/js-krauset/vite.config.ts Adds/adjusts alias ordering for kernel subpaths (/react, /internal).
packages/js-krauset/perf-stats.ts Adds --trim support and Node-compatible dirname handling.
packages/js-krauset/perf-compare.ts Fixes Node dirname handling and prints effective sample size.
packages/js-krauset/package.json Switches perf scripts to npx tsx (but currently without declaring tsx); file formatting changed.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 8 to +13
const name = process.argv[2];
const runCount = parseInt(process.argv[3] || "15", 10);
// --trim N: drop N lowest and N highest values per metric before computing
// stats. Requires runCount > 2*N. Default 0 (no trimming).
const trimArg = process.argv.indexOf("--trim");
const trimCount = trimArg !== -1 ? parseInt(process.argv[trimArg + 1] || "0", 10) : 0;
Comment on lines +10 to +13
"test:perf": "pnpm build-prod && vitest run --config vitest.dist.config.ts src/perf.test.ts --reporter=verbose 2>&1 | tee perf-results.txt",
"perf:stats": "npx tsx perf-stats.ts",
"perf:compare": "npx tsx perf-compare.ts",
"perf:profile": "pnpm build-prod && PROFILE=1 vitest run --config vitest.dist.config.ts src/perf.test.ts",
Comment on lines +2 to +39
"name": "js-framework-benchmark-react-supergrain",
"version": "1.0.0",
"private": true,
"scripts": {
"dev": "vite",
"build-prod": "vite build",
"typecheck": "tsc --noEmit",
"test": "pnpm build-prod && vitest run --config vitest.dist.config.ts src/dist.test.ts",
"test:perf": "pnpm build-prod && vitest run --config vitest.dist.config.ts src/perf.test.ts --reporter=verbose 2>&1 | tee perf-results.txt",
"perf:stats": "npx tsx perf-stats.ts",
"perf:compare": "npx tsx perf-compare.ts",
"perf:profile": "pnpm build-prod && PROFILE=1 vitest run --config vitest.dist.config.ts src/perf.test.ts",
"perf:analyze": "node perf-profile.ts"
},
"dependencies": {
"@supergrain/kernel": "workspace:*",
"react": "19.2.4",
"react-dom": "19.2.4"
},
"devDependencies": {
"@rollup/plugin-strip": "^3.0.4",
"@types/node": "^22.18.3",
"@types/ramda": "^0.31.1",
"@types/react": "^19.1.13",
"@types/react-dom": "^19.1.9",
"@vitejs/plugin-react": "^4.7.0",
"@vitest/browser": "4.1.0",
"playwright": "^1.55.0",
"ramda": "^0.32.0",
"typescript": "^5.9.2",
"vite": "^7.1.5",
"vite-plugin-dts": "^4.5.4",
"vitest": "4.1.0"
},
"js-framework-benchmark": {
"frameworkVersionFromPackage": "react",
"frameworkHomeURL": "https://reactjs.org/"
}
Copilot AI requested a review from scottmessinger May 1, 2026 01:15
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.

3 participants