Skip to content

Commit 4a9dcfb

Browse files
benpeartdscho
authored andcommitted
fscache: fscache takes an initial size
Update enable_fscache() to take an optional initial size parameter which is used to initialize the hashmap so that it can avoid having to rehash as additional entries are added. Add a separate disable_fscache() macro to make the code clearer and easier to read. Signed-off-by: Ben Peart <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 0013860 commit 4a9dcfb

File tree

9 files changed

+24
-15
lines changed

9 files changed

+24
-15
lines changed

builtin/add.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
540540
die_in_unpopulated_submodule(&the_index, prefix);
541541
die_path_inside_submodule(&the_index, &pathspec);
542542

543-
enable_fscache(1);
543+
enable_fscache(0);
544544
/* We do not really re-read the index but update the up-to-date flags */
545545
preload_index(&the_index, &pathspec, 0);
546546

builtin/checkout.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
368368
NULL);
369369

370370
enable_delayed_checkout(&state);
371-
enable_fscache(1);
371+
enable_fscache(active_nr);
372372
for (pos = 0; pos < active_nr; pos++) {
373373
struct cache_entry *ce = active_cache[pos];
374374
if (ce->ce_flags & CE_MATCHED) {
@@ -388,7 +388,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
388388
pos = skip_same_name(ce, pos) - 1;
389389
}
390390
}
391-
enable_fscache(0);
391+
disable_fscache();
392392
remove_marked_cache_entries(&the_index, 1);
393393
remove_scheduled_dirs();
394394
errs |= finish_delayed_checkout(&state, &nr_checkouts);

builtin/commit.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
14181418
PATHSPEC_PREFER_FULL,
14191419
prefix, argv);
14201420

1421-
enable_fscache(1);
1421+
enable_fscache(0);
14221422
if (status_format != STATUS_FORMAT_PORCELAIN &&
14231423
status_format != STATUS_FORMAT_PORCELAIN_V2)
14241424
progress_flag = REFRESH_PROGRESS;
@@ -1459,7 +1459,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
14591459
wt_status_print(&s);
14601460
wt_status_collect_free_buffers(&s);
14611461

1462-
enable_fscache(0);
1462+
disable_fscache();
14631463
return 0;
14641464
}
14651465

compat/win32/fscache.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ static struct fsentry *fscache_get(struct fsentry *key)
397397
* Enables or disables the cache. Note that the cache is read-only, changes to
398398
* the working directory are NOT reflected in the cache while enabled.
399399
*/
400-
int fscache_enable(int enable)
400+
int fscache_enable(int enable, size_t initial_size)
401401
{
402402
int result;
403403

@@ -413,7 +413,11 @@ int fscache_enable(int enable)
413413
InitializeCriticalSection(&mutex);
414414
lstat_requests = opendir_requests = 0;
415415
fscache_misses = fscache_requests = 0;
416-
hashmap_init(&map, (hashmap_cmp_fn) fsentry_cmp, NULL, 0);
416+
/*
417+
* avoid having to rehash by leaving room for the parent dirs.
418+
* '4' was determined empirically by testing several repos
419+
*/
420+
hashmap_init(&map, (hashmap_cmp_fn) fsentry_cmp, NULL, initial_size * 4);
417421
initialized = 1;
418422
}
419423

compat/win32/fscache.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#ifndef FSCACHE_H
22
#define FSCACHE_H
33

4-
int fscache_enable(int enable);
5-
#define enable_fscache(x) fscache_enable(x)
4+
int fscache_enable(int enable, size_t initial_size);
5+
#define enable_fscache(initial_size) fscache_enable(1, initial_size)
6+
#define disable_fscache() fscache_enable(0, 0)
67

78
int fscache_enabled(const char *path);
89
#define is_fscache_enabled(path) fscache_enabled(path)

fetch-pack.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
675675
save_commit_buffer = 0;
676676

677677
trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
678-
enable_fscache(1);
678+
enable_fscache(0);
679679
for (ref = *refs; ref; ref = ref->next) {
680680
struct object *o;
681681

@@ -698,7 +698,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
698698
cutoff = commit->date;
699699
}
700700
}
701-
enable_fscache(0);
701+
disable_fscache();
702702
trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
703703

704704
/*

git-compat-util.h

+4
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,10 @@ static inline int is_missing_file_error(int errno_)
13041304
#define enable_fscache(x) /* noop */
13051305
#endif
13061306

1307+
#ifndef disable_fscache
1308+
#define disable_fscache() /* noop */
1309+
#endif
1310+
13071311
#ifndef is_fscache_enabled
13081312
#define is_fscache_enabled(path) (0)
13091313
#endif

preload-index.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void preload_index(struct index_state *index,
120120
pthread_mutex_init(&pd.mutex, NULL);
121121
}
122122

123-
enable_fscache(1);
123+
enable_fscache(index->cache_nr);
124124
for (i = 0; i < threads; i++) {
125125
struct thread_data *p = data+i;
126126
int err;
@@ -146,7 +146,7 @@ void preload_index(struct index_state *index,
146146
stop_progress(&pd.progress);
147147

148148
trace_performance_leave("preload index");
149-
enable_fscache(0);
149+
disable_fscache();
150150
}
151151

152152
int repo_read_index_preload(struct repository *repo,

read-cache.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
15301530
typechange_fmt = in_porcelain ? "T\t%s\n" : "%s: needs update\n";
15311531
added_fmt = in_porcelain ? "A\t%s\n" : "%s: needs update\n";
15321532
unmerged_fmt = in_porcelain ? "U\t%s\n" : "%s: needs merge\n";
1533-
enable_fscache(1);
1533+
enable_fscache(0);
15341534
/*
15351535
* Use the multi-threaded preload_index() to refresh most of the
15361536
* cache entries quickly then in the single threaded loop below,
@@ -1608,7 +1608,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
16081608
stop_progress(&progress);
16091609
}
16101610
trace_performance_leave("refresh index");
1611-
enable_fscache(0);
1611+
disable_fscache();
16121612
return has_errors;
16131613
}
16141614

0 commit comments

Comments
 (0)