Skip to content

perf(core): tighten resolve() sorting to drop the quadratic hot paths#89

Merged
SingleSourceStudios merged 2 commits into
mainfrom
perf/dag-sort-tightening
May 27, 2026
Merged

perf(core): tighten resolve() sorting to drop the quadratic hot paths#89
SingleSourceStudios merged 2 commits into
mainfrom
perf/dag-sort-tightening

Conversation

@SingleSourceStudios
Copy link
Copy Markdown
Collaborator

@SingleSourceStudios SingleSourceStudios commented May 27, 2026

Issue #46, Candidate 3 (PR 4 of 4, the final algorithmic fix). Follows the scaffold (#53), compiler (#87), and expression cache (#88).

Summary

resolve() (Kahn's topological sort) had two quadratic hot paths:

  1. queue.sort() on every pop, O(V^2 log V).
  2. A per-depth-level sorted.filter(...).sort(), O(V*D), which is quadratic on a linear chain.

The fix:

  • Walk the alphabetically-seeded queue with a FIFO index pointer instead of re-sorting on every pop, and drop the per-node dependents sort.
  • Build the levels in a single O(V) pass over the pre-sorted names array, which drops each node into its depth bucket already in alphabetical order, instead of filter-and-sort per level.

Determinism (the hard constraint)

Output (levels, order, and error shapes) is byte-identical. The output is a pure function of the depth map and the per-level alphabetical sort. depth is the longest path from a root and is dequeue-order-invariant: a node is enqueued only after every predecessor has been processed (the in-degree gate), so its depth is final when read, regardless of queue discipline. The dequeue order only ever affected the internal sorted traversal array, which feeds nothing observable except the unreachable error branch, and that branch is provably empty on the no-cycle path (every node in sorted is reachable from a root).

Determinism-baseline test approach

A golden-master baseline: resolve() output was captured from the pre-optimisation implementation on main across linear chain, diamond, wide fan-out, multiple roots, disconnected components, cross-level depth, reverse-name ordering, an alphabetical-vs-topological conflict shape, and the cycle / self-dependency / missing-dependency error shapes. The optimised resolve() reproduces every snapshot byte-for-byte (toEqual against embedded literals). The baseline was verified to FAIL under a perturbed level ordering, so it guards determinism rather than passing vacuously.

Proof (npm run bench, Candidate-3 assertion: resolve on a 1000-step linear chain)

best of 5 margin vs 800ms threshold
before (main) ~21.2ms ~38x
after (this PR) ~1.19ms ~670x

~18x faster; the assertion passes with a far wider margin. Thresholds left unchanged per the issue's calibration guidance.

Scope

packages/core/dag.ts + its tests only.

Test Plan

  • npm run build:core && npm run build
  • npm test (511 passed)
  • npm run typecheck
  • npm run lint (no new errors; 5 pre-existing out-of-scope errors unchanged)
  • node spec/fixtures/run-fixtures.mjs (29 passed)
  • npm run bench (3 passed, Candidate-3 margin widened)

Closes #46 (final of the 4-PR sequence: #53 scaffold, #87 compiler, #88 expression cache, this).

🤖 Generated with Claude Code


Summary by cubic

Tightened resolve() to remove two quadratic hot paths, delivering ~18x faster DAG sorting with byte-identical output and alphabetical per-level ordering. Final algorithmic fix for issue #46.

  • Refactors
    • Walk the alphabetically seeded queue with a FIFO index (no queue.sort() on each pop) and drop per-node dependents sorting.
    • Build levels in one O(V) pass by bucketing pre-sorted names into depth slots, preserving alphabetical order within each level.
    • Determinism preserved exactly; added golden-master tests across varied DAG shapes and error cases (including wide sibling ordering). Bench: 21.2ms → 1.19ms on a 1000-step chain (~18x). Thresholds unchanged.

Written for commit 6f7f665. Summary will update on new commits. Review in cubic

resolve() re-sorted the Kahn queue on every pop (O(V^2 log V)) and rebuilt each
depth level with a filter-and-sort over the full node array (O(V*D), quadratic
on a linear chain). This is Candidate 3 of issue #46.

The output (levels and order) is derived only from the depth map and a per-level
alphabetical sort: the depth map is the longest path from a root and is
order-invariant (a node is dequeued only after every predecessor is processed,
so its depth is final when read), and each level is sorted alphabetically. The
dequeue order therefore has no effect on output.

So: walk the alphabetically-seeded queue with a FIFO index pointer instead of
re-sorting on every pop, drop the per-node dependents sort, and build the levels
in a single O(V) pass over the pre-sorted names array (which drops each node into
its depth bucket already in alphabetical order) instead of filter-and-sort per
level.

Determinism is preserved exactly. A golden-master baseline captures the
pre-optimisation resolve() output across linear chain, diamond, wide fan-out,
multiple roots, disconnected components, cross-level depth, reverse-name
ordering, and the cycle / self-dependency / missing-dependency error shapes; the
optimised resolve() reproduces every snapshot byte-for-byte. The baseline was
verified to fail under a perturbed level ordering, so it genuinely guards
determinism rather than passing vacuously.

Proof (npm run bench, resolve on a 1000-step linear chain, this machine): best
21.2ms before, 1.19ms after (~18x), widening the margin against the 800ms
assertion from ~38x to ~670x. Thresholds left unchanged.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 27, 2026

Warning

Review limit reached

@SingleSourceStudios, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 15 minutes and 7 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5cd813c5-e27d-448f-b193-0f0af845aa8a

📥 Commits

Reviewing files that changed from the base of the PR and between c83c8fb and 6f7f665.

📒 Files selected for processing (2)
  • packages/core/dag.test.ts
  • packages/core/dag.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/dag-sort-tightening

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

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

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

Re-trigger cubic

@SingleSourceStudios SingleSourceStudios enabled auto-merge (squash) May 27, 2026 09:04
@SingleSourceStudios SingleSourceStudios merged commit aa60bea into main May 27, 2026
4 checks passed
@SingleSourceStudios SingleSourceStudios deleted the perf/dag-sort-tightening branch May 27, 2026 09:04
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.

perf: missing scaling assertions on compiler, expression engine, and DAG resolver — three regression risks

1 participant