Add AsyncDrainWriter to eliminate PrintWriter lock contention - #12654
Add AsyncDrainWriter to eliminate PrintWriter lock contention#12654gnodet wants to merge 1 commit into
Conversation
During parallel model building, multiple PhasingExecutor threads log concurrently via SLF4J → ProjectBuildLogAppender → SimpleBuildEventListener → PrintWriter.println(). Since PrintWriter.println() is synchronized, this creates significant contention (1,470ms blocked time on a 4383-module project with -T1C). AsyncDrainWriter wraps the writer Consumer with a lock-free ConcurrentLinkedQueue and a non-blocking tryLock() drain pattern: - Producers enqueue messages (CAS, no blocking) - At most one thread drains the queue to the underlying PrintWriter - Other threads return immediately after enqueue - close() performs a final blocking drain to ensure no messages are lost JFR results on 4383-module diamond project (validate -T1C): - PrintWriter contention: 1,470ms → 0ms (eliminated) - PhasingExecutor contention: 546ms → 49ms (side effect) - Median wall time: -485ms (-3.3%) - Run-to-run variance halved (range 2.70s → 1.33s) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
gnodet
left a comment
There was a problem hiding this comment.
Clean, well-motivated performance optimization with a correct lock-free drain algorithm. The ConcurrentLinkedQueue + tryLock drain + single re-check pattern is well-established for reducing contention on a shared output sink.
The 1,470ms-to-0ms PrintWriter contention elimination is a compelling result backed by thorough JFR profiling data.
One suggestion: consider adding unit tests for AsyncDrainWriter — a concurrent component should have at least basic tests for (1) single-threaded message ordering, (2) multi-threaded stress test verifying no messages are lost, and (3) close() draining remaining buffered messages.
Also, if delegate.accept(m) throws during drain(), subsequent queued messages are silently dropped. In practice this is unlikely since PrintWriter.println() swallows IOExceptions, but a try/catch in drain() would make the component more robust.
Note: Cannot submit as APPROVE because the PR author matches the review account.
This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.
Claude Code on behalf of gnodet
Summary
During parallel model building (
-T1C), multiplePhasingExecutorthreads log concurrently via:Since
PrintWriter.println()issynchronized, all threads serialize on every log call. JFR profiling on a 4383-module diamond-graph project shows 1,470ms of blocked time from this single contention point — the #1 source of lock contention duringvalidate.Fix
AsyncDrainWriterwraps theConsumer<String>writer with a lock-free queue + non-blocking drain:ConcurrentLinkedQueue(CAS, no blocking)PrintWriter(viaReentrantLock.tryLock())close()performs a final blocking drain to guarantee no messages are lostJFR Results (4383-module diamond project,
validate -T1C)Benchmark (10 runs)
All 8,766 module log lines verified present — zero messages lost.
Test plan
maven-coreunit tests passmaven-cliunit tests pass🤖 Generated with Claude Code