Skip to content

Commit 260d408

Browse files
peffgitster
authored andcommitted
config: use getc_unlocked when reading from file
We read config files character-by-character from a stdio handle using fgetc(). This incurs significant locking overhead, even though we know that only one thread can possibly access the handle. We can speed this up by taking the lock ourselves, and then using getc_unlocked to read each character. On a silly pathological case: perl -le ' print "[core]"; print "key$_ = value$_" for (1..1000000) ' >input git config -f input core.key1 this dropped the time to run git-config from: real 0m0.263s user 0m0.260s sys 0m0.000s to: real 0m0.159s user 0m0.152s sys 0m0.004s for a savings of 39%. Most config files are not this big, but the savings should be proportional to the size of the file (i.e., we always save 39%, just of a much smaller number). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 82912d1 commit 260d408

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

config.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static struct config_set the_config_set;
4949

5050
static int config_file_fgetc(struct config_source *conf)
5151
{
52-
return fgetc(conf->u.file);
52+
return getc_unlocked(conf->u.file);
5353
}
5454

5555
static int config_file_ungetc(int c, struct config_source *conf)
@@ -1088,7 +1088,9 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)
10881088

10891089
f = fopen(filename, "r");
10901090
if (f) {
1091+
flockfile(f);
10911092
ret = do_config_from_file(fn, filename, filename, f, data);
1093+
funlockfile(f);
10921094
fclose(f);
10931095
}
10941096
return ret;

0 commit comments

Comments
 (0)