Skip to content

Perf/sharded handoff queue - #110

Merged
Yiming1997 merged 2 commits into
mainfrom
perf/sharded-handoff-queue
Oct 14, 2026
Merged

Yiming1997 merged 2 commits into
mainfrom
perf/sharded-handoff-queue

Conversation

@Yiming1997

Copy link
Copy Markdown
Owner

No description provided.

Replace the single taskQueue channel with N power-of-two shard channels so a large worker count no longer serialises on one channel lock. Producers round-robin across shards via a lock-free atomic cursor; workers scan shards from a per-worker cursor and only fall back to the chunked overflow buffer when every shard is empty, dropping the old double non-blocking poll.

Shard count defaults to next-power-of-two(min(GOMAXPROCS,32)) for queues of at least 1024 slots, single-channel below that; each shard gets at least one slot so total capacity never shrinks. Adds WithTaskQueueShards(n); GetTaskQueueLen sums shards and GetTaskQueueCapacity reports the configured total.

Measured on 20 cores, capacity 20000, queue 100000: hand-off throughput about 2.4x, CPU per hand-off about 2.4x lower, procyield -82 percent. Execution-bound 10ms tasks: +12-18 percent at high concurrency, no regression at low concurrency. Note: task execution order is not guaranteed.
Add invariant-based concurrency stress tests for the sharded handoff queue: no task loss or duplication, no deadlock across shard/queue/capacity combinations, nonblock accept accounting, reentrant submission, graceful shutdown, panic isolation and ctx cancellation, plus a 300k-task heavy run.

Add benchmark/scaling_matrix_test.go (worker-count x load matrix, implementation agnostic) and benchmark/ants_compare_test.go (CPU and hand-off comparison against panjf2000/ants v2, added as a test-only dependency).
@Yiming1997
Yiming1997 merged commit 219437b into main Oct 14, 2026
1 check passed
@Yiming1997
Yiming1997 deleted the perf/sharded-handoff-queue branch October 14, 2026 12:10
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