commit-reach: replace queue_has_nonstale with a counter#2124
Conversation
paint_down_to_common() can enqueue the same commit multiple times when it is reached through different parents with different flag combinations. Add an ENQUEUED flag to track whether a commit is currently in the priority queue, and skip it if already present. This change is performance-neutral on its own: the O(n) queue_has_nonstale() scan still dominates the per-iteration cost. However, the deduplication guarantee (each commit appears in the queue at most once) is a prerequisite for the next commit, which replaces that scan with an O(1) nonstale counter. Signed-off-by: Kristofer Karlsson <krka@spotify.com>
30c6cb6 to
ac9f211
Compare
|
There is an issue in commit f780d59:
|
paint_down_to_common() terminates when every commit remaining in its priority queue is STALE. This was checked by queue_has_nonstale(), which performed an O(n) linear scan of the entire queue on every iteration, resulting in O(n*m) total overhead where n is the queue size and m is the number of commits processed. Replace this with an O(1) nonstale_count that tracks the number of non-stale commits currently in the queue. The counter is incremented by maybe_enqueue() and decremented on dequeue and by mark_stale() when a commit transitions to STALE while still in the queue. Since each commit appears at most once (guaranteed by the ENQUEUED flag from the previous commit), the counter is exact. ahead_behind() also uses queue_has_nonstale() and will be converted in the next commit. Signed-off-by: Kristofer Karlsson <krka@spotify.com>
Apply the same nonstale_count optimization from the previous commit to ahead_behind(). This replaces the remaining caller of the O(n) queue_has_nonstale() scan with an O(1) counter check, allowing queue_has_nonstale() to be removed. ahead_behind() already deduplicates queue entries using the PARENT2 flag (via insert_no_dup), so the counter is maintained through insert_no_dup() and mark_stale() using PARENT2 as the queued_flag. Signed-off-by: Kristofer Karlsson <krka@spotify.com>
ac9f211 to
711a0e2
Compare
|
/submit |
|
Submitted as pull.2124.git.1779644541.gitgitgadget@gmail.com To fetch this version into To fetch this version to local tag |
| @@ -17,8 +17,9 @@ | |||
| #define PARENT2 (1u<<17) | |||
There was a problem hiding this comment.
Junio C Hamano wrote on the Git mailing list (how to reply to this email):
"Kristofer Karlsson via GitGitGadget" <gitgitgadget@gmail.com>
writes:
> diff --git a/commit-reach.c b/commit-reach.c
> index d3a9b3ed6f..c16d4b061c 100644
> --- a/commit-reach.c
> +++ b/commit-reach.c
> @@ -17,8 +17,9 @@
> #define PARENT2 (1u<<17)
> #define STALE (1u<<18)
> #define RESULT (1u<<19)
> +#define ENQUEUED (1u<<20)
>
> -static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
> +static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT | ENQUEUED);
> ...
> diff --git a/object.h b/object.h
> index d814647ebe..05cbf728e9 100644
> --- a/object.h
> +++ b/object.h
> @@ -74,7 +74,7 @@ void object_array_init(struct object_array *array);
> * bundle.c: 16
> * http-push.c: 11-----14
> * commit-graph.c: 15
> - * commit-reach.c: 16-----19
> + * commit-reach.c: 16-------20
> * builtin/last-modified.c: 1617
> * sha1-name.c: 20
> * list-objects-filter.c: 21
Not directly the fault of this series, but we'd need to audit and
update this table of bit assignment to match more recent reality.
For example, there no longer exists sha1-name.c but the table claims
that bit 20 is in use for its own purpose, and it being stale makes
it harder to audit and ensure that this new use would not crash with
these existing uses (note. there are other uses of bit 20 in other
subsystems).
FWIW, object-name.c, which was formerly known as sha1-name.c, uses
the bit 20 as ONELINE_SEEN bit, which is used to turn textual object
names like :/string (i.e., commit with that string in its message)
into raw object name, and bit 20 is cleared from all the objects
involved in the search before the helper function returns.
Presumably, once commit-reach.c starts queueing commits and reuses
this bit for its own purpose, we will never try to parse a textual
commit object name to clobber what we thought is ENQUEUED bit,
breaking the code introduced here, so we are probably safe against
its use.
I didn't check all other uses of bit 20, though.
There was a problem hiding this comment.
Derrick Stolee wrote on the Git mailing list (how to reply to this email):
On 5/24/26 7:40 PM, Junio C Hamano wrote:
> "Kristofer Karlsson via GitGitGadget" <gitgitgadget@gmail.com>
> writes:
> >> diff --git a/commit-reach.c b/commit-reach.c
>> index d3a9b3ed6f..c16d4b061c 100644
>> --- a/commit-reach.c
>> +++ b/commit-reach.c
>> @@ -17,8 +17,9 @@
>> #define PARENT2 (1u<<17)
>> #define STALE (1u<<18)
>> #define RESULT (1u<<19)
>> +#define ENQUEUED (1u<<20)
>> >> -static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
>> +static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT | ENQUEUED);
>> ...
>> diff --git a/object.h b/object.h
>> index d814647ebe..05cbf728e9 100644
>> --- a/object.h
>> +++ b/object.h
>> @@ -74,7 +74,7 @@ void object_array_init(struct object_array *array);
>> * bundle.c: 16
>> * http-push.c: 11-----14
>> * commit-graph.c: 15
>> - * commit-reach.c: 16-----19
>> + * commit-reach.c: 16-------20
>> * builtin/last-modified.c: 1617
>> * sha1-name.c: 20
>> * list-objects-filter.c: 21
> > Not directly the fault of this series, but we'd need to audit and
> update this table of bit assignment to match more recent reality.
> > For example, there no longer exists sha1-name.c but the table claims
> that bit 20 is in use for its own purpose, and it being stale makes
> it harder to audit and ensure that this new use would not crash with
> these existing uses (note. there are other uses of bit 20 in other
> subsystems).
It would be worth adding an update patch before this patch, that
only makes these adjustments
> FWIW, object-name.c, which was formerly known as sha1-name.c, uses
> the bit 20 as ONELINE_SEEN bit, which is used to turn textual object
> names like :/string (i.e., commit with that string in its message)
> into raw object name, and bit 20 is cleared from all the objects
> involved in the search before the helper function returns.
This appears to me like the only interaction that _could_ have
overlap with paint_down_to_common().
> Presumably, once commit-reach.c starts queueing commits and reuses
> this bit for its own purpose, we will never try to parse a textual
> commit object name to clobber what we thought is ENQUEUED bit,
> breaking the code introduced here, so we are probably safe against
> its use.
> > I didn't check all other uses of bit 20, though.
FLAG_LINK in builtin/index-pack.c and FLAG_OPEN in
builtin/unpack-objects.c both seem to be completely independent from
this use in commit-reach.c.
Thanks,
-Stolee
|
User |
|
This patch series was integrated into seen via git@1449c2e. |
| @@ -39,6 +40,27 @@ static int compare_commits_by_gen(const void *_a, const void *_b) | |||
| return 0; | |||
There was a problem hiding this comment.
Derrick Stolee wrote on the Git mailing list (how to reply to this email):
On 5/24/26 1:42 PM, Kristofer Karlsson via GitGitGadget wrote:
> From: Kristofer Karlsson <krka@spotify.com>
> > paint_down_to_common() terminates when every commit remaining in its
> priority queue is STALE. This was checked by queue_has_nonstale(),
> which performed an O(n) linear scan of the entire queue on every
> iteration, resulting in O(n*m) total overhead where n is the queue
> size and m is the number of commits processed.
> > Replace this with an O(1) nonstale_count that tracks the number of
> non-stale commits currently in the queue. The counter is incremented
> by maybe_enqueue() and decremented on dequeue and by mark_stale()
> when a commit transitions to STALE while still in the queue. Since
> each commit appears at most once (guaranteed by the ENQUEUED flag
> from the previous commit), the counter is exact.
This idea has a lot of merit, but I'm a bit concerned about the
organization of data. My ideas of how to improve things may also
impact patch 1's use of ENQUEUED.
> -static void maybe_enqueue(struct prio_queue *queue, struct commit *c)
> +static void maybe_enqueue(struct prio_queue *queue, struct commit *c,
> + int *nonstale_count)
> {
> if (c->object.flags & ENQUEUED)
> return;
> c->object.flags |= ENQUEUED;
> prio_queue_put(queue, c);
> + if (!(c->object.flags & STALE))
> + (*nonstale_count)++;
> +}
> +
> +static void mark_stale(struct commit *c, unsigned queued_flag,
> + int *nonstale_count)
> +{
> + if (!(c->object.flags & STALE)) {
> + if (c->object.flags & queued_flag)
> + (*nonstale_count)--;
> + c->object.flags |= STALE;
> + }
> }
These two methods have some concerns on my end:
1. We need to store the nonstale count somewhere other than the
priority queue, even though it's necessarily representing a
subset of the commits within the queue.
2. mark_stale() needs a queued_flag. (I need to check to see if
this is indeed changing in multiple callers or should always
be ENQUEUED).
> static int queue_has_nonstale(struct prio_queue *queue)
> @@ -68,6 +81,7 @@ static int paint_down_to_common(struct repository *r,
> {
> struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
> int i;
> + int nonstale_count = 0;
My preference would be to create a new struct that contains a
prio_queue as a member _and_ a nonstale_count. It could initialize
with compare_commits_by_gen_then_commit_date by default.
The important thing is that consumers of such a "stale-tracking"
queue would not be setting the STALE or ENQUEUED bits themselves,
but instead the queue would be responsible for that.
This could allow us to simplify callers by always assuming we can
"add" an element to the queue and the queue will use its ENQUEUED
bit to prevent duplicates from reaching its internal prio_queue.
Such a data structure could be private to commit-reach.c for now,
since all the methods that would use it seem to be colocated there.
This is a big ask, but I'm interested to see if such an approach
would simplify things here.
Here's a potential breakdown of how to build such a thing in
"small" patches:
1. Create the data structure and update paint_down_to_common and
ahead_behind to use that structure, but still use the existing
prio_queue methods on its internal member.
2. Add the ENQUEUED bit and methods on the new struct that add
that bit as it adds commits to the inner prio_queue. It would
also ignore commits that already have that bit. (Should it
also remove the bit as commits are removed from the queue?)
3. Now add the nonstale_count (or stale count?) to the struct and
have it control the STALE bit modifications, with increasing
the stale count when ENQUEUED is live, and decreasing the stale
count as such a STALE object is dequeued.
I like the idea of this being encapsulated within the struct and
its helper methods. But the proof will be in the implementation.
Thanks,
-Stolee
paint_down_to_common() and ahead_behind() terminate when every commit
in their priority queue is STALE. The current check, queue_has_nonstale(),
does an O(n) linear scan of the queue on every iteration, costing O(n*m)
total where n is the queue size and m is the number of commits processed.
This series replaces that scan with an O(1) counter.
Performance measurements with
git merge-base --allandgit for-each-ref --format='%(ahead-behind:...)':The improvement depends on how wide the frontier gets during the
walk. Component imports in the monorepo create wide frontiers where
the queue grows large, making the O(n) scan expensive -- up to 2.5x
speedup for merge-base and 2.4x for ahead-behind. Linear history and
simple merges show no regression.
With a very narrow frontier the counter approach adds a small constant
overhead per iteration (maintaining the counter and the ENQUEUED flag)
compared to the old scan which would return almost immediately. Both
are O(1) and cheap in that scenario, so it should not matter in
practice -- the benchmark numbers above confirm this.
cc: Derrick Stolee stolee@gmail.com