Skip to content

Commit 637ae9e

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 5e0e39a commit 637ae9e

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

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

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, opts->show_progress);

builtin/commit.c

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

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

1583-
enable_fscache(0);
1583+
disable_fscache();
15841584
return 0;
15851585
}
15861586

compat/win32/fscache.c

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

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

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
@@ -1333,6 +1333,10 @@ static inline int is_missing_file_error(int errno_)
13331333
#define enable_fscache(x) /* noop */
13341334
#endif
13351335

1336+
#ifndef disable_fscache
1337+
#define disable_fscache() /* noop */
1338+
#endif
1339+
13361340
#ifndef is_fscache_enabled
13371341
#define is_fscache_enabled(path) (0)
13381342
#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)