Skip to content

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

Open
merlimat wants to merge 1 commit into
apache:masterfrom
merlimat:mpsc-producer-spin-wait
Open

Add spin-wait hint to BlockingMpscQueue producer-side busy loops#4831
merlimat wants to merge 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.

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