Commit

56c9056e30812bbba545d95ed7eb0f5beb089edd

commit-reach: remove commit-date ordering fallback

Remove the fallback that switched paint_down_to_common() from
generation ordering to commit-date ordering when the commit-graph
lacks corrected commit dates (v1 graph with topo levels only).

The fallback was added in 091f4cf3 (commit: don't use generation
numbers if not needed, 2018-08-30) to avoid a performance
regression on the Linux kernel repo where v1 topo levels caused
"git merge-base v4.8 v4.9" to walk 636k commits instead of 167k.
A side branch with a low topo level stayed in the queue behind a
long chain, preventing early STALE propagation.

Side-exhaustion (added in the previous commits) solves this
differently by terminating the walk as soon as one paint side
empties from the queue, preventing the deep walk regardless of
queue ordering. Benchmarks of "git merge-base --all v4.8 v4.9"
on the Linux kernel repo show that side-exhaustion reduces the
step count far below what the date-ordering fallback achieved:

                         steps      time
  no graph, baseline:   167,413    3.25 s
  v1 graph, baseline:   167,413    0.25 s
  v2 graph, baseline:   167,441    0.29 s
  v1 graph, this series:  5,725    0.02 s
  v2 graph, this series:  3,887    0.01 s

With generation ordering always active, the existing min_generation
check in paint_queue_get() can safely terminate once the walk
crosses below the caller's generation floor. The date ordering
fallback broke this invariant: a commit could have a finite topo
level while the queue was date-ordered, causing the early exit to
fire before all merge bases were found.

With the fallback removed, gen_ordered is always true and can be
dropped. The topo_ceiling field (introduced earlier) already
handles V1_MAX saturation, so the early exit gates need no further
changes.

Signed-off-by: Kristofer Karlsson <krka@spotify.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kristofer Karlsson <krka@spotify.com> · 2026-08-11 09:28 UTC · committed by Junio C Hamano

Browse files
3 files changed, +10 −50
modified Documentation/technical/paint-down-to-common.adoc
@@ -148,43 +148,6 @@ candidate (generation ordering guarantees children are visited
148 148 first), so it cannot be redundant and the walk can stop
149 149 immediately.
150 150
151 - This optimization is NOT safe when the date-ordering fallback is
152 - active, because commit-date order can visit a deeper ancestor
153 - before a shallower one -- see <<date-ordering-fallback>>.
154 -
155 - [[date-ordering-fallback]]
156 - Date-ordering fallback
157 - ----------------------
158 -
159 - When the commit-graph has generation numbers v1 and no
160 - generation floor is specified, topological ordering
161 - (via generation numbers) is disabled. Topological levels are
162 - correct but unbalanced -- ordering by such generation numbers
163 - can sometimes cause the walk to detour too far before finding
164 - merge bases. Commit-date ordering typically reaches them in
165 - fewer steps -- see this change for more details:
166 -
167 - 091f4cf3 (commit: don't use generation numbers if not needed,
168 - 2018-08-30)
169 -
170 - With generation number v2 (corrected commit dates) we have the best
171 - of both worlds and do not need this fallback.
172 -
173 - For v1, `paint_down_to_common()` falls back to pure commit-date
174 - ordering via `compare_commits_by_commit_date`. Because commit
175 - dates are not monotonic (clock skew, rebases, etc.), the queue
176 - may visit commits out of topological order.
177 -
178 - This disables the optimizations that depend on generation ordering:
179 -
180 - - *Single result*: the first merge-base candidate found may not
181 - be the shallowest, because a deeper ancestor with a higher
182 - commit date can be dequeued first.
183 -
184 - - *Side exhaustion*: one paint side can appear to drain from the
185 - queue while commits from that side are still waiting with lower
186 - dates, causing premature termination.
187 -
188 151 Related documentation
189 152 ---------------------
190 153
modified commit-reach.c
@@ -89,7 +89,6 @@ struct paint_state {
89 89 size_t parent1_count;
90 90 size_t parent2_count;
91 91 size_t mb_candidate_count;
92 - int gen_ordered;
93 92 timestamp_t min_generation;
94 93 timestamp_t last_gen;
95 94 timestamp_t topo_ceiling;
@@ -172,7 +171,6 @@ static struct commit *paint_queue_get(struct paint_state *state)
172 171
173 172 /* one side is exhausted */
174 173 if ((!state->parent1_count || !state->parent2_count) &&
175 - state->gen_ordered &&
176 174 generation < state->topo_ceiling)
177 175 return NULL;
178 176 }
@@ -193,9 +191,13 @@ static int paint_down_to_common(struct repository *r,
193 191 enum merge_base_flags mb_flags,
194 192 struct commit_list **result)
195 193 {
194 + /*
195 + * Generation ordering is required for the side-exhaustion and
196 + * single-result early exits, which rely on topological traversal
197 + * order (children visited before parents) in the ordered region.
198 + */
196 199 struct paint_state state = {
197 - .queue = { compare_commits_by_gen_then_commit_date },
198 - .gen_ordered = 1,
200 + .queue = { compare_commits_by_gen_then_commit_date }
199 201 };
200 202 struct commit *commit;
201 203 int i;
@@ -207,10 +209,6 @@ static int paint_down_to_common(struct repository *r,
207 209 state.topo_ceiling = corrected_commit_dates_enabled(r)
208 210 ? GENERATION_NUMBER_INFINITY
209 211 : GENERATION_NUMBER_V1_MAX;
210 - if (!min_generation && !corrected_commit_dates_enabled(r)) {
211 - state.queue.compare = compare_commits_by_commit_date;
212 - state.gen_ordered = 0;
213 - }
214 212
215 213 one->object.flags |= PARENT1;
216 214 if (!n) {
@@ -238,7 +236,6 @@ static int paint_down_to_common(struct repository *r,
238 236 * descendant of this one.
239 237 */
240 238 if (!(mb_flags & MERGE_BASE_FIND_ALL) &&
241 - state.gen_ordered &&
242 239 state.last_gen < state.topo_ceiling)
243 240 break;
244 241 }
modified t/t6600-test-reach.sh
@@ -381,7 +381,7 @@ test_expect_success 'get_merge_bases_many:infinity-both-sides' '
381 381 git rev-parse pi-B
382 382 } >expect &&
383 383 test_all_modes get_merge_bases_many &&
384 - test_paint_down_steps 5 4 5 5
384 + test_paint_down_steps 5 4 5 4
385 385 '
386 386
387 387 test_expect_success 'setup mixed finite/INFINITY topology' '
@@ -414,7 +414,7 @@ test_expect_success 'merge-base --all commit-walk steps' '
414 414 >input &&
415 415 git rev-parse commit-9-1 >expect &&
416 416 run_all_modes git merge-base --all commit-9-9 commit-9-1 &&
417 - test_paint_down_steps 81 9 57 81
417 + test_paint_down_steps 81 9 57 37
418 418 '
419 419
420 420 test_expect_success 'merge-base --all with clock skew (side-exhaustion)' '
@@ -423,7 +423,7 @@ test_expect_success 'merge-base --all with clock skew (side-exhaustion)' '
423 423 >input &&
424 424 git rev-parse se-D >expect &&
425 425 run_all_modes git merge-base --all se-A se-B &&
426 - test_paint_down_steps 6 4 6 6
426 + test_paint_down_steps 6 4 6 4
427 427 '
428 428
429 429 test_expect_success 'merge-base --all with clock skew and redundant ancestor (side-exhaustion)' '
@@ -433,7 +433,7 @@ test_expect_success 'merge-base --all with clock skew and redundant ancestor (si
433 433 >input &&
434 434 git rev-parse se2-MB1 >expect &&
435 435 run_all_modes git merge-base --all se2-A se2-B &&
436 - test_paint_down_steps 8 6 8 8
436 + test_paint_down_steps 8 6 8 6
437 437 '
438 438
439 439 test_expect_success 'reduce_heads' '