Skip to content

Commit c4b3ecb

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 36db88a commit c4b3ecb

File tree

9 files changed

+24
-15
lines changed

9 files changed

+24
-15
lines changed

builtin/add.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
466466
die_in_unpopulated_submodule(&the_index, prefix);
467467
die_path_inside_submodule(&the_index, &pathspec);
468468

469-
enable_fscache(1);
469+
enable_fscache(0);
470470
/* We do not really re-read the index but update the up-to-date flags */
471471
preload_index(&the_index, &pathspec, 0);
472472

builtin/checkout.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
404404
if (pc_workers > 1)
405405
init_parallel_checkout();
406406

407-
enable_fscache(1);
407+
enable_fscache(the_index.cache_nr);
408408
for (pos = 0; pos < the_index.cache_nr; pos++) {
409409
struct cache_entry *ce = the_index.cache[pos];
410410
if (ce->ce_flags & CE_MATCHED) {
@@ -429,7 +429,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
429429
errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
430430
NULL, NULL);
431431
mem_pool_discard(&ce_mem_pool, should_validate_cache_entries());
432-
enable_fscache(0);
432+
disable_fscache();
433433
remove_marked_cache_entries(&the_index, 1);
434434
remove_scheduled_dirs();
435435
errs |= finish_delayed_checkout(&state, opts->show_progress);

builtin/commit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
15661566
PATHSPEC_PREFER_FULL,
15671567
prefix, argv);
15681568

1569-
enable_fscache(1);
1569+
enable_fscache(0);
15701570
if (status_format != STATUS_FORMAT_PORCELAIN &&
15711571
status_format != STATUS_FORMAT_PORCELAIN_V2)
15721572
progress_flag = REFRESH_PROGRESS;
@@ -1607,7 +1607,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
16071607
wt_status_print(&s);
16081608
wt_status_collect_free_buffers(&s);
16091609

1610-
enable_fscache(0);
1610+
disable_fscache();
16111611
return 0;
16121612
}
16131613

compat/win32/fscache.c

Lines changed: 6 additions & 2 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
761761
save_commit_buffer = 0;
762762

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

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

794794
/*

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,10 @@ static inline int is_missing_file_error(int errno_)
14951495
#define enable_fscache(x) /* noop */
14961496
#endif
14971497

1498+
#ifndef disable_fscache
1499+
#define disable_fscache() /* noop */
1500+
#endif
1501+
14981502
#ifndef is_fscache_enabled
14991503
#define is_fscache_enabled(path) (0)
15001504
#endif

preload-index.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ void preload_index(struct index_state *index,
132132
pthread_mutex_init(&pd.mutex, NULL);
133133
}
134134

135-
enable_fscache(1);
135+
enable_fscache(index->cache_nr);
136136
for (i = 0; i < threads; i++) {
137137
struct thread_data *p = data+i;
138138
int err;
@@ -169,7 +169,7 @@ void preload_index(struct index_state *index,
169169
trace2_data_intmax("index", NULL, "preload/sum_lstat", t2_sum_lstat);
170170
trace2_region_leave("index", "preload", NULL);
171171

172-
enable_fscache(0);
172+
disable_fscache();
173173
}
174174

175175
int repo_read_index_preload(struct repository *repo,

read-cache.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,7 +1535,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
15351535
typechange_fmt = in_porcelain ? "T\t%s\n" : "%s: needs update\n";
15361536
added_fmt = in_porcelain ? "A\t%s\n" : "%s: needs update\n";
15371537
unmerged_fmt = in_porcelain ? "U\t%s\n" : "%s: needs merge\n";
1538-
enable_fscache(1);
1538+
enable_fscache(0);
15391539
/*
15401540
* Use the multi-threaded preload_index() to refresh most of the
15411541
* cache entries quickly then in the single threaded loop below,
@@ -1630,7 +1630,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
16301630
display_progress(progress, istate->cache_nr);
16311631
stop_progress(&progress);
16321632
trace_performance_leave("refresh index");
1633-
enable_fscache(0);
1633+
disable_fscache();
16341634
return has_errors;
16351635
}
16361636

0 commit comments

Comments
 (0)