Skip to content

Commit 6281519

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 e9afcc3 commit 6281519

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
@@ -597,7 +597,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
597597
die_in_unpopulated_submodule(&the_index, prefix);
598598
die_path_inside_submodule(&the_index, &pathspec);
599599

600-
enable_fscache(1);
600+
enable_fscache(0);
601601
/* We do not really re-read the index but update the up-to-date flags */
602602
preload_index(&the_index, &pathspec, 0);
603603

builtin/checkout.c

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

394-
enable_fscache(1);
394+
enable_fscache(active_nr);
395395
for (pos = 0; pos < active_nr; pos++) {
396396
struct cache_entry *ce = active_cache[pos];
397397
if (ce->ce_flags & CE_MATCHED) {
@@ -416,7 +416,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
416416
errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
417417
NULL, NULL);
418418
mem_pool_discard(&ce_mem_pool, should_validate_cache_entries());
419-
enable_fscache(0);
419+
disable_fscache();
420420
remove_marked_cache_entries(&the_index, 1);
421421
remove_scheduled_dirs();
422422
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
@@ -1540,7 +1540,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
15401540
PATHSPEC_PREFER_FULL,
15411541
prefix, argv);
15421542

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

1584-
enable_fscache(0);
1584+
disable_fscache();
15851585
return 0;
15861586
}
15871587

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
@@ -700,7 +700,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
700700
save_commit_buffer = 0;
701701

702702
trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
703-
enable_fscache(1);
703+
enable_fscache(0);
704704
for (ref = *refs; ref; ref = ref->next) {
705705
struct commit *commit;
706706

@@ -727,7 +727,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
727727
if (!cutoff || cutoff < commit->date)
728728
cutoff = commit->date;
729729
}
730-
enable_fscache(0);
730+
disable_fscache();
731731
trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
732732

733733
/*

git-compat-util.h

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

1424+
#ifndef disable_fscache
1425+
#define disable_fscache() /* noop */
1426+
#endif
1427+
14241428
#ifndef is_fscache_enabled
14251429
#define is_fscache_enabled(path) (0)
14261430
#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
@@ -1625,7 +1625,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
16251625
typechange_fmt = in_porcelain ? "T\t%s\n" : "%s: needs update\n";
16261626
added_fmt = in_porcelain ? "A\t%s\n" : "%s: needs update\n";
16271627
unmerged_fmt = in_porcelain ? "U\t%s\n" : "%s: needs merge\n";
1628-
enable_fscache(1);
1628+
enable_fscache(0);
16291629
/*
16301630
* Use the multi-threaded preload_index() to refresh most of the
16311631
* cache entries quickly then in the single threaded loop below,
@@ -1720,7 +1720,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
17201720
display_progress(progress, istate->cache_nr);
17211721
stop_progress(&progress);
17221722
trace_performance_leave("refresh index");
1723-
enable_fscache(0);
1723+
disable_fscache();
17241724
return has_errors;
17251725
}
17261726

0 commit comments

Comments
 (0)