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>

Patrick Steinhardt committed Mar 12, 2026 at 09:42 UTC 222fddeaa44b633eea345996735b4f7893eb71ec
3 files changed +63 -28
builtin/gc.c
+9 -28
@@ -467,37 +467,18 @@ out:
467 static int too_many_loose_objects(int limit)
468 {
469 /*
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.
473 */
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)
479 return 0;
480
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;
482 }
483
484 static struct packed_git *find_base_packs(struct string_list *packs,
object-file.c
+41
@@ -1868,6 +1868,47 @@ int odb_source_loose_for_each_object(struct odb_source *source,
1868 NULL, NULL, &data);
1869 }
1870
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 +
1912 static int append_loose_object(const struct object_id *oid,
1913 const char *path UNUSED,
1914 void *data)
object-file.h
+13
@@ -139,6 +139,19 @@ int odb_source_loose_for_each_object(struct odb_source *source,
139 void *cb_data,
140 unsigned flags);
141
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 +
155 /**
156 * format_object_header() is a thin wrapper around s xsnprintf() that
157 * writes the initial "<type> <obj-len>" part of the loose object