Skip to content

Commit 0a045ff

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 c43f50b commit 0a045ff

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

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

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) {
@@ -430,7 +430,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
430430
errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
431431
NULL, NULL);
432432
mem_pool_discard(&ce_mem_pool, should_validate_cache_entries());
433-
enable_fscache(0);
433+
disable_fscache();
434434
remove_marked_cache_entries(&the_index, 1);
435435
remove_scheduled_dirs();
436436
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
@@ -1582,7 +1582,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
15821582
PATHSPEC_PREFER_FULL,
15831583
prefix, argv);
15841584

1585-
enable_fscache(1);
1585+
enable_fscache(0);
15861586
if (status_format != STATUS_FORMAT_PORCELAIN &&
15871587
status_format != STATUS_FORMAT_PORCELAIN_V2)
15881588
progress_flag = REFRESH_PROGRESS;
@@ -1623,7 +1623,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
16231623
wt_status_print(&s);
16241624
wt_status_collect_free_buffers(&s);
16251625

1626-
enable_fscache(0);
1626+
disable_fscache();
16271627
return 0;
16281628
}
16291629

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
@@ -759,7 +759,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
759759
save_commit_buffer = 0;
760760

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

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

792792
/*

git-compat-util.h

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

1526+
#ifndef disable_fscache
1527+
#define disable_fscache() /* noop */
1528+
#endif
1529+
15261530
#ifndef is_fscache_enabled
15271531
#define is_fscache_enabled(path) (0)
15281532
#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
@@ -1560,7 +1560,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
15601560
typechange_fmt = in_porcelain ? "T\t%s\n" : "%s: needs update\n";
15611561
added_fmt = in_porcelain ? "A\t%s\n" : "%s: needs update\n";
15621562
unmerged_fmt = in_porcelain ? "U\t%s\n" : "%s: needs merge\n";
1563-
enable_fscache(1);
1563+
enable_fscache(0);
15641564
/*
15651565
* Use the multi-threaded preload_index() to refresh most of the
15661566
* cache entries quickly then in the single threaded loop below,
@@ -1655,7 +1655,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
16551655
display_progress(progress, istate->cache_nr);
16561656
stop_progress(&progress);
16571657
trace_performance_leave("refresh index");
1658-
enable_fscache(0);
1658+
disable_fscache();
16591659
return has_errors;
16601660
}
16611661

0 commit comments

Comments
 (0)