Skip to content

Commit 155ef25

Browse files
tanayabhgitster
authored andcommitted
rewrite git_config() to use the config-set API
Of all the functions in `git_config*()` family, `git_config()` has the most invocations in the whole code base. Each `git_config()` invocation causes config file rereads which can be avoided using the config-set API. Use the config-set API to rewrite `git_config()` to use the config caching layer to avoid config file rereads on each invocation during a git process lifetime. First invocation constructs the cache, and after that for each successive invocation, `git_config()` feeds values from the config cache instead of rereading the configuration files. Signed-off-by: Tanay Abhra <[email protected]> Reviewed-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5a80e97 commit 155ef25

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

cache.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "gettext.h"
99
#include "convert.h"
1010
#include "trace.h"
11+
#include "string-list.h"
1112

1213
#include SHA1_HEADER
1314
#ifndef git_SHA_CTX
@@ -1351,9 +1352,32 @@ extern int parse_config_key(const char *var,
13511352
const char **subsection, int *subsection_len,
13521353
const char **key);
13531354

1355+
struct config_set_element {
1356+
struct hashmap_entry ent;
1357+
char *key;
1358+
struct string_list value_list;
1359+
};
1360+
1361+
struct configset_list_item {
1362+
struct config_set_element *e;
1363+
int value_index;
1364+
};
1365+
1366+
/*
1367+
* the contents of the list are ordered according to their
1368+
* position in the config files and order of parsing the files.
1369+
* (i.e. key-value pair at the last position of .git/config will
1370+
* be at the last item of the list)
1371+
*/
1372+
struct configset_list {
1373+
struct configset_list_item *items;
1374+
unsigned int nr, alloc;
1375+
};
1376+
13541377
struct config_set {
13551378
struct hashmap config_hash;
13561379
int hash_initialized;
1380+
struct configset_list list;
13571381
};
13581382

13591383
extern void git_configset_init(struct config_set *cs);

config.c

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ struct config_source {
3535
long (*do_ftell)(struct config_source *c);
3636
};
3737

38-
struct config_set_element {
39-
struct hashmap_entry ent;
40-
char *key;
41-
struct string_list value_list;
42-
};
43-
4438
static struct config_source *cf;
4539

4640
static int zlib_compression_seen;
@@ -1232,7 +1226,7 @@ int git_config_with_options(config_fn_t fn, void *data,
12321226
return ret;
12331227
}
12341228

1235-
void git_config(config_fn_t fn, void *data)
1229+
static void git_config_raw(config_fn_t fn, void *data)
12361230
{
12371231
if (git_config_with_options(fn, data, NULL, 1) < 0)
12381232
/*
@@ -1249,6 +1243,33 @@ void git_config(config_fn_t fn, void *data)
12491243
die(_("unknown error occured while reading the configuration files"));
12501244
}
12511245

1246+
static void configset_iter(struct config_set *cs, config_fn_t fn, void *data)
1247+
{
1248+
int i, value_index;
1249+
struct string_list *values;
1250+
struct config_set_element *entry;
1251+
struct configset_list *list = &cs->list;
1252+
struct key_value_info *kv_info;
1253+
1254+
for (i = 0; i < list->nr; i++) {
1255+
entry = list->items[i].e;
1256+
value_index = list->items[i].value_index;
1257+
values = &entry->value_list;
1258+
if (fn(entry->key, values->items[value_index].string, data) < 0) {
1259+
kv_info = values->items[value_index].util;
1260+
git_die_config_linenr(entry->key, kv_info->filename, kv_info->linenr);
1261+
}
1262+
}
1263+
}
1264+
1265+
static void git_config_check_init(void);
1266+
1267+
void git_config(config_fn_t fn, void *data)
1268+
{
1269+
git_config_check_init();
1270+
configset_iter(&the_config_set, fn, data);
1271+
}
1272+
12521273
static struct config_set_element *configset_find_element(struct config_set *cs, const char *key)
12531274
{
12541275
struct config_set_element k;
@@ -1275,6 +1296,7 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
12751296
{
12761297
struct config_set_element *e;
12771298
struct string_list_item *si;
1299+
struct configset_list_item *l_item;
12781300
struct key_value_info *kv_info = xmalloc(sizeof(*kv_info));
12791301

12801302
e = configset_find_element(cs, key);
@@ -1290,6 +1312,12 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
12901312
hashmap_add(&cs->config_hash, e);
12911313
}
12921314
si = string_list_append_nodup(&e->value_list, value ? xstrdup(value) : NULL);
1315+
1316+
ALLOC_GROW(cs->list.items, cs->list.nr + 1, cs->list.alloc);
1317+
l_item = &cs->list.items[cs->list.nr++];
1318+
l_item->e = e;
1319+
l_item->value_index = e->value_list.nr - 1;
1320+
12931321
if (cf) {
12941322
kv_info->filename = strintern(cf->name);
12951323
kv_info->linenr = cf->linenr;
@@ -1313,6 +1341,9 @@ void git_configset_init(struct config_set *cs)
13131341
{
13141342
hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp, 0);
13151343
cs->hash_initialized = 1;
1344+
cs->list.nr = 0;
1345+
cs->list.alloc = 0;
1346+
cs->list.items = NULL;
13161347
}
13171348

13181349
void git_configset_clear(struct config_set *cs)
@@ -1329,6 +1360,10 @@ void git_configset_clear(struct config_set *cs)
13291360
}
13301361
hashmap_free(&cs->config_hash, 1);
13311362
cs->hash_initialized = 0;
1363+
free(cs->list.items);
1364+
cs->list.nr = 0;
1365+
cs->list.alloc = 0;
1366+
cs->list.items = NULL;
13321367
}
13331368

13341369
static int config_set_callback(const char *key, const char *value, void *cb)
@@ -1447,7 +1482,7 @@ static void git_config_check_init(void)
14471482
if (the_config_set.hash_initialized)
14481483
return;
14491484
git_configset_init(&the_config_set);
1450-
git_config(config_set_callback, &the_config_set);
1485+
git_config_raw(config_set_callback, &the_config_set);
14511486
}
14521487

14531488
void git_config_clear(void)

t/t4055-diff-context.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ test_expect_success 'non-integer config parsing' '
7979
test_expect_success 'negative integer config parsing' '
8080
git config diff.context -1 &&
8181
test_must_fail git diff 2>output &&
82-
test_i18ngrep "bad config file" output
82+
test_i18ngrep "bad config variable" output
8383
'
8484

8585
test_expect_success '-U0 is valid, so is diff.context=0' '

0 commit comments

Comments
 (0)