config: make `delta_base_cache_limit` a non-global variable

The `delta_base_cache_limit` variable is a global config variable used by multiple subsystems. Let's make this non-global, by adding this variable independently to the subsystems where it is used. First, add the setting to the `repo_settings` struct, this provides access to the config in places where the repository is available. Use this in `packfile.c`. In `index-pack.c` we add it to the `pack_idx_option` struct and its constructor. While the repository struct is available here, it may not be set because `git index-pack` can be used without a repository. In `gc.c` add it to the `gc_config` struct and also the constructor function. The gc functions currently do not have direct access to a repository struct. These changes are made to remove the usage of `delta_base_cache_limit` as a global variable in `packfile.c`. This brings us one step closer to removing the `USE_THE_REPOSITORY_VARIABLE` definition in `packfile.c` which we complete in the next patch. Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Dec 3, 2024 at 15:44 UTC d6b2d21fbf269db7a6be56d28a62cb65a7d7a660
11 files changed +39 -14
builtin/gc.c
+11 -1
@@ -138,6 +138,11 @@ struct gc_config {
138 char *repack_filter_to;
139 unsigned long big_pack_threshold;
140 unsigned long max_delta_cache_size;
141 + /*
142 + * Remove this member from gc_config once repo_settings is passed
143 + * through the callchain.
144 + */
145 + size_t delta_base_cache_limit;
146 };
147
148 #define GC_CONFIG_INIT { \
@@ -153,6 +158,7 @@ struct gc_config {
158 .prune_expire = xstrdup("2.weeks.ago"), \
159 .prune_worktrees_expire = xstrdup("3.months.ago"), \
160 .max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE, \
161 + .delta_base_cache_limit = DEFAULT_DELTA_BASE_CACHE_LIMIT, \
162 }
163
164 static void gc_config_release(struct gc_config *cfg)
@@ -168,6 +174,7 @@ static void gc_config(struct gc_config *cfg)
174 {
175 const char *value;
176 char *owned = NULL;
177 + unsigned long ulongval;
178
179 if (!git_config_get_value("gc.packrefs", &value)) {
180 if (value && !strcmp(value, "notbare"))
@@ -206,6 +213,9 @@ static void gc_config(struct gc_config *cfg)
213 git_config_get_ulong("gc.bigpackthreshold", &cfg->big_pack_threshold);
214 git_config_get_ulong("pack.deltacachesize", &cfg->max_delta_cache_size);
215
216 + if (!git_config_get_ulong("core.deltabasecachelimit", &ulongval))
217 + cfg->delta_base_cache_limit = ulongval;
218 +
219 if (!git_config_get_string("gc.repackfilter", &owned)) {
220 free(cfg->repack_filter);
221 cfg->repack_filter = owned;
@@ -416,7 +426,7 @@ static uint64_t estimate_repack_memory(struct gc_config *cfg,
426 * read_sha1_file() (either at delta calculation phase, or
427 * writing phase) also fills up the delta base cache
428 */
419 - heap += delta_base_cache_limit;
429 + heap += cfg->delta_base_cache_limit;
430 /* and of course pack-objects has its own delta cache */
431 heap += cfg->max_delta_cache_size;
432
builtin/index-pack.c
+7 -3
@@ -1238,7 +1238,7 @@ static void parse_pack_objects(unsigned char *hash)
1238 * recursively checking if the resulting object is used as a base
1239 * for some more deltas.
1240 */
1241 -static void resolve_deltas(void)
1241 +static void resolve_deltas(struct pack_idx_option *opts)
1242 {
1243 int i;
1244
@@ -1254,7 +1254,7 @@ static void resolve_deltas(void)
1254 nr_ref_deltas + nr_ofs_deltas);
1255
1256 nr_dispatched = 0;
1257 - base_cache_limit = delta_base_cache_limit * nr_threads;
1257 + base_cache_limit = opts->delta_base_cache_limit * nr_threads;
1258 if (nr_threads > 1 || getenv("GIT_FORCE_THREADS")) {
1259 init_thread();
1260 work_lock();
@@ -1604,6 +1604,10 @@ static int git_index_pack_config(const char *k, const char *v,
1604 else
1605 opts->flags &= ~WRITE_REV;
1606 }
1607 + if (!strcmp(k, "core.deltabasecachelimit")) {
1608 + opts->delta_base_cache_limit = git_config_ulong(k, v, ctx->kvi);
1609 + return 0;
1610 + }
1611 return git_default_config(k, v, ctx, cb);
1612 }
1613
@@ -1930,7 +1934,7 @@ int cmd_index_pack(int argc,
1934 parse_pack_objects(pack_hash);
1935 if (report_end_of_input)
1936 write_in_full(2, "\0", 1);
1933 - resolve_deltas();
1937 + resolve_deltas(&opts);
1938 conclude_pack(fix_thin_pack, curr_pack, pack_hash);
1939 free(ofs_deltas);
1940 free(ref_deltas);
config.c
-5
@@ -1515,11 +1515,6 @@ static int git_default_core_config(const char *var, const char *value,
1515 return 0;
1516 }
1517
1518 - if (!strcmp(var, "core.deltabasecachelimit")) {
1519 - delta_base_cache_limit = git_config_ulong(var, value, ctx->kvi);
1520 - return 0;
1521 - }
1522 -
1518 if (!strcmp(var, "core.autocrlf")) {
1519 if (value && !strcasecmp(value, "input")) {
1520 auto_crlf = AUTO_CRLF_INPUT;
environment.c
-1
@@ -51,7 +51,6 @@ enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
51 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
52 size_t packed_git_window_size = DEFAULT_PACKED_GIT_WINDOW_SIZE;
53 size_t packed_git_limit = DEFAULT_PACKED_GIT_LIMIT;
54 -size_t delta_base_cache_limit = 96 * 1024 * 1024;
54 unsigned long big_file_threshold = 512 * 1024 * 1024;
55 char *editor_program;
56 char *askpass_program;
environment.h
-1
@@ -165,7 +165,6 @@ extern int zlib_compression_level;
165 extern int pack_compression_level;
166 extern size_t packed_git_window_size;
167 extern size_t packed_git_limit;
168 -extern size_t delta_base_cache_limit;
168 extern unsigned long big_file_threshold;
169 extern unsigned long pack_size_limit_cfg;
170 extern int max_allowed_tree_depth;
pack-objects.h
+2 -1
@@ -7,7 +7,8 @@
7
8 struct repository;
9
10 -#define DEFAULT_DELTA_CACHE_SIZE (256 * 1024 * 1024)
10 +#define DEFAULT_DELTA_CACHE_SIZE (256 * 1024 * 1024)
11 +#define DEFAULT_DELTA_BASE_CACHE_LIMIT (96 * 1024 * 1024)
12
13 #define OE_DFS_STATE_BITS 2
14 #define OE_DEPTH_BITS 12
pack-write.c
+1
@@ -21,6 +21,7 @@ void reset_pack_idx_option(struct pack_idx_option *opts)
21 memset(opts, 0, sizeof(*opts));
22 opts->version = 2;
23 opts->off32_limit = 0x7fffffff;
24 + opts->delta_base_cache_limit = DEFAULT_DELTA_BASE_CACHE_LIMIT;
25 }
26
27 static int sha1_compare(const void *_a, const void *_b)
pack.h
+2
@@ -58,6 +58,8 @@ struct pack_idx_option {
58 */
59 int anomaly_alloc, anomaly_nr;
60 uint32_t *anomaly;
61 +
62 + size_t delta_base_cache_limit;
63 };
64
65 void reset_pack_idx_option(struct pack_idx_option *);
packfile.c
+8 -2
@@ -1496,7 +1496,9 @@ void clear_delta_base_cache(void)
1496 }
1497
1498 static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
1499 - void *base, unsigned long base_size, enum object_type type)
1499 + void *base, unsigned long base_size,
1500 + unsigned long delta_base_cache_limit,
1501 + enum object_type type)
1502 {
1503 struct delta_base_cache_entry *ent;
1504 struct list_head *lru, *tmp;
@@ -1698,6 +1700,8 @@ void *unpack_entry(struct repository *r, struct packed_git *p, off_t obj_offset,
1700 int delta_stack_nr = 0, delta_stack_alloc = UNPACK_ENTRY_STACK_PREALLOC;
1701 int base_from_cache = 0;
1702
1703 + prepare_repo_settings(p->repo);
1704 +
1705 write_pack_access_log(p, obj_offset);
1706
1707 /* PHASE 1: drill down to the innermost base object */
@@ -1878,7 +1882,9 @@ void *unpack_entry(struct repository *r, struct packed_git *p, off_t obj_offset,
1882 * before we are done using it.
1883 */
1884 if (!external_base)
1881 - add_delta_base_cache(p, base_obj_offset, base, base_size, type);
1885 + add_delta_base_cache(p, base_obj_offset, base, base_size,
1886 + p->repo->settings.delta_base_cache_limit,
1887 + type);
1888
1889 free(delta_data);
1890 free(external_base);
repo-settings.c
+5
@@ -3,6 +3,7 @@
3 #include "repo-settings.h"
4 #include "repository.h"
5 #include "midx.h"
6 +#include "pack-objects.h"
7
8 static void repo_cfg_bool(struct repository *r, const char *key, int *dest,
9 int def)
@@ -26,6 +27,7 @@ void prepare_repo_settings(struct repository *r)
27 const char *strval;
28 int manyfiles;
29 int read_changed_paths;
30 + unsigned long ulongval;
31
32 if (!r->gitdir)
33 BUG("Cannot add settings for uninitialized repository");
@@ -123,6 +125,9 @@ void prepare_repo_settings(struct repository *r)
125 * removed.
126 */
127 r->settings.command_requires_full_index = 1;
128 +
129 + if (!repo_config_get_ulong(r, "core.deltabasecachelimit", &ulongval))
130 + r->settings.delta_base_cache_limit = ulongval;
131 }
132
133 enum log_refs_config repo_settings_get_log_all_ref_updates(struct repository *repo)
repo-settings.h
+3
@@ -57,12 +57,15 @@ struct repo_settings {
57
58 int core_multi_pack_index;
59 int warn_ambiguous_refs; /* lazily loaded via accessor */
60 +
61 + size_t delta_base_cache_limit;
62 };
63 #define REPO_SETTINGS_INIT { \
64 .index_version = -1, \
65 .core_untracked_cache = UNTRACKED_CACHE_KEEP, \
66 .fetch_negotiation_algorithm = FETCH_NEGOTIATION_CONSECUTIVE, \
67 .warn_ambiguous_refs = -1, \
68 + .delta_base_cache_limit = DEFAULT_DELTA_BASE_CACHE_LIMIT, \
69 }
70
71 void prepare_repo_settings(struct repository *r);