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 <dstolee@microsoft.com> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Derrick Stolee committed
Oct 12, 2017 at 08:02 UTC
a42d6fd274940be9c0fa6ada738e7611d011a1fc
1 file changed
+12
-2
sha1_name.c
+12
-2
@@ -480,13 +480,23 @@ struct min_abbrev_data {
480
char *hex;
481
};
482
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
+
494
static int extend_abbrev_len(const struct object_id *oid, void *cb_data)
495
{
496
struct min_abbrev_data *mad = cb_data;
497
487
- char *hex = oid_to_hex(oid);
498
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))
500
i++;
501
502
if (i < GIT_MAX_RAWSZ && i >= mad->cur_len)