Skip to content

Commit a42d6fd

Browse files
derrickstoleegitster
authored andcommitted
sha1_name: parse less while finding common prefix
Create get_hex_char_from_oid() to parse oids one hex character at a time. This prevents unnecessary copying of hex characters in extend_abbrev_len() when finding the length of a common prefix. Signed-off-by: Derrick Stolee <[email protected]> Reviewed-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5b20ace commit a42d6fd

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

sha1_name.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,13 +480,23 @@ struct min_abbrev_data {
480480
char *hex;
481481
};
482482

483+
static inline char get_hex_char_from_oid(const struct object_id *oid,
484+
unsigned int pos)
485+
{
486+
static const char hex[] = "0123456789abcdef";
487+
488+
if ((pos & 1) == 0)
489+
return hex[oid->hash[pos >> 1] >> 4];
490+
else
491+
return hex[oid->hash[pos >> 1] & 0xf];
492+
}
493+
483494
static int extend_abbrev_len(const struct object_id *oid, void *cb_data)
484495
{
485496
struct min_abbrev_data *mad = cb_data;
486497

487-
char *hex = oid_to_hex(oid);
488498
unsigned int i = mad->init_len;
489-
while (mad->hex[i] && mad->hex[i] == hex[i])
499+
while (mad->hex[i] && mad->hex[i] == get_hex_char_from_oid(oid, i))
490500
i++;
491501

492502
if (i < GIT_MAX_RAWSZ && i >= mad->cur_len)

0 commit comments

Comments
 (0)