Skip to content

Commit d0187ae

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 e6ad839 commit d0187ae

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
@@ -602,7 +602,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
602602
die_in_unpopulated_submodule(&the_index, prefix);
603603
die_path_inside_submodule(&the_index, &pathspec);
604604

605-
enable_fscache(1);
605+
enable_fscache(0);
606606
/* We do not really re-read the index but update the up-to-date flags */
607607
preload_index(&the_index, &pathspec, 0);
608608

builtin/checkout.c

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

389-
enable_fscache(1);
389+
enable_fscache(active_nr);
390390
for (pos = 0; pos < active_nr; pos++) {
391391
struct cache_entry *ce = active_cache[pos];
392392
if (ce->ce_flags & CE_MATCHED) {
@@ -411,7 +411,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
411411
errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
412412
NULL, NULL);
413413
mem_pool_discard(&ce_mem_pool, should_validate_cache_entries());
414-
enable_fscache(0);
414+
disable_fscache();
415415
remove_marked_cache_entries(&the_index, 1);
416416
remove_scheduled_dirs();
417417
errs |= finish_delayed_checkout(&state, &nr_checkouts, opts->show_progress);

builtin/commit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
15411541
PATHSPEC_PREFER_FULL,
15421542
prefix, argv);
15431543

1544-
enable_fscache(1);
1544+
enable_fscache(0);
15451545
if (status_format != STATUS_FORMAT_PORCELAIN &&
15461546
status_format != STATUS_FORMAT_PORCELAIN_V2)
15471547
progress_flag = REFRESH_PROGRESS;
@@ -1582,7 +1582,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
15821582
wt_status_print(&s);
15831583
wt_status_collect_free_buffers(&s);
15841584

1585-
enable_fscache(0);
1585+
disable_fscache();
15861586
return 0;
15871587
}
15881588

compat/win32/fscache.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ static struct fsentry *fscache_get(struct fsentry *key)
402402
* Enables or disables the cache. Note that the cache is read-only, changes to
403403
* the working directory are NOT reflected in the cache while enabled.
404404
*/
405-
int fscache_enable(int enable)
405+
int fscache_enable(int enable, size_t initial_size)
406406
{
407407
int result;
408408

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

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
@@ -695,7 +695,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
695695
save_commit_buffer = 0;
696696

697697
trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
698-
enable_fscache(1);
698+
enable_fscache(0);
699699
for (ref = *refs; ref; ref = ref->next) {
700700
struct object *o;
701701

@@ -718,7 +718,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
718718
cutoff = commit->date;
719719
}
720720
}
721-
enable_fscache(0);
721+
disable_fscache();
722722
trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
723723

724724
/*

git-compat-util.h

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

1397+
#ifndef disable_fscache
1398+
#define disable_fscache() /* noop */
1399+
#endif
1400+
13971401
#ifndef is_fscache_enabled
13981402
#define is_fscache_enabled(path) (0)
13991403
#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;
@@ -157,7 +157,7 @@ void preload_index(struct index_state *index,
157157
trace2_data_intmax("index", NULL, "preload/sum_lstat", t2_sum_lstat);
158158
trace2_region_leave("index", "preload", NULL);
159159

160-
enable_fscache(0);
160+
disable_fscache();
161161
}
162162

163163
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
@@ -1607,7 +1607,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
16071607
typechange_fmt = in_porcelain ? "T\t%s\n" : "%s: needs update\n";
16081608
added_fmt = in_porcelain ? "A\t%s\n" : "%s: needs update\n";
16091609
unmerged_fmt = in_porcelain ? "U\t%s\n" : "%s: needs merge\n";
1610-
enable_fscache(1);
1610+
enable_fscache(0);
16111611
/*
16121612
* Use the multi-threaded preload_index() to refresh most of the
16131613
* cache entries quickly then in the single threaded loop below,
@@ -1702,7 +1702,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
17021702
display_progress(progress, istate->cache_nr);
17031703
stop_progress(&progress);
17041704
trace_performance_leave("refresh index");
1705-
enable_fscache(0);
1705+
disable_fscache();
17061706
return has_errors;
17071707
}
17081708

0 commit comments

Comments
 (0)