Skip to content

Commit 152997e

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 0f91efc commit 152997e

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
@@ -550,7 +550,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
550550
die_in_unpopulated_submodule(&the_index, prefix);
551551
die_path_inside_submodule(&the_index, &pathspec);
552552

553-
enable_fscache(1);
553+
enable_fscache(0);
554554
/* We do not really re-read the index but update the up-to-date flags */
555555
preload_index(&the_index, &pathspec, 0);
556556

builtin/checkout.c

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

396-
enable_fscache(1);
396+
enable_fscache(the_index.cache_nr);
397397
for (pos = 0; pos < the_index.cache_nr; pos++) {
398398
struct cache_entry *ce = the_index.cache[pos];
399399
if (ce->ce_flags & CE_MATCHED) {
@@ -418,7 +418,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
418418
errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
419419
NULL, NULL);
420420
mem_pool_discard(&ce_mem_pool, should_validate_cache_entries());
421-
enable_fscache(0);
421+
disable_fscache();
422422
remove_marked_cache_entries(&the_index, 1);
423423
remove_scheduled_dirs();
424424
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
@@ -1554,7 +1554,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
15541554
PATHSPEC_PREFER_FULL,
15551555
prefix, argv);
15561556

1557-
enable_fscache(1);
1557+
enable_fscache(0);
15581558
if (status_format != STATUS_FORMAT_PORCELAIN &&
15591559
status_format != STATUS_FORMAT_PORCELAIN_V2)
15601560
progress_flag = REFRESH_PROGRESS;
@@ -1595,7 +1595,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
15951595
wt_status_print(&s);
15961596
wt_status_collect_free_buffers(&s);
15971597

1598-
enable_fscache(0);
1598+
disable_fscache();
15991599
return 0;
16001600
}
16011601

compat/win32/fscache.c

Lines changed: 6 additions & 2 deletions
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

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
@@ -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

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

1586+
#ifndef disable_fscache
1587+
#define disable_fscache() /* noop */
1588+
#endif
1589+
15861590
#ifndef is_fscache_enabled
15871591
#define is_fscache_enabled(path) (0)
15881592
#endif

preload-index.c

Lines changed: 2 additions & 2 deletions
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

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

0 commit comments

Comments
 (0)