Skip to content

Commit 65cab33

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 e829c89 commit 65cab33

9 files changed

+24
-15
lines changed

builtin/add.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
593593
die_in_unpopulated_submodule(&the_index, prefix);
594594
die_path_inside_submodule(&the_index, &pathspec);
595595

596-
enable_fscache(1);
596+
enable_fscache(0);
597597
/* We do not really re-read the index but update the up-to-date flags */
598598
preload_index(&the_index, &pathspec, 0);
599599

builtin/checkout.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
392392
if (pc_workers > 1)
393393
init_parallel_checkout();
394394

395-
enable_fscache(1);
395+
enable_fscache(the_index.cache_nr);
396396
for (pos = 0; pos < the_index.cache_nr; pos++) {
397397
struct cache_entry *ce = the_index.cache[pos];
398398
if (ce->ce_flags & CE_MATCHED) {
@@ -417,7 +417,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
417417
errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
418418
NULL, NULL);
419419
mem_pool_discard(&ce_mem_pool, should_validate_cache_entries());
420-
enable_fscache(0);
420+
disable_fscache();
421421
remove_marked_cache_entries(&the_index, 1);
422422
remove_scheduled_dirs();
423423
errs |= finish_delayed_checkout(&state, opts->show_progress);

builtin/commit.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
15511551
PATHSPEC_PREFER_FULL,
15521552
prefix, argv);
15531553

1554-
enable_fscache(1);
1554+
enable_fscache(0);
15551555
if (status_format != STATUS_FORMAT_PORCELAIN &&
15561556
status_format != STATUS_FORMAT_PORCELAIN_V2)
15571557
progress_flag = REFRESH_PROGRESS;
@@ -1592,7 +1592,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
15921592
wt_status_print(&s);
15931593
wt_status_collect_free_buffers(&s);
15941594

1595-
enable_fscache(0);
1595+
disable_fscache();
15961596
return 0;
15971597
}
15981598

compat/win32/fscache.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ static struct fsentry *fscache_get(struct fsentry *key)
403403
* Enables or disables the cache. Note that the cache is read-only, changes to
404404
* the working directory are NOT reflected in the cache while enabled.
405405
*/
406-
int fscache_enable(int enable)
406+
int fscache_enable(int enable, size_t initial_size)
407407
{
408408
int result;
409409

@@ -419,7 +419,11 @@ int fscache_enable(int enable)
419419
InitializeCriticalSection(&mutex);
420420
lstat_requests = opendir_requests = 0;
421421
fscache_misses = fscache_requests = 0;
422-
hashmap_init(&map, (hashmap_cmp_fn) fsentry_cmp, NULL, 0);
422+
/*
423+
* avoid having to rehash by leaving room for the parent dirs.
424+
* '4' was determined empirically by testing several repos
425+
*/
426+
hashmap_init(&map, (hashmap_cmp_fn) fsentry_cmp, NULL, initial_size * 4);
423427
initialized = 1;
424428
}
425429

compat/win32/fscache.h

+3-2
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
755755
save_commit_buffer = 0;
756756

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

@@ -782,7 +782,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
782782
if (!cutoff || cutoff < commit->date)
783783
cutoff = commit->date;
784784
}
785-
enable_fscache(0);
785+
disable_fscache();
786786
trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
787787

788788
/*

git-compat-util.h

+4
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,10 @@ static inline int is_missing_file_error(int errno_)
15521552
#define enable_fscache(x) /* noop */
15531553
#endif
15541554

1555+
#ifndef disable_fscache
1556+
#define disable_fscache() /* noop */
1557+
#endif
1558+
15551559
#ifndef is_fscache_enabled
15561560
#define is_fscache_enabled(path) (0)
15571561
#endif

preload-index.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void preload_index(struct index_state *index,
126126
pthread_mutex_init(&pd.mutex, NULL);
127127
}
128128

129-
enable_fscache(1);
129+
enable_fscache(index->cache_nr);
130130
for (i = 0; i < threads; i++) {
131131
struct thread_data *p = data+i;
132132
int err;
@@ -163,7 +163,7 @@ void preload_index(struct index_state *index,
163163
trace2_data_intmax("index", NULL, "preload/sum_lstat", t2_sum_lstat);
164164
trace2_region_leave("index", "preload", NULL);
165165

166-
enable_fscache(0);
166+
disable_fscache();
167167
}
168168

169169
int repo_read_index_preload(struct repository *repo,

read-cache.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
16301630
typechange_fmt = in_porcelain ? "T\t%s\n" : "%s: needs update\n";
16311631
added_fmt = in_porcelain ? "A\t%s\n" : "%s: needs update\n";
16321632
unmerged_fmt = in_porcelain ? "U\t%s\n" : "%s: needs merge\n";
1633-
enable_fscache(1);
1633+
enable_fscache(0);
16341634
/*
16351635
* Use the multi-threaded preload_index() to refresh most of the
16361636
* cache entries quickly then in the single threaded loop below,
@@ -1725,7 +1725,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
17251725
display_progress(progress, istate->cache_nr);
17261726
stop_progress(&progress);
17271727
trace_performance_leave("refresh index");
1728-
enable_fscache(0);
1728+
disable_fscache();
17291729
return has_errors;
17301730
}
17311731

0 commit comments

Comments
 (0)