Skip to content

improvement(workflow-code): speed up stableStringify with byte-identical output - #10745

Merged
waleedlatif1 merged 2 commits into
stagingfrom
perf/workflow-code-stable-stringify
Dec 3, 2026
Merged

waleedlatif1 merged 2 commits into
stagingfrom
perf/workflow-code-stable-stringify

Conversation

@waleedlatif1

@waleedlatif1 waleedlatif1 commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • stableStringify (the hash primitive behind catalogHash, catalog compaction memo keys, and workflow hashes) now sorts Object.keys(...) with compareStrings and reads fields directly instead of sorting Object.entries tuples, and quotes printable-ASCII keys without a JSON.stringify call
  • Output is byte-identical for JSON data (plain objects and arrays without side-effecting getters): no global state and no cache, and anything outside printable ASCII or containing " or \ still goes through JSON.stringify
  • Block catalog hash is unchanged before and after (same digest under Node and Bun)

Measurements

Medians over 7 processes (5 warm iterations each), registry block catalog (513 blocks), Apple Silicon laptop. Node runs a bun build --target=node bundle; Bun runs the source directly.

Node before Node after Bun before Bun after
stableStringify(core) 80.8 ms 66.7 ms (-17%) 56.6 ms 48.9 ms (-14%)
buildBlockCatalog cold 500.6 ms 459.3 ms (-8%) 364.0 ms 335.9 ms (-8%)
buildBlockCatalog warm 129.9 ms 117.7 ms (-9%) 87.7 ms 81.6 ms (-7%)

A module-level memo of quoted keys measured about the same (Node build-cold 509.8 ms, warm 130.6 ms). I dropped it in favour of the stateless fast path, so no cache is needed. String concatenation instead of join was slower on Node.

Type of Change

  • Performance improvement

Testing

  • New seeded differential test in hash.test.ts: 20k generated values (escape and boundary keys, lone surrogates, __proto__, null-prototype objects, -0, NaN/±Infinity, holes, undefined, functions) must give the same bytes and the same TypeErrors as a frozen copy of the previous implementation
  • Red-checked by mutating the new code: unescaped surrogates, backslash, quote, the control-character boundary, locale sort, and keeping undefined fields each fail the test
  • hash.test.ts and catalog/compact.test.ts pass; biome check and check:utils are clean. The full suite runs in CI.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing (new tests pass the test-audit authoring gate)
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

…cal output

Sort Object.keys with compareStrings and read fields directly instead of Object.entries, preallocate the array item buffer, and quote printable-ASCII keys without calling JSON.stringify. A seeded differential test pins the output, and the errors it throws, to the previous implementation.
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

Greptile is currently unavailable, so this PR is merging on green CI plus a cubic review of the HEAD commit, with no unresolved threads.

@vercel

vercel Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docs Skipped Skipped Dec 3, 2026 10:21pm UTC

Request Review

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

Comment thread packages/workflow-code/src/hash.ts Resolved
Comment thread packages/workflow-code/src/hash.ts Resolved
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Turn on auto-fix | Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit 90b9042 into staging Dec 3, 2026
83 of 84 checks passed
@waleedlatif1
waleedlatif1 deleted the perf/workflow-code-stable-stringify branch December 3, 2026 22:55

This branch was previously deployed

1 inactive deployment
Preview — e5acac49 Deployed Dec 3, 2026 by vercel[bot]
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