sha1_name: use bsearch_pack() for abbreviations

When computing abbreviation lengths for an object ID against a single packfile, the method find_abbrev_len_for_pack() currently implements binary search. This is one of several implementations. One issue with this implementation is that it ignores the fanout table in the pack- index. Translate this binary search to use the existing bsearch_pack() method that correctly uses a fanout table. Due to the use of the fanout table, the abbreviation computation is slightly faster than before. For a fully-repacked copy of the Linux repo, the following 'git log' commands improved: * git log --oneline --parents --raw Before: 59.2s After: 56.9s Rel %: -3.8% * git log --oneline --parents Before: 6.48s After: 5.91s Rel %: -8.9% Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Mar 22, 2018 at 13:40 UTC 0aaf05b3bdc1ce48bed9f8dcba54153a2b7cdc09
1 file changed +4 -20
sha1_name.c
+4 -20
@@ -512,32 +512,16 @@ static void find_abbrev_len_for_pack(struct packed_git *p,
512 struct min_abbrev_data *mad)
513 {
514 int match = 0;
515 - uint32_t num, last, first = 0;
515 + uint32_t num, first = 0;
516 struct object_id oid;
517 + const struct object_id *mad_oid;
518
519 if (open_pack_index(p) || !p->num_objects)
520 return;
521
522 num = p->num_objects;
522 - last = num;
523 - while (first < last) {
524 - uint32_t mid = first + (last - first) / 2;
525 - const unsigned char *current;
526 - int cmp;
527 -
528 - current = nth_packed_object_sha1(p, mid);
529 - cmp = hashcmp(mad->oid->hash, current);
530 - if (!cmp) {
531 - match = 1;
532 - first = mid;
533 - break;
534 - }
535 - if (cmp > 0) {
536 - first = mid + 1;
537 - continue;
538 - }
539 - last = mid;
540 - }
523 + mad_oid = mad->oid;
524 + match = bsearch_pack(mad_oid, p, &first);
525
526 /*
527 * first is now the position in the packfile where we would insert