Skip to content

Commit 034be8a

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 575e23c commit 034be8a

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
@@ -491,7 +491,7 @@ int cmd_add(int argc,
491491
die_in_unpopulated_submodule(repo->index, prefix);
492492
die_path_inside_submodule(repo->index, &pathspec);
493493

494-
enable_fscache(1);
494+
enable_fscache(0);
495495
/* We do not really re-read the index but update the up-to-date flags */
496496
preload_index(repo->index, &pathspec, 0);
497497

builtin/checkout.c

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

418-
enable_fscache(1);
418+
enable_fscache(the_repository->index->cache_nr);
419419
for (pos = 0; pos < the_repository->index->cache_nr; pos++) {
420420
struct cache_entry *ce = the_repository->index->cache[pos];
421421
if (ce->ce_flags & CE_MATCHED) {
@@ -441,7 +441,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
441441
errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
442442
NULL, NULL);
443443
mem_pool_discard(&ce_mem_pool, should_validate_cache_entries());
444-
enable_fscache(0);
444+
disable_fscache();
445445
remove_marked_cache_entries(the_repository->index, 1);
446446
remove_scheduled_dirs();
447447
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
@@ -1618,7 +1618,7 @@ struct repository *repo UNUSED)
16181618
PATHSPEC_PREFER_FULL,
16191619
prefix, argv);
16201620

1621-
enable_fscache(1);
1621+
enable_fscache(0);
16221622
if (status_format != STATUS_FORMAT_PORCELAIN &&
16231623
status_format != STATUS_FORMAT_PORCELAIN_V2)
16241624
progress_flag = REFRESH_PROGRESS;
@@ -1659,7 +1659,7 @@ struct repository *repo UNUSED)
16591659
wt_status_print(&s);
16601660
wt_status_collect_free_buffers(&s);
16611661

1662-
enable_fscache(0);
1662+
disable_fscache();
16631663
return 0;
16641664
}
16651665

compat/win32/fscache.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ static struct fsentry *fscache_get(struct fsentry *key)
410410
* Enables or disables the cache. Note that the cache is read-only, changes to
411411
* the working directory are NOT reflected in the cache while enabled.
412412
*/
413-
int fscache_enable(int enable)
413+
int fscache_enable(int enable, size_t initial_size)
414414
{
415415
int result;
416416

@@ -426,7 +426,11 @@ int fscache_enable(int enable)
426426
InitializeCriticalSection(&mutex);
427427
lstat_requests = opendir_requests = 0;
428428
fscache_misses = fscache_requests = 0;
429-
hashmap_init(&map, (hashmap_cmp_fn) fsentry_cmp, NULL, 0);
429+
/*
430+
* avoid having to rehash by leaving room for the parent dirs.
431+
* '4' was determined empirically by testing several repos
432+
*/
433+
hashmap_init(&map, (hashmap_cmp_fn) fsentry_cmp, NULL, initial_size * 4);
430434
initialized = 1;
431435
}
432436

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
@@ -767,7 +767,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
767767
save_commit_buffer = 0;
768768

769769
trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
770-
enable_fscache(1);
770+
enable_fscache(0);
771771
for (ref = *refs; ref; ref = ref->next) {
772772
struct commit *commit;
773773

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

798798
/*

git-compat-util.h

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

1068+
#ifndef disable_fscache
1069+
#define disable_fscache() /* noop */
1070+
#endif
1071+
10681072
#ifndef is_fscache_enabled
10691073
#define is_fscache_enabled(path) (0)
10701074
#endif

preload-index.c

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

144-
enable_fscache(1);
144+
enable_fscache(index->cache_nr);
145145
for (i = 0; i < threads; i++) {
146146
struct thread_data *p = data+i;
147147
int err;
@@ -178,7 +178,7 @@ void preload_index(struct index_state *index,
178178
trace2_data_intmax("index", NULL, "preload/sum_lstat", t2_sum_lstat);
179179
trace2_region_leave("index", "preload", NULL);
180180

181-
enable_fscache(0);
181+
disable_fscache();
182182
}
183183

184184
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
@@ -1505,7 +1505,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
15051505
typechange_fmt = in_porcelain ? "T\t%s\n" : "%s: needs update\n";
15061506
added_fmt = in_porcelain ? "A\t%s\n" : "%s: needs update\n";
15071507
unmerged_fmt = in_porcelain ? "U\t%s\n" : "%s: needs merge\n";
1508-
enable_fscache(1);
1508+
enable_fscache(0);
15091509
/*
15101510
* Use the multi-threaded preload_index() to refresh most of the
15111511
* cache entries quickly then in the single threaded loop below,
@@ -1600,7 +1600,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
16001600
display_progress(progress, istate->cache_nr);
16011601
stop_progress(&progress);
16021602
trace_performance_leave("refresh index");
1603-
enable_fscache(0);
1603+
disable_fscache();
16041604
return has_errors;
16051605
}
16061606

0 commit comments

Comments
 (0)