Skip to content

Commit cf2dc1c

Browse files
Eric Wonggitster
authored andcommitted
speed up alt_odb_usable() with many alternates
With many alternates, the duplicate check in alt_odb_usable() wastes many cycles doing repeated fspathcmp() on every existing alternate. Use a khash to speed up lookups by odb->path. Since the kh_put_* API uses the supplied key without duplicating it, we also take advantage of it to replace both xstrdup() and strbuf_release() in link_alt_odb_entry() with strbuf_detach() to avoid the allocation and copy. In a test repository with 50K alternates and each of those 50K alternates having one alternate each (for a total of 100K total alternates); this speeds up lookup of a non-existent blob from over 16 minutes to roughly 2.7 seconds on my busy workstation. Note: all underlying git object directories were small and unpacked with only loose objects and no packs. Having to load packs increases times significantly. Signed-off-by: Eric Wong <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 670b81a commit cf2dc1c

File tree

5 files changed

+42
-12
lines changed

5 files changed

+42
-12
lines changed

dir.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,21 @@ int fspathcmp(const char *a, const char *b)
8484
return ignore_case ? strcasecmp(a, b) : strcmp(a, b);
8585
}
8686

87+
int fspatheq(const char *a, const char *b)
88+
{
89+
return !fspathcmp(a, b);
90+
}
91+
8792
int fspathncmp(const char *a, const char *b, size_t count)
8893
{
8994
return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count);
9095
}
9196

97+
unsigned int fspathhash(const char *str)
98+
{
99+
return ignore_case ? strihash(str) : strhash(str);
100+
}
101+
92102
int git_fnmatch(const struct pathspec_item *item,
93103
const char *pattern, const char *string,
94104
int prefix)

dir.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,9 @@ int remove_dir_recursively(struct strbuf *path, int flag);
489489
int remove_path(const char *path);
490490

491491
int fspathcmp(const char *a, const char *b);
492+
int fspatheq(const char *a, const char *b);
492493
int fspathncmp(const char *a, const char *b, size_t count);
494+
unsigned int fspathhash(const char *str);
493495

494496
/*
495497
* The prefix part of pattern must not contains wildcards.

object-file.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,9 @@ const char *loose_object_path(struct repository *r, struct strbuf *buf,
517517
*/
518518
static int alt_odb_usable(struct raw_object_store *o,
519519
struct strbuf *path,
520-
const char *normalized_objdir)
520+
const char *normalized_objdir, khiter_t *pos)
521521
{
522-
struct object_directory *odb;
522+
int r;
523523

524524
/* Detect cases where alternate disappeared */
525525
if (!is_directory(path->buf)) {
@@ -533,14 +533,20 @@ static int alt_odb_usable(struct raw_object_store *o,
533533
* Prevent the common mistake of listing the same
534534
* thing twice, or object directory itself.
535535
*/
536-
for (odb = o->odb; odb; odb = odb->next) {
537-
if (!fspathcmp(path->buf, odb->path))
538-
return 0;
536+
if (!o->odb_by_path) {
537+
khiter_t p;
538+
539+
o->odb_by_path = kh_init_odb_path_map();
540+
assert(!o->odb->next);
541+
p = kh_put_odb_path_map(o->odb_by_path, o->odb->path, &r);
542+
assert(r == 1); /* never used */
543+
kh_value(o->odb_by_path, p) = o->odb;
539544
}
540-
if (!fspathcmp(path->buf, normalized_objdir))
545+
if (fspatheq(path->buf, normalized_objdir))
541546
return 0;
542-
543-
return 1;
547+
*pos = kh_put_odb_path_map(o->odb_by_path, path->buf, &r);
548+
/* r: 0 = exists, 1 = never used, 2 = deleted */
549+
return r == 0 ? 0 : 1;
544550
}
545551

546552
/*
@@ -566,6 +572,7 @@ static int link_alt_odb_entry(struct repository *r, const char *entry,
566572
{
567573
struct object_directory *ent;
568574
struct strbuf pathbuf = STRBUF_INIT;
575+
khiter_t pos;
569576

570577
if (!is_absolute_path(entry) && relative_base) {
571578
strbuf_realpath(&pathbuf, relative_base, 1);
@@ -587,23 +594,25 @@ static int link_alt_odb_entry(struct repository *r, const char *entry,
587594
while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/')
588595
strbuf_setlen(&pathbuf, pathbuf.len - 1);
589596

590-
if (!alt_odb_usable(r->objects, &pathbuf, normalized_objdir)) {
597+
if (!alt_odb_usable(r->objects, &pathbuf, normalized_objdir, &pos)) {
591598
strbuf_release(&pathbuf);
592599
return -1;
593600
}
594601

595602
CALLOC_ARRAY(ent, 1);
596-
ent->path = xstrdup(pathbuf.buf);
603+
/* pathbuf.buf is already in r->objects->odb_by_path */
604+
ent->path = strbuf_detach(&pathbuf, NULL);
597605

598606
/* add the alternate entry */
599607
*r->objects->odb_tail = ent;
600608
r->objects->odb_tail = &(ent->next);
601609
ent->next = NULL;
610+
assert(r->objects->odb_by_path);
611+
kh_value(r->objects->odb_by_path, pos) = ent;
602612

603613
/* recursively add alternates */
604-
read_info_alternates(r, pathbuf.buf, depth + 1);
614+
read_info_alternates(r, ent->path, depth + 1);
605615

606-
strbuf_release(&pathbuf);
607616
return 0;
608617
}
609618

object-store.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "oid-array.h"
88
#include "strbuf.h"
99
#include "thread-utils.h"
10+
#include "khash.h"
11+
#include "dir.h"
1012

1113
struct object_directory {
1214
struct object_directory *next;
@@ -30,6 +32,9 @@ struct object_directory {
3032
char *path;
3133
};
3234

35+
KHASH_INIT(odb_path_map, const char * /* key: odb_path */,
36+
struct object_directory *, 1, fspathhash, fspatheq);
37+
3338
void prepare_alt_odb(struct repository *r);
3439
char *compute_alternate_path(const char *path, struct strbuf *err);
3540
typedef int alt_odb_fn(struct object_directory *, void *);
@@ -116,6 +121,8 @@ struct raw_object_store {
116121
*/
117122
struct object_directory *odb;
118123
struct object_directory **odb_tail;
124+
kh_odb_path_map_t *odb_by_path;
125+
119126
int loaded_alternates;
120127

121128
/*

object.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ static void free_object_directories(struct raw_object_store *o)
511511
free_object_directory(o->odb);
512512
o->odb = next;
513513
}
514+
kh_destroy_odb_path_map(o->odb_by_path);
515+
o->odb_by_path = NULL;
514516
}
515517

516518
void raw_object_store_clear(struct raw_object_store *o)

0 commit comments

Comments
 (0)