Skip to content

Commit 37d2223

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 39d4be0 commit 37d2223

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

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

builtin/checkout.c

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

408-
enable_fscache(1);
408+
enable_fscache(the_repository->index->cache_nr);
409409
for (pos = 0; pos < the_repository->index->cache_nr; pos++) {
410410
struct cache_entry *ce = the_repository->index->cache[pos];
411411
if (ce->ce_flags & CE_MATCHED) {
@@ -431,7 +431,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
431431
errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
432432
NULL, NULL);
433433
mem_pool_discard(&ce_mem_pool, should_validate_cache_entries());
434-
enable_fscache(0);
434+
disable_fscache();
435435
remove_marked_cache_entries(the_repository->index, 1);
436436
remove_scheduled_dirs();
437437
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
@@ -1580,7 +1580,7 @@ struct repository *repo UNUSED)
15801580
PATHSPEC_PREFER_FULL,
15811581
prefix, argv);
15821582

1583-
enable_fscache(1);
1583+
enable_fscache(0);
15841584
if (status_format != STATUS_FORMAT_PORCELAIN &&
15851585
status_format != STATUS_FORMAT_PORCELAIN_V2)
15861586
progress_flag = REFRESH_PROGRESS;
@@ -1621,7 +1621,7 @@ struct repository *repo UNUSED)
16211621
wt_status_print(&s);
16221622
wt_status_collect_free_buffers(&s);
16231623

1624-
enable_fscache(0);
1624+
disable_fscache();
16251625
return 0;
16261626
}
16271627

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
@@ -1562,6 +1562,10 @@ static inline int is_missing_file_error(int errno_)
15621562
#define enable_fscache(x) /* noop */
15631563
#endif
15641564

1565+
#ifndef disable_fscache
1566+
#define disable_fscache() /* noop */
1567+
#endif
1568+
15651569
#ifndef is_fscache_enabled
15661570
#define is_fscache_enabled(path) (0)
15671571
#endif

preload-index.c

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

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

175-
enable_fscache(0);
175+
disable_fscache();
176176
}
177177

178178
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
@@ -1531,7 +1531,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
15311531
typechange_fmt = in_porcelain ? "T\t%s\n" : "%s: needs update\n";
15321532
added_fmt = in_porcelain ? "A\t%s\n" : "%s: needs update\n";
15331533
unmerged_fmt = in_porcelain ? "U\t%s\n" : "%s: needs merge\n";
1534-
enable_fscache(1);
1534+
enable_fscache(0);
15351535
/*
15361536
* Use the multi-threaded preload_index() to refresh most of the
15371537
* cache entries quickly then in the single threaded loop below,
@@ -1626,7 +1626,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
16261626
display_progress(progress, istate->cache_nr);
16271627
stop_progress(&progress);
16281628
trace_performance_leave("refresh index");
1629-
enable_fscache(0);
1629+
disable_fscache();
16301630
return has_errors;
16311631
}
16321632

0 commit comments

Comments
 (0)