Skip to content

Commit e6b7930

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 4429f0d commit e6b7930

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
@@ -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

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

411-
enable_fscache(1);
411+
enable_fscache(the_repository->index->cache_nr);
412412
for (pos = 0; pos < the_repository->index->cache_nr; pos++) {
413413
struct cache_entry *ce = the_repository->index->cache[pos];
414414
if (ce->ce_flags & CE_MATCHED) {
@@ -434,7 +434,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
434434
errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
435435
NULL, NULL);
436436
mem_pool_discard(&ce_mem_pool, should_validate_cache_entries());
437-
enable_fscache(0);
437+
disable_fscache();
438438
remove_marked_cache_entries(the_repository->index, 1);
439439
remove_scheduled_dirs();
440440
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
@@ -1602,7 +1602,7 @@ struct repository *repo UNUSED)
16021602
PATHSPEC_PREFER_FULL,
16031603
prefix, argv);
16041604

1605-
enable_fscache(1);
1605+
enable_fscache(0);
16061606
if (status_format != STATUS_FORMAT_PORCELAIN &&
16071607
status_format != STATUS_FORMAT_PORCELAIN_V2)
16081608
progress_flag = REFRESH_PROGRESS;
@@ -1643,7 +1643,7 @@ struct repository *repo UNUSED)
16431643
wt_status_print(&s);
16441644
wt_status_collect_free_buffers(&s);
16451645

1646-
enable_fscache(0);
1646+
disable_fscache();
16471647
return 0;
16481648
}
16491649

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
@@ -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

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

793793
/*

git-compat-util.h

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

1050+
#ifndef disable_fscache
1051+
#define disable_fscache() /* noop */
1052+
#endif
1053+
10501054
#ifndef is_fscache_enabled
10511055
#define is_fscache_enabled(path) (0)
10521056
#endif

preload-index.c

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

141-
enable_fscache(1);
141+
enable_fscache(index->cache_nr);
142142
for (i = 0; i < threads; i++) {
143143
struct thread_data *p = data+i;
144144
int err;
@@ -175,7 +175,7 @@ void preload_index(struct index_state *index,
175175
trace2_data_intmax("index", NULL, "preload/sum_lstat", t2_sum_lstat);
176176
trace2_region_leave("index", "preload", NULL);
177177

178-
enable_fscache(0);
178+
disable_fscache();
179179
}
180180

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

0 commit comments

Comments
 (0)