Skip to content

Commit 0d4f6ee

Browse files
benpeartvdye
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 6aab823 commit 0d4f6ee

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
@@ -577,7 +577,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
577577
die_in_unpopulated_submodule(&the_index, prefix);
578578
die_path_inside_submodule(&the_index, &pathspec);
579579

580-
enable_fscache(1);
580+
enable_fscache(0);
581581
/* We do not really re-read the index but update the up-to-date flags */
582582
preload_index(&the_index, &pathspec, 0);
583583

builtin/checkout.c

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

381-
enable_fscache(1);
381+
enable_fscache(active_nr);
382382
for (pos = 0; pos < active_nr; pos++) {
383383
struct cache_entry *ce = active_cache[pos];
384384
if (ce->ce_flags & CE_MATCHED) {
@@ -403,7 +403,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
403403
errs |= run_parallel_checkout(&state, pc_workers, pc_threshold,
404404
NULL, NULL);
405405
mem_pool_discard(&ce_mem_pool, should_validate_cache_entries());
406-
enable_fscache(0);
406+
disable_fscache();
407407
remove_marked_cache_entries(&the_index, 1);
408408
remove_scheduled_dirs();
409409
errs |= finish_delayed_checkout(&state, &nr_checkouts);

builtin/commit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
15381538
PATHSPEC_PREFER_FULL,
15391539
prefix, argv);
15401540

1541-
enable_fscache(1);
1541+
enable_fscache(0);
15421542
if (status_format != STATUS_FORMAT_PORCELAIN &&
15431543
status_format != STATUS_FORMAT_PORCELAIN_V2)
15441544
progress_flag = REFRESH_PROGRESS;
@@ -1579,7 +1579,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
15791579
wt_status_print(&s);
15801580
wt_status_collect_free_buffers(&s);
15811581

1582-
enable_fscache(0);
1582+
disable_fscache();
15831583
return 0;
15841584
}
15851585

compat/win32/fscache.c

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

@@ -413,7 +413,11 @@ int fscache_enable(int enable)
413413
InitializeCriticalSection(&mutex);
414414
lstat_requests = opendir_requests = 0;
415415
fscache_misses = fscache_requests = 0;
416-
hashmap_init(&map, (hashmap_cmp_fn) fsentry_cmp, NULL, 0);
416+
/*
417+
* avoid having to rehash by leaving room for the parent dirs.
418+
* '4' was determined empirically by testing several repos
419+
*/
420+
hashmap_init(&map, (hashmap_cmp_fn) fsentry_cmp, NULL, initial_size * 4);
417421
initialized = 1;
418422
}
419423

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
@@ -683,7 +683,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
683683
save_commit_buffer = 0;
684684

685685
trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
686-
enable_fscache(1);
686+
enable_fscache(0);
687687
for (ref = *refs; ref; ref = ref->next) {
688688
struct object *o;
689689

@@ -706,7 +706,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
706706
cutoff = commit->date;
707707
}
708708
}
709-
enable_fscache(0);
709+
disable_fscache();
710710
trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
711711

712712
/*

git-compat-util.h

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

1334+
#ifndef disable_fscache
1335+
#define disable_fscache() /* noop */
1336+
#endif
1337+
13341338
#ifndef is_fscache_enabled
13351339
#define is_fscache_enabled(path) (0)
13361340
#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
@@ -1579,7 +1579,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
15791579
typechange_fmt = in_porcelain ? "T\t%s\n" : "%s: needs update\n";
15801580
added_fmt = in_porcelain ? "A\t%s\n" : "%s: needs update\n";
15811581
unmerged_fmt = in_porcelain ? "U\t%s\n" : "%s: needs merge\n";
1582-
enable_fscache(1);
1582+
enable_fscache(0);
15831583
/*
15841584
* Use the multi-threaded preload_index() to refresh most of the
15851585
* cache entries quickly then in the single threaded loop below,
@@ -1674,7 +1674,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
16741674
display_progress(progress, istate->cache_nr);
16751675
stop_progress(&progress);
16761676
trace_performance_leave("refresh index");
1677-
enable_fscache(0);
1677+
disable_fscache();
16781678
return has_errors;
16791679
}
16801680

0 commit comments

Comments
 (0)