Subject: Re: [GSoC][RFC PATCH] show-branch: use commit-slab for flag storage

From: Junio C Hamano <[email-masked]>
Date: Tue, 18 Feb 2025 10:40:21 -0800

Meet Soni <[email-masked]> writes:

> Replace direct accesses to commit->object.flags with the commit-slab
> mechanism. Introduce `get_commit_flags()` and `set_commit_flags()` to
> retrieve and update flags, respectively, and include `revision.h` so that
> the canonical UNINTERESTING definition is used.
>
> Signed-off-by: Meet Soni <[email-masked]>

Ohhhh.  I thought people somehow have "refactored" the commit
traversal code here to share more with the machinery used by the
"log" family of commands, but the change in this patch being
contained within the single "show-branch" file indicates that it is
not the case, which is good ;-)

And the MAX_REVS limitation has been with us from the very beginning
of the "show-branch" command.  Lifting it is very good ;-) ;-).

> ---
> I'm not entirely sure what the TODO comment meant by storing a pointer to
> the "ref name" directly, so I've assumed that the intent was to store
> flags (of type int) directly in the commit-slab instead of commit->object.

It has been forever since I looked at the code around here the last
time, but I suspect that it meant the final mapping the code makes
at the output phase from the bit position in the flags bits to which
reference the bit (i.e. "I am reachable from that ref") could be
omitted if we make the slab entry a set of (interned) refnames.

But I think using a slab whose element is still a bag of bits that
is wider than object.flags word is is the most straight-forward way
to lift MAX_REVS limitation.  If we can leave everything else
unchanged, that would be great.

> I've tested these changes using:
>  - test suite -- they passed.
>  - github ci result -- https://github.com/inosmeet/git/actions/runs/13355488433

New tests that traverse from more than historical MAX_REVS would be
the most interesting.

It is not exactly surprising if the existing tests do not exercise
such a case (even though it probably should have been checking that
the command refused to accept more than MAX_REVS refs to prevent it
from producing garbage results).

If there were such a test to illustrate what happens when too many
refs are given, we can demonstrate how the behaviour changes, for
the better, with this change ;-)

However ...

> +static int get_commit_flags(struct commit *commit)
> +{
> +	int *result = commit_flags_peek(&commit_flags, commit);
> +	return result ? *result : 0;
> +}
> +
> +static void set_commit_flags(struct commit *commit, int flags)
> +{
> +	*commit_flags_at(&commit_flags, commit) = flags;
> +}

... it does not make much sense to use the slab mechanism if we are
still limited to bitsizeof(type(flags)).  If your "int" is 32 bit,
and the command line fed 100 revs, we'd want a flags parameter that
can house at least 100 bits passed into the "set" function, and
yields the result that can hold at least 100 bits from the "get"
function.  I'd expect that the interface would be more like

    static int get_commit_flags(struct commit *commit, unsigned flags[]);
    static int set_commit_flags(struct commit *commit, unsigned flags[]);

with a file-scope static variable signaling how many bits we are
dealing with (allowing these functions to infer how many words
flags[] array has), and the return values from these helpers to
indicate success (with 0) and failure (with a negative value).  On a
32-bit platform, you'd need at least 4 words in flags[] array to
deal with 100 revs.  On a 64-bit box, 2 words would be sufficient.

I would imagine that we would need two more helpers

    static int set_flag_bit(unsigned flags[], unsigned n);
    static int get_flag_bit(unsigned flags[], unsigned n);

to query the n-th bit in a set of bits stored in flags[] array.

Thanks.