Re: [PATCH] cache-tree: use ce_namelen() instead of strlen()
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Jan 5, 2021, 12:05 UTC
- Message-ID
- <xmqqlfd7zj0d.fsf@gitster.c.googlers.com>
- In-Reply-To
- <a8b5bb13-5c88-d744-b47e-7204e12ad05d@web.de>
René Scharfe <l.s.r@web.de> writes:
Show 6 quoted lines
> Use the name length field of cache entries instead of calculating its > value anew. > > Signed-off-by: René Scharfe <l.s.r@web.de> > --- > Not sure why it took me so long to spot this.. o_O
That probably is because this does not cause behaviour change.
It used to be that use of ce_namelen() was more important in learning the length of the string before 7a51ed66 (Make on-disk index representation separate from in-core one, 2008-01-14) and 7fec10b7 (index: be careful when handling long names, 2008-01-18). The on-disk field to store the name length has only 12 bits, and before b60e188c (Strip namelen out of ce_flags into a ce_namelen field, 2012-07-11), the convention to learn the length of the name of an in-core cache entry was to see the field and then if it fully occupies the full 12-bit field, ask the name string itself its length with strlen().
These days, ce->namelen is a separate field that always gives the full length after the on-disk index is read, so with this change, you won't be running strlen() in this part of the function even with an entry with a long pathname.
Show 27 quoted lines
>
> cache-tree.c | 10 ++++++----
> 1 file changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/cache-tree.c b/cache-tree.c
> index a537a806c1..57cacab195 100644
> --- a/cache-tree.c
> +++ b/cache-tree.c
> @@ -185,10 +185,12 @@ static int verify_cache(struct cache_entry **cache,
> * the cache is sorted. Also path can appear only once,
> * which means conflicting one would immediately follow.
> */
> - const char *this_name = cache[i]->name;
> - const char *next_name = cache[i+1]->name;
> - int this_len = strlen(this_name);
> - if (this_len < strlen(next_name) &&
> + const struct cache_entry *this_ce = cache[i];
> + const struct cache_entry *next_ce = cache[i + 1];
> + const char *this_name = this_ce->name;
> + const char *next_name = next_ce->name;
> + int this_len = ce_namelen(this_ce);
> + if (this_len < ce_namelen(next_ce) &&
> strncmp(this_name, next_name, this_len) == 0 &&
> next_name[this_len] == '/') {
> if (10 < ++funny) {
> --
> 2.30.0