Skip to content

Commit 222fdde

Browse files
pks-tgitster
authored andcommitted
object-file: extract logic to approximate object count
In "builtin/gc.c" we have some logic that checks whether we need to repack objects. This is done by counting the number of objects that we have and checking whether it exceeds a certain threshold. We don't really need an accurate object count though, which is why we only open a single object directory shard and then extrapolate from there. Extract this logic into a new function that is owned by the loose object database source. This is done to prepare for a subsequent change, where we'll introduce object counting on the object database source level. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent dd587cd commit 222fdde

File tree

3 files changed

+63
-28
lines changed

3 files changed

+63
-28
lines changed

builtin/gc.c

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -467,37 +467,18 @@ static int rerere_gc_condition(struct gc_config *cfg UNUSED)
467467
static int too_many_loose_objects(int limit)
468468
{
469469
/*
470-
* Quickly check if a "gc" is needed, by estimating how
471-
* many loose objects there are. Because SHA-1 is evenly
472-
* distributed, we can check only one and get a reasonable
473-
* estimate.
470+
* This is weird, but stems from legacy behaviour: the GC auto
471+
* threshold was always essentially interpreted as if it was rounded up
472+
* to the next multiple 256 of, so we retain this behaviour for now.
474473
*/
475-
DIR *dir;
476-
struct dirent *ent;
477-
int auto_threshold;
478-
int num_loose = 0;
479-
int needed = 0;
480-
const unsigned hexsz_loose = the_hash_algo->hexsz - 2;
481-
char *path;
482-
483-
path = repo_git_path(the_repository, "objects/17");
484-
dir = opendir(path);
485-
free(path);
486-
if (!dir)
474+
int auto_threshold = DIV_ROUND_UP(limit, 256) * 256;
475+
unsigned long loose_count;
476+
477+
if (odb_source_loose_approximate_object_count(the_repository->objects->sources,
478+
&loose_count) < 0)
487479
return 0;
488480

489-
auto_threshold = DIV_ROUND_UP(limit, 256);
490-
while ((ent = readdir(dir)) != NULL) {
491-
if (strspn(ent->d_name, "0123456789abcdef") != hexsz_loose ||
492-
ent->d_name[hexsz_loose] != '\0')
493-
continue;
494-
if (++num_loose > auto_threshold) {
495-
needed = 1;
496-
break;
497-
}
498-
}
499-
closedir(dir);
500-
return needed;
481+
return loose_count > auto_threshold;
501482
}
502483

503484
static struct packed_git *find_base_packs(struct string_list *packs,

object-file.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,6 +1868,47 @@ int odb_source_loose_for_each_object(struct odb_source *source,
18681868
NULL, NULL, &data);
18691869
}
18701870

1871+
int odb_source_loose_approximate_object_count(struct odb_source *source,
1872+
unsigned long *out)
1873+
{
1874+
const unsigned hexsz = source->odb->repo->hash_algo->hexsz - 2;
1875+
unsigned long count = 0;
1876+
struct dirent *ent;
1877+
char *path = NULL;
1878+
DIR *dir = NULL;
1879+
int ret;
1880+
1881+
path = xstrfmt("%s/17", source->path);
1882+
1883+
dir = opendir(path);
1884+
if (!dir) {
1885+
if (errno == ENOENT) {
1886+
*out = 0;
1887+
ret = 0;
1888+
goto out;
1889+
}
1890+
1891+
ret = error_errno("cannot open object shard '%s'", path);
1892+
goto out;
1893+
}
1894+
1895+
while ((ent = readdir(dir)) != NULL) {
1896+
if (strspn(ent->d_name, "0123456789abcdef") != hexsz ||
1897+
ent->d_name[hexsz] != '\0')
1898+
continue;
1899+
count++;
1900+
}
1901+
1902+
*out = count * 256;
1903+
ret = 0;
1904+
1905+
out:
1906+
if (dir)
1907+
closedir(dir);
1908+
free(path);
1909+
return ret;
1910+
}
1911+
18711912
static int append_loose_object(const struct object_id *oid,
18721913
const char *path UNUSED,
18731914
void *data)

object-file.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,19 @@ int odb_source_loose_for_each_object(struct odb_source *source,
139139
void *cb_data,
140140
unsigned flags);
141141

142+
/*
143+
* Count the number of loose objects in this source.
144+
*
145+
* The object count is approximated by opening a single sharding directory for
146+
* loose objects and scanning its contents. The result is then extrapolated by
147+
* 256. This should generally work as a reasonable estimate given that the
148+
* object hash is supposed to be indistinguishable from random.
149+
*
150+
* Returns 0 on success, a negative error code otherwise.
151+
*/
152+
int odb_source_loose_approximate_object_count(struct odb_source *source,
153+
unsigned long *out);
154+
142155
/**
143156
* format_object_header() is a thin wrapper around s xsnprintf() that
144157
* writes the initial "<type> <obj-len>" part of the loose object

0 commit comments

Comments
 (0)