Skip to content

Commit

Permalink
Merge branch 'rs/loose-object-cache-perffix'
Browse files Browse the repository at this point in the history
The loose object cache used to optimize existence look-up has been
updated.

* rs/loose-object-cache-perffix:
  object-store: retire odb_load_loose_cache()
  object-store: use one oid_array per subdirectory for loose cache
  object-store: factor out odb_clear_loose_cache()
  object-store: factor out odb_loose_cache()
  • Loading branch information
gitster committed Jan 18, 2019
2 parents 702bbfe + 8be88db commit eb8638a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
13 changes: 8 additions & 5 deletions object-store.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct object_directory {
* Be sure to call odb_load_loose_cache() before using.
*/
char loose_objects_subdir_seen[256];
struct oid_array loose_objects_cache;
struct oid_array loose_objects_cache[256];

/*
* Path to the alternative object store. If this is a relative path,
Expand Down Expand Up @@ -48,11 +48,14 @@ void add_to_alternates_file(const char *dir);
void add_to_alternates_memory(const char *dir);

/*
* Populate an odb's loose object cache for one particular subdirectory (i.e.,
* the one that corresponds to the first byte of objects you're interested in,
* from 0 to 255 inclusive).
* Populate and return the loose object cache array corresponding to the
* given object ID.
*/
void odb_load_loose_cache(struct object_directory *odb, int subdir_nr);
struct oid_array *odb_loose_cache(struct object_directory *odb,
const struct object_id *oid);

/* Empty the loose object cache for the specified object directory. */
void odb_clear_loose_cache(struct object_directory *odb);

struct packed_git {
struct packed_git *next;
Expand Down
2 changes: 1 addition & 1 deletion object.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ struct raw_object_store *raw_object_store_new(void)
static void free_object_directory(struct object_directory *odb)
{
free(odb->path);
oid_array_clear(&odb->loose_objects_cache);
odb_clear_loose_cache(odb);
free(odb);
}

Expand Down
7 changes: 2 additions & 5 deletions packfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -994,11 +994,8 @@ void reprepare_packed_git(struct repository *r)
{
struct object_directory *odb;

for (odb = r->objects->odb; odb; odb = odb->next) {
oid_array_clear(&odb->loose_objects_cache);
memset(&odb->loose_objects_subdir_seen, 0,
sizeof(odb->loose_objects_subdir_seen));
}
for (odb = r->objects->odb; odb; odb = odb->next)
odb_clear_loose_cache(odb);

r->objects->approximate_object_count_valid = 0;
r->objects->packed_git_initialized = 0;
Expand Down
23 changes: 17 additions & 6 deletions sha1-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -924,16 +924,14 @@ static int open_sha1_file(struct repository *r,
static int quick_has_loose(struct repository *r,
const unsigned char *sha1)
{
int subdir_nr = sha1[0];
struct object_id oid;
struct object_directory *odb;

hashcpy(oid.hash, sha1);

prepare_alt_odb(r);
for (odb = r->objects->odb; odb; odb = odb->next) {
odb_load_loose_cache(odb, subdir_nr);
if (oid_array_lookup(&odb->loose_objects_cache, &oid) >= 0)
if (oid_array_lookup(odb_loose_cache(odb, &oid), &oid) >= 0)
return 1;
}
return 0;
Expand Down Expand Up @@ -2152,24 +2150,37 @@ static int append_loose_object(const struct object_id *oid, const char *path,
return 0;
}

void odb_load_loose_cache(struct object_directory *odb, int subdir_nr)
struct oid_array *odb_loose_cache(struct object_directory *odb,
const struct object_id *oid)
{
int subdir_nr = oid->hash[0];
struct strbuf buf = STRBUF_INIT;

if (subdir_nr < 0 ||
subdir_nr >= ARRAY_SIZE(odb->loose_objects_subdir_seen))
BUG("subdir_nr out of range");

if (odb->loose_objects_subdir_seen[subdir_nr])
return;
return &odb->loose_objects_cache[subdir_nr];

strbuf_addstr(&buf, odb->path);
for_each_file_in_obj_subdir(subdir_nr, &buf,
append_loose_object,
NULL, NULL,
&odb->loose_objects_cache);
&odb->loose_objects_cache[subdir_nr]);
odb->loose_objects_subdir_seen[subdir_nr] = 1;
strbuf_release(&buf);
return &odb->loose_objects_cache[subdir_nr];
}

void odb_clear_loose_cache(struct object_directory *odb)
{
int i;

for (i = 0; i < ARRAY_SIZE(odb->loose_objects_cache); i++)
oid_array_clear(&odb->loose_objects_cache[i]);
memset(&odb->loose_objects_subdir_seen, 0,
sizeof(odb->loose_objects_subdir_seen));
}

static int check_stream_sha1(git_zstream *stream,
Expand Down
10 changes: 5 additions & 5 deletions sha1-name.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,21 @@ static int match_sha(unsigned, const unsigned char *, const unsigned char *);

static void find_short_object_filename(struct disambiguate_state *ds)
{
int subdir_nr = ds->bin_pfx.hash[0];
struct object_directory *odb;

for (odb = the_repository->objects->odb;
odb && !ds->ambiguous;
odb = odb->next) {
int pos;
struct oid_array *loose_objects;

odb_load_loose_cache(odb, subdir_nr);
pos = oid_array_lookup(&odb->loose_objects_cache, &ds->bin_pfx);
loose_objects = odb_loose_cache(odb, &ds->bin_pfx);
pos = oid_array_lookup(loose_objects, &ds->bin_pfx);
if (pos < 0)
pos = -1 - pos;
while (!ds->ambiguous && pos < odb->loose_objects_cache.nr) {
while (!ds->ambiguous && pos < loose_objects->nr) {
const struct object_id *oid;
oid = odb->loose_objects_cache.oid + pos;
oid = loose_objects->oid + pos;
if (!match_sha(ds->len, ds->bin_pfx.hash, oid->hash))
break;
update_candidates(ds, oid);
Expand Down

0 comments on commit eb8638a

Please sign in to comment.