gnodet opened a new pull request, #14186:
URL: https://github.com/apache/maven/pull/14186

   # perf: replace O(N²) full-plan scan with O(N) push-based ready queue in 
concurrent builder
   
   ## Summary
   
   The concurrent builder's `executePlan()` performed a full O(N) scan of all 
build
   steps on every scheduling cycle to find steps whose predecessors had all 
completed.
   Since a step completion triggers one scheduling cycle, the total work is 
O(N²) —
   unacceptable for large multi-module projects.
   
   Reported by @seregamorph in the Maven dev mailing list with a 1,100-module
   synthetic project seeing the build scheduler spin at 100% CPU on 6 threads 
for
   several minutes.
   
   ## Root Cause
   
   ```java
   // OLD — O(all steps) on every call
   plan.sortedNodes().stream()
       .filter(BuildStep::isCreated)
       .filter(step -> step.predecessors.stream().allMatch(BuildStep::isDone))
       .forEach(this::processStep);
   ```
   
   With N steps and each step completion triggering one cycle, the total work is
   `O(N × N) = O(N²)`. For 1,100 modules × ~50 lifecycle steps each = 55,000 
steps,
   this is ~3 billion comparisons.
   
   ## Fix
   
   Replace the scan with a push-based topology approach:
   
   - **`BuildStep.pendingPredecessors`** — `AtomicInteger` tracking the number 
of
     predecessors not yet in a terminal state.
   - **`BuildStep.notifySuccessors(Queue)`** — called exactly once when a step
     reaches a terminal state (EXECUTED/FAILED/SKIPPED); decrements each 
successor's
     counter and enqueues those that reach zero.
   - **`BuildPlan.initReadyQueue(Queue)`** — initializes counters on all CREATED
     steps and seeds the queue; called once at plan construction time and again 
after
     each dynamic re-plan.
   - **`executePlan()`** — replaced with a simple `while (readyQueue.poll() != 
null)`
     drain loop, O(ready steps) per call.
   
   Total scheduling work is now **O(N)** — each step enters the queue exactly 
once
   and is processed exactly once.
   
   ## Thread Safety
   
   - `readyQueue` is a `ConcurrentLinkedQueue` — lock-free, thread-safe.
   - `pendingPredecessors` is an `AtomicInteger` — CAS-based decrement ensures
     each successor is enqueued exactly once regardless of concurrency.
   - `initReadyQueue` is called under the write lock (inside `plan()`) so it
     sees a consistent view of the DAG.
   - `notifySuccessors` is called without the read lock (from async executor
     threads) — safe because it only touches lock-free data structures.
   
   ## Tests
   
   All 769 existing `maven-core` tests pass. The concurrent builder tests
   (`BuildPlanExecutorTest`, `BuildStepTest`, `BuildPlanCreatorTest`) all pass
   (`mvn verify -pl impl/maven-core -am`).
   
   _Hermes Agent (Claude Sonnet 4.6) on behalf of Guillaume Nodet_
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to