Skip to content

Commit e44c50f

Browse files
benpeartmjcheetham
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 abddb33 commit e44c50f

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
@@ -460,7 +460,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
460460
die_in_unpopulated_submodule(the_repository->index, prefix);
461461
die_path_inside_submodule(the_repository->index, &pathspec);
462462

463-
enable_fscache(1);
463+
enable_fscache(0);
464464
/* We do not really re-read the index but update the up-to-date flags */
465465
preload_index(the_repository->index, &pathspec, 0);
466466

builtin/checkout.c

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

406-
enable_fscache(1);
406+
enable_fscache(the_repository->index->cache_nr);
407407
for (pos = 0; pos < the_repository->index->cache_nr; pos++) {
408408
struct cache_entry *ce = the_repository->index->cache[pos];
409409
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_repository->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
@@ -1568,7 +1568,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
15681568
PATHSPEC_PREFER_FULL,
15691569
prefix, argv);
15701570

1571-
enable_fscache(1);
1571+
enable_fscache(0);
15721572
if (status_format != STATUS_FORMAT_PORCELAIN &&
15731573
status_format != STATUS_FORMAT_PORCELAIN_V2)
15741574
progress_flag = REFRESH_PROGRESS;
@@ -1609,7 +1609,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
16091609
wt_status_print(&s);
16101610
wt_status_collect_free_buffers(&s);
16111611

1612-
enable_fscache(0);
1612+
disable_fscache();
16131613
return 0;
16141614
}
16151615

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
@@ -757,7 +757,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
757757
save_commit_buffer = 0;
758758

759759
trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
760-
enable_fscache(1);
760+
enable_fscache(0);
761761
for (ref = *refs; ref; ref = ref->next) {
762762
struct commit *commit;
763763

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

790790
/*

git-compat-util.h

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

1540+
#ifndef disable_fscache
1541+
#define disable_fscache() /* noop */
1542+
#endif
1543+
15401544
#ifndef is_fscache_enabled
15411545
#define is_fscache_enabled(path) (0)
15421546
#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
@@ -1530,7 +1530,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
15301530
typechange_fmt = in_porcelain ? "T\t%s\n" : "%s: needs update\n";
15311531
added_fmt = in_porcelain ? "A\t%s\n" : "%s: needs update\n";
15321532
unmerged_fmt = in_porcelain ? "U\t%s\n" : "%s: needs merge\n";
1533-
enable_fscache(1);
1533+
enable_fscache(0);
15341534
/*
15351535
* Use the multi-threaded preload_index() to refresh most of the
15361536
* cache entries quickly then in the single threaded loop below,
@@ -1625,7 +1625,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
16251625
display_progress(progress, istate->cache_nr);
16261626
stop_progress(&progress);
16271627
trace_performance_leave("refresh index");
1628-
enable_fscache(0);
1628+
disable_fscache();
16291629
return has_errors;
16301630
}
16311631

0 commit comments

Comments
 (0)