commit-reach: terminate merge-base walk when one paint side is exhausted

Add an early termination check to paint_down_to_common() using the per-side counters introduced earlier. Once the walk enters the finite-generation region, terminate early when one side's exclusive count drops to zero -- no new merge-base can form without both paint sides meeting. The check also waits for pending_merge_bases to reach zero, ensuring all merge-base candidates have been dequeued and recorded before exiting. The INFINITY gate ensures correctness: commits without a commit-graph entry have GENERATION_NUMBER_INFINITY and are ordered by commit date, which is not topologically reliable. The optimization only fires once the walk enters the finite-generation region where ordering guarantees hold. Step counts measured with trace2 on git.git with commit-graph: merge-base --all v2.0.0 v2.55.0-rc1: before: 72264 steps after: 44589 steps merge-base --all v2.55.0-rc1 v2.55.0-rc1~5: before: 110 steps after: 7 steps Helped-by: Derrick Stolee <stolee@gmail.com> Helped-by: Elijah Newren <newren@gmail.com> Signed-off-by: Kristofer Karlsson <krka@spotify.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kristofer Karlsson committed Jul 11, 2026 at 13:27 UTC 8df944d32a9792d07e60081222c7b31fd98e6c8b
3 files changed +39 -6
Documentation/technical/paint-down-to-common.adoc
+22 -1
@@ -105,6 +105,9 @@ ends when one of the following conditions holds:
105 4. Single result: the caller only needs one merge base, one has
106 been found, and the walk has entered the finite-generation
107 region.
108 + 5. Side exhaustion: no pure PARENT1 or pure PARENT2 commits
109 + remain in the queue, no pending merge-base candidates exist,
110 + and the walk has entered the finite-generation region.
111
112 Stale entry condition
113 ~~~~~~~~~~~~~~~~~~~~~
@@ -115,6 +118,20 @@ existing candidates by proving one is an ancestor of another, but
118 `remove_redundant()` handles that as a post-processing step, so it
119 is safe to exit early.
120
121 +Side-exhaustion condition
122 +~~~~~~~~~~~~~~~~~~~~~~~~~
123 +A new merge-base requires commits from both sides to meet. When one
124 +side's exclusive counter reaches zero and there are no pending
125 +merge-base candidates, no future traversal step can produce a new
126 +candidate.
127 +
128 +This optimization only activates in the finite-generation region
129 +where topological ordering holds. In that region, children are
130 +always visited before parents, so paint flags are final at visit
131 +time and an exhausted side cannot reappear. In the INFINITY region,
132 +commit-date ordering can violate this guarantee, so the check is
133 +skipped.
134 +
135 Generation cutoff
136 ~~~~~~~~~~~~~~~~~
137 Some callers (notably `remove_redundant()`) supply a `min_generation`
@@ -159,12 +176,16 @@ ordering via `compare_commits_by_commit_date`. Because commit
176 dates are not monotonic (clock skew, rebases, etc.), the queue
177 may visit commits out of topological order.
178
162 -This disables the optimization that depends on generation ordering:
179 +This disables the optimizations that depend on generation ordering:
180
181 - *Single result*: the first merge-base candidate found may not
182 be the shallowest, because a deeper ancestor with a higher
183 commit date can be dequeued first.
184
185 + - *Side exhaustion*: one paint side can appear to drain from the
186 + queue while commits from that side are still waiting with lower
187 + dates, causing premature termination.
188 +
189 Related documentation
190 ---------------------
191
commit-reach.c
+15 -3
@@ -132,6 +132,10 @@ static void paint_queue_put(struct paint_state *state,
132 }
133 }
134
135 +/*
136 + * Dequeue the next commit for the paint walk, or return NULL when
137 + * no more merge bases can be discovered.
138 + */
139 static struct commit *paint_queue_get(struct paint_state *state)
140 {
141 struct commit *commit = prio_queue_get(&state->queue);
@@ -141,9 +145,17 @@ static struct commit *paint_queue_get(struct paint_state *state)
145
146 commit->object.flags &= ~ENQUEUED;
147
144 - if (!state->parent1_count && !state->parent2_count &&
145 - !state->mb_candidate_count)
146 - return NULL;
148 + if (!state->mb_candidate_count) {
149 + /* only stale entries remain */
150 + if (!state->parent1_count && !state->parent2_count)
151 + return NULL;
152 +
153 + /* one side is exhausted */
154 + if ((!state->parent1_count || !state->parent2_count) &&
155 + state->gen_ordered &&
156 + commit_graph_generation(commit) < GENERATION_NUMBER_INFINITY)
157 + return NULL;
158 + }
159
160 paint_count_update(state, commit->object.flags, -1);
161 return commit;
t/t6600-test-reach.sh
+2 -2
@@ -297,7 +297,7 @@ test_expect_success 'in_merge_bases_many:self' '
297 EOF
298 echo "in_merge_bases_many(A,X):1" >expect &&
299 test_all_modes in_merge_bases_many &&
300 - test_paint_down_steps 45 2 25 3
300 + test_paint_down_steps 45 1 25 1
301 '
302
303 test_expect_success 'is_descendant_of:hit' '
@@ -414,7 +414,7 @@ test_expect_success 'merge-base --all commit-walk steps' '
414 >input &&
415 git rev-parse commit-9-1 >expect &&
416 run_all_modes git merge-base --all commit-9-9 commit-9-1 &&
417 - test_paint_down_steps 81 80 81 81
417 + test_paint_down_steps 81 9 57 81
418 '
419
420 test_expect_success 'merge-base --all with clock skew (side-exhaustion)' '