Skip to content

Add spin-wait hint to BlockingMpscQueue producer-side busy loops#4831

Merged
merlimat merged 1 commit into
apache:masterfrom
merlimat:mpsc-producer-spin-wait
Jul 14, 2026
Merged

Add spin-wait hint to BlockingMpscQueue producer-side busy loops#4831
merlimat merged 1 commit into
apache:masterfrom
merlimat:mpsc-producer-spin-wait

Conversation

@merlimat

Copy link
Copy Markdown
Contributor

Motivation

BlockingMpscQueue.put() and the timed offer() busy-spin on relaxedOffer() in a bare loop, with no pause hint. When the queue fills up under back-pressure (e.g. the journal queue with enableBusyWait=true), every blocked producer thread pins a core spinning at full speed — competing for CPU with the very consumer thread that needs to run to drain the queue.

The consumer side (take() / poll()) already idles through WAIT_STRATEGY.idle(), which uses the Thread.onSpinWait() intrinsic (via BusyWait.onSpinWait()). The producer side was missing the same treatment.

Changes

Apply the same wait strategy in the put() and offer(timeout) spin loops. The producers still busy-spin (by design for this queue), but with the spin-wait hint that reduces power/pipeline contention and lets the sibling hyperthread — often the consumer — make progress.

Verified with BlockingMpscQueueTest (4/4 pass) and module checkstyle.

@merlimat merlimat merged commit f1d7cc1 into apache:master Jul 14, 2026
20 checks passed
merlimat added a commit that referenced this pull request Jul 14, 2026
@merlimat merlimat added this to the 4.18.0 milestone Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants