Skip to content

Commit a755ea6

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 <benpeart@microsoft.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 6f28abe commit a755ea6

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
@@ -477,7 +477,7 @@ int cmd_add(int argc,
477477
die_in_unpopulated_submodule(repo->index, prefix);
478478
die_path_inside_submodule(repo->index, &pathspec);
479479

480-
enable_fscache(1);
480+
enable_fscache(0);
481481
/* We do not really re-read the index but update the up-to-date flags */
482482
preload_index(repo->index, &pathspec, 0);
483483

builtin/checkout.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
407407
if (pc_workers > 1)
408408
init_parallel_checkout();
409409

410-
enable_fscache(1);
410+
enable_fscache(the_repository->index->cache_nr);
411411
for (pos = 0; pos < the_repository->index->cache_nr; pos++) {
412412
struct cache_entry *ce = the_repository->index->cache[pos];
413413
if (ce->ce_flags & CE_MATCHED) {
@@ -433,7 +433,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
433433
errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
434434
NULL, NULL);
435435
mem_pool_discard(&ce_mem_pool, should_validate_cache_entries());
436-
enable_fscache(0);
436+
disable_fscache();
437437
remove_marked_cache_entries(the_repository->index, 1);
438438
remove_scheduled_dirs();
439439
errs |= finish_delayed_checkout(&state, opts->show_progress);

builtin/commit.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1583,7 +1583,7 @@ struct repository *repo UNUSED)
15831583
PATHSPEC_PREFER_FULL,
15841584
prefix, argv);
15851585

1586-
enable_fscache(1);
1586+
enable_fscache(0);
15871587
if (status_format != STATUS_FORMAT_PORCELAIN &&
15881588
status_format != STATUS_FORMAT_PORCELAIN_V2)
15891589
progress_flag = REFRESH_PROGRESS;
@@ -1624,7 +1624,7 @@ struct repository *repo UNUSED)
16241624
wt_status_print(&s);
16251625
wt_status_collect_free_buffers(&s);
16261626

1627-
enable_fscache(0);
1627+
disable_fscache();
16281628
return 0;
16291629
}
16301630

compat/win32/fscache.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ static struct fsentry *fscache_get(struct fsentry *key)
405405
* Enables or disables the cache. Note that the cache is read-only, changes to
406406
* the working directory are NOT reflected in the cache while enabled.
407407
*/
408-
int fscache_enable(int enable)
408+
int fscache_enable(int enable, size_t initial_size)
409409
{
410410
int result;
411411

@@ -421,7 +421,11 @@ int fscache_enable(int enable)
421421
InitializeCriticalSection(&mutex);
422422
lstat_requests = opendir_requests = 0;
423423
fscache_misses = fscache_requests = 0;
424-
hashmap_init(&map, (hashmap_cmp_fn) fsentry_cmp, NULL, 0);
424+
/*
425+
* avoid having to rehash by leaving room for the parent dirs.
426+
* '4' was determined empirically by testing several repos
427+
*/
428+
hashmap_init(&map, (hashmap_cmp_fn) fsentry_cmp, NULL, initial_size * 4);
425429
initialized = 1;
426430
}
427431

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
@@ -762,7 +762,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
762762
save_commit_buffer = 0;
763763

764764
trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
765-
enable_fscache(1);
765+
enable_fscache(0);
766766
for (ref = *refs; ref; ref = ref->next) {
767767
struct commit *commit;
768768

@@ -789,7 +789,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
789789
if (!cutoff || cutoff < commit->date)
790790
cutoff = commit->date;
791791
}
792-
enable_fscache(0);
792+
disable_fscache();
793793
trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
794794

795795
/*

git-compat-util.h

+4
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,10 @@ static inline int is_missing_file_error(int errno_)
15721572
#define enable_fscache(x) /* noop */
15731573
#endif
15741574

1575+
#ifndef disable_fscache
1576+
#define disable_fscache() /* noop */
1577+
#endif
1578+
15751579
#ifndef is_fscache_enabled
15761580
#define is_fscache_enabled(path) (0)
15771581
#endif

preload-index.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void preload_index(struct index_state *index,
136136
pthread_mutex_init(&pd.mutex, NULL);
137137
}
138138

139-
enable_fscache(1);
139+
enable_fscache(index->cache_nr);
140140
for (i = 0; i < threads; i++) {
141141
struct thread_data *p = data+i;
142142
int err;
@@ -173,7 +173,7 @@ void preload_index(struct index_state *index,
173173
trace2_data_intmax("index", NULL, "preload/sum_lstat", t2_sum_lstat);
174174
trace2_region_leave("index", "preload", NULL);
175175

176-
enable_fscache(0);
176+
disable_fscache();
177177
}
178178

179179
int repo_read_index_preload(struct repository *repo,

read-cache.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
15321532
typechange_fmt = in_porcelain ? "T\t%s\n" : "%s: needs update\n";
15331533
added_fmt = in_porcelain ? "A\t%s\n" : "%s: needs update\n";
15341534
unmerged_fmt = in_porcelain ? "U\t%s\n" : "%s: needs merge\n";
1535-
enable_fscache(1);
1535+
enable_fscache(0);
15361536
/*
15371537
* Use the multi-threaded preload_index() to refresh most of the
15381538
* cache entries quickly then in the single threaded loop below,
@@ -1627,7 +1627,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
16271627
display_progress(progress, istate->cache_nr);
16281628
stop_progress(&progress);
16291629
trace_performance_leave("refresh index");
1630-
enable_fscache(0);
1630+
disable_fscache();
16311631
return has_errors;
16321632
}
16331633

0 commit comments

Comments
 (0)