Skip to content

Commit ad803f7

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 4d545f8 commit ad803f7

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
@@ -464,7 +464,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
464464
die_in_unpopulated_submodule(&the_index, prefix);
465465
die_path_inside_submodule(&the_index, &pathspec);
466466

467-
enable_fscache(1);
467+
enable_fscache(0);
468468
/* We do not really re-read the index but update the up-to-date flags */
469469
preload_index(&the_index, &pathspec, 0);
470470

builtin/checkout.c

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

404-
enable_fscache(1);
404+
enable_fscache(the_index.cache_nr);
405405
for (pos = 0; pos < the_index.cache_nr; pos++) {
406406
struct cache_entry *ce = the_index.cache[pos];
407407
if (ce->ce_flags & CE_MATCHED) {
@@ -426,7 +426,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
426426
errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
427427
NULL, NULL);
428428
mem_pool_discard(&ce_mem_pool, should_validate_cache_entries());
429-
enable_fscache(0);
429+
disable_fscache();
430430
remove_marked_cache_entries(&the_index, 1);
431431
remove_scheduled_dirs();
432432
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
@@ -1558,7 +1558,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
15581558
PATHSPEC_PREFER_FULL,
15591559
prefix, argv);
15601560

1561-
enable_fscache(1);
1561+
enable_fscache(0);
15621562
if (status_format != STATUS_FORMAT_PORCELAIN &&
15631563
status_format != STATUS_FORMAT_PORCELAIN_V2)
15641564
progress_flag = REFRESH_PROGRESS;
@@ -1599,7 +1599,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
15991599
wt_status_print(&s);
16001600
wt_status_collect_free_buffers(&s);
16011601

1602-
enable_fscache(0);
1602+
disable_fscache();
16031603
return 0;
16041604
}
16051605

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
@@ -1510,6 +1510,10 @@ static inline int is_missing_file_error(int errno_)
15101510
#define enable_fscache(x) /* noop */
15111511
#endif
15121512

1513+
#ifndef disable_fscache
1514+
#define disable_fscache() /* noop */
1515+
#endif
1516+
15131517
#ifndef is_fscache_enabled
15141518
#define is_fscache_enabled(path) (0)
15151519
#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)