Skip to content

perf(runtime): drain nextTick queue with VecDeque::pop_front (was O(n²))#6126

Merged
proggeramlug merged 1 commit into
mainfrom
fix/6084-nexttick-vecdeque
Jul 8, 2026
Merged

perf(runtime): drain nextTick queue with VecDeque::pop_front (was O(n²))#6126
proggeramlug merged 1 commit into
mainfrom
fix/6084-nexttick-vecdeque

Conversation

@proggeramlug

@proggeramlug proggeramlug commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

One of the #6084 server-scalability quadratics.

Problem

QUEUED_MICROTASKS (the process.nextTick queue) was a Vec drained with queue.remove(0), which is O(n) per job — so draining a burst of n nextTick jobs (common under Node stream / 'readable' scheduling) memmoves ~n²/2 entries.

Fix

Use VecDeque with push_back / pop_front for O(1) drain — the same structure the promise TASK_QUEUE already uses.

Behavior

Unchanged: FIFO order and same-drain visibility of jobs enqueued during the drain. Verified vs node --experimental-strip-types: nextTick ordering (1,2,3 incl. a job queued inside a job) and a 1000-job burst both match.

Summary by CodeRabbit

  • Performance
    • Improved microtask handling for queueMicrotask and process.nextTick, reducing queue draining overhead and making scheduling more efficient.
  • Bug Fixes
    • Smoothed out queue processing behavior by using a more efficient first-in, first-out approach.

… O(n²))

QUEUED_MICROTASKS was a Vec drained with `remove(0)` — O(n) per job, so a burst
of n queued nextTick jobs (common under Node stream / 'readable' scheduling)
memmoved ~n²/2 entries. Switch to VecDeque + push_back/pop_front for O(1) drain.
FIFO order and same-drain visibility of jobs enqueued during the drain are
unchanged (verified vs node: ordering + a 1000-job burst). #6084.
@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: c3f4e56d-130b-4839-b100-90d42cafdd8f

📥 Commits

Reviewing files that changed from the base of the PR and between c78c20b and afc4fd5.

📒 Files selected for processing (1)
  • crates/perry-runtime/src/builtins/globals.rs

📝 Walkthrough

Walkthrough

The internal microtask queue backing queueMicrotask/process.nextTick is changed from a Vec to a VecDeque. Enqueue operations use push_back, dequeue operations use pop_front instead of remove(0), and related unit tests are updated accordingly.

Changes

Microtask Queue VecDeque Migration

Layer / File(s) Summary
Queue type and enqueue/dequeue implementation
crates/perry-runtime/src/builtins/globals.rs
QUEUED_MICROTASKS is redefined as RefCell<VecDeque<QueuedMicrotask>>, enqueue uses push_back, and draining uses pop_front instead of remove(0).
Test updates for VecDeque
crates/perry-runtime/src/builtins/globals.rs
Unit tests seed the queue with push_back() and inspect the front element with front() instead of push()/first().

Estimated code review effort: 1 (Trivial) | ~5 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the runtime performance change to the nextTick queue and matches the implemented VecDeque drain fix.
Description check ✅ Passed The description covers the problem, fix, behavior, and verification, though it does not follow the template's explicit section headings.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/6084-nexttick-vecdeque

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.

@proggeramlug proggeramlug merged commit 04b8170 into main Jul 8, 2026
23 of 24 checks passed
@proggeramlug proggeramlug deleted the fix/6084-nexttick-vecdeque branch July 8, 2026 12:32
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