Skip to content

Remove ready_queue_next - #206

Merged
lalinsky merged 1 commit into
mainfrom
remove-next-queue
Jan 16, 2026
Merged

lalinsky merged 1 commit into
mainfrom
remove-next-queue

Conversation

@lalinsky

Copy link
Copy Markdown
Owner

Increase of trying to invent the concept of a tick, we will just run tasks that are ready, until we either reach the end, or we already ran 61 of them.

This makes is easier to implement work-stealing later on, it also improves CPU-bound tasks. It might slightly increase latency for I/O tasks, but if there are I/O tasks, they will simple clear the ready queue and do poll more often.

The worse case are mixed workloads, I/O latency could increase, but we will add more control over this and work-stealing will also help with balancing the load.

Increase of trying to invent the concept of a tick, we will just run tasks
that are ready, until we either reach the end, or we already ran 61 of them.

This makes is easier to implement work-stealing later on, it also improves
CPU-bound tasks. It might slightly increase latency for I/O tasks, but if there are
I/O tasks, they will simple clear the ready queue and do poll more often.

The worse case are mixed workloads, I/O latency could increase, but we will
add more control over this and work-stealing will also help with balancing
the load.
@coderabbitai

coderabbitai Bot commented Jan 16, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Simplified task scheduler by eliminating the next_ready_queue field from Executor and consolidating the code path for yielded coroutines. Rerouted tasks directly to the ready queue or LIFO slot based on yield status and slot availability, reducing queue complexity.

Changes

Cohort / File(s) Summary
Executor scheduling simplification
src/runtime.zig
Removed next_ready_queue field from Executor struct. Eliminated the code path that moved yielded coroutines from next_ready_queue back to ready queue. Updated scheduleTaskLocal to route tasks directly to ready_queue (on yield) or lifo_slot (when enabled and not yielding). Updated comments to clarify new routing behavior.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

One queue removed, the scheduler leans,
No more tomorrow's deferred dreams—
Yielded tasks find their way home,
LIFO slots make schedules groan. 🚀

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Remove ready_queue_next' directly and specifically describes the main change: removing the next_ready_queue field and associated scheduling logic from the Executor struct.
Description check ✅ Passed The description explains the reasoning behind the removal and the new execution model, though it doesn't explicitly mention ready_queue_next by name, it clearly relates to the changeset's core purpose.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch remove-next-queue


📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4cce02c and 333e4af.

📒 Files selected for processing (1)
  • src/runtime.zig
🧰 Additional context used
🧠 Learnings (4)
📚 Learning: 2025-10-21T10:00:56.962Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 62
File: src/utils/queue.zig:141-147
Timestamp: 2025-10-21T10:00:56.962Z
Learning: In work-stealing queues implemented in src/utils/queue.zig, stealers only modify the victim's head position, never the tail. The tail is exclusively owned by the owner thread, so no atomic acquire operations are needed when the owner reads its own tail.

Applied to files:

  • src/runtime.zig
📚 Learning: 2025-10-16T14:33:19.718Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 48
File: src/core/ConcurrentAwaitableList.zig:254-269
Timestamp: 2025-10-16T14:33:19.718Z
Learning: In the zio repository's `src/core/ConcurrentAwaitableList.zig` and `src/core/Awaitable.zig`, the `in_list` field should remain debug-only (conditional compilation with `builtin.mode == .Debug`). Double-removal detection in `ConcurrentAwaitableList.remove()` uses pointer-based checks (comparing `prev`/`next` against `head`/`tail`) rather than relying on a regular `in_list` field.

Applied to files:

  • src/runtime.zig
📚 Learning: 2026-01-10T13:55:00.163Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 184
File: src/runtime/blocking_task.zig:117-146
Timestamp: 2026-01-10T13:55:00.163Z
Learning: In Zig files, do not reference libxev for async I/O. The project uses a custom event loop implementation under src/ev/ and a callback-based async I/O loop similar to libuv/libxev. Reviewers should ensure code relies on the src/ev event loop and that there are no libxev usage patterns; prefer the central event loop in src/ev/ for all asynchronous operations.

Applied to files:

  • src/runtime.zig
📚 Learning: 2026-01-10T14:08:37.019Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 184
File: src/stdio.zig:117-129
Timestamp: 2026-01-10T14:08:37.019Z
Learning: In Zig files implementing swap-like semantics (e.g., old = swap(new); defer swap(old)), ensure the return value is an opaque restoration token rather than a direct reflection of internal state. Rely on the contract to perform increment/decrement logic internally (e.g., shield_count) and not expose internal state through the token. This makes behavior predictable, supports refactoring, and avoids leaking internal state across calls. Apply this pattern broadly to Zig code that uses a paired-swap or restoration-token mechanism.

Applied to files:

  • src/runtime.zig
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: test (windows-latest)
🔇 Additional comments (1)
src/runtime.zig (1)

713-739: Looks good: yield vs wake scheduling is clear and fair.

The rule is consistent and keeps ready_count in sync.

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.


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.

@coderabbitai coderabbitai 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.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/runtime.zig (1)

308-323: Update stale comments that still mention next_ready_queue.

They now contradict the implementation and will mislead future edits.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4cce02c and 333e4af.

📒 Files selected for processing (1)
  • src/runtime.zig
🧰 Additional context used
🧠 Learnings (4)
📚 Learning: 2025-10-21T10:00:56.962Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 62
File: src/utils/queue.zig:141-147
Timestamp: 2025-10-21T10:00:56.962Z
Learning: In work-stealing queues implemented in src/utils/queue.zig, stealers only modify the victim's head position, never the tail. The tail is exclusively owned by the owner thread, so no atomic acquire operations are needed when the owner reads its own tail.

Applied to files:

  • src/runtime.zig
📚 Learning: 2025-10-16T14:33:19.718Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 48
File: src/core/ConcurrentAwaitableList.zig:254-269
Timestamp: 2025-10-16T14:33:19.718Z
Learning: In the zio repository's `src/core/ConcurrentAwaitableList.zig` and `src/core/Awaitable.zig`, the `in_list` field should remain debug-only (conditional compilation with `builtin.mode == .Debug`). Double-removal detection in `ConcurrentAwaitableList.remove()` uses pointer-based checks (comparing `prev`/`next` against `head`/`tail`) rather than relying on a regular `in_list` field.

Applied to files:

  • src/runtime.zig
📚 Learning: 2026-01-10T13:55:00.163Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 184
File: src/runtime/blocking_task.zig:117-146
Timestamp: 2026-01-10T13:55:00.163Z
Learning: In Zig files, do not reference libxev for async I/O. The project uses a custom event loop implementation under src/ev/ and a callback-based async I/O loop similar to libuv/libxev. Reviewers should ensure code relies on the src/ev event loop and that there are no libxev usage patterns; prefer the central event loop in src/ev/ for all asynchronous operations.

Applied to files:

  • src/runtime.zig
📚 Learning: 2026-01-10T14:08:37.019Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 184
File: src/stdio.zig:117-129
Timestamp: 2026-01-10T14:08:37.019Z
Learning: In Zig files implementing swap-like semantics (e.g., old = swap(new); defer swap(old)), ensure the return value is an opaque restoration token rather than a direct reflection of internal state. Rely on the contract to perform increment/decrement logic internally (e.g., shield_count) and not expose internal state through the token. This makes behavior predictable, supports refactoring, and avoids leaking internal state across calls. Apply this pattern broadly to Zig code that uses a paired-swap or restoration-token mechanism.

Applied to files:

  • src/runtime.zig
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: test (windows-latest)
🔇 Additional comments (1)
src/runtime.zig (1)

713-739: Looks good: yield vs wake scheduling is clear and fair.

The rule is consistent and keeps ready_count in sync.

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.

@lalinsky

Copy link
Copy Markdown
Owner Author

@coderabbitai can you guess the reason behind this? if so, does it make sense to yuo?

@coderabbitai

coderabbitai Bot commented Jan 16, 2026

Copy link
Copy Markdown
Contributor

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Read the changes in src/runtime.zig to understand the modifications
git diff origin/main...HEAD -- src/runtime.zig

Length of output: 2490


@lalinsky Yes, I can piece together the reasoning from the code changes:

The old design: You had a two-tier queue system where yielded tasks went to next_ready_queue and would only run in the next event loop iteration (after the concatByMoving merged them back). This provided fairness - yielded tasks wouldn't immediately compete with fresh work.

Why remove it:

  1. Work-stealing simplicity - With work-stealing, you need other threads to steal from your queues. Two queues per worker doubles the synchronization complexity. Which queue do you steal from? Do you need locks on both? Single queue is much cleaner to reason about.

  2. The 61-task batch limit replaces the fairness mechanism - You mentioned executing up to 61 tasks before polling I/O again. This achieves similar fairness without needing separate queues. Yielded tasks go back into the ready queue but the batch limit ensures they don't starve I/O indefinitely.

  3. Less overhead - One less queue, one less concat per iteration, simpler branching in scheduleTaskLocal.

  4. Better CPU-bound throughput - Yielded CPU tasks can run sooner instead of waiting a full iteration.

Does it make sense? Yeah, it's a pragmatic trade-off. You're accepting potential I/O latency hits in mixed workloads in exchange for simpler code that's easier to extend with work-stealing. The batch limit mitigates the worst fairness issues, and you've acknowledged the I/O concerns as something to tune later. For work-stealing, starting with the simplest queue structure that could work is the right call.

@lalinsky
lalinsky merged commit 91281be into main Jan 16, 2026
@lalinsky
lalinsky deleted the remove-next-queue branch January 16, 2026 17:17
@coderabbitai coderabbitai Bot mentioned this pull request Feb 7, 2026
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