Skip to content

Commit 0caecb3

Browse files
benpeartderrickstolee
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 6dcb4a2 commit 0caecb3

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
@@ -534,7 +534,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
534534
die_in_unpopulated_submodule(&the_index, prefix);
535535
die_path_inside_submodule(&the_index, &pathspec);
536536

537-
enable_fscache(1);
537+
enable_fscache(0);
538538
/* We do not really re-read the index but update the up-to-date flags */
539539
preload_index(&the_index, &pathspec, 0);
540540

builtin/checkout.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
367367
NULL);
368368

369369
enable_delayed_checkout(&state);
370-
enable_fscache(1);
370+
enable_fscache(active_nr);
371371
for (pos = 0; pos < active_nr; pos++) {
372372
struct cache_entry *ce = active_cache[pos];
373373
if (ce->ce_flags & CE_MATCHED) {
@@ -387,7 +387,7 @@ static int checkout_worktree(const struct checkout_opts *opts,
387387
pos = skip_same_name(ce, pos) - 1;
388388
}
389389
}
390-
enable_fscache(0);
390+
disable_fscache();
391391
remove_marked_cache_entries(&the_index, 1);
392392
remove_scheduled_dirs();
393393
errs |= finish_delayed_checkout(&state, &nr_checkouts);

builtin/commit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
14201420
PATHSPEC_PREFER_FULL,
14211421
prefix, argv);
14221422

1423-
enable_fscache(1);
1423+
enable_fscache(0);
14241424
if (status_format != STATUS_FORMAT_PORCELAIN &&
14251425
status_format != STATUS_FORMAT_PORCELAIN_V2)
14261426
progress_flag = REFRESH_PROGRESS;
@@ -1461,7 +1461,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
14611461
wt_status_print(&s);
14621462
wt_status_collect_free_buffers(&s);
14631463

1464-
enable_fscache(0);
1464+
disable_fscache();
14651465
return 0;
14661466
}
14671467

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
@@ -672,7 +672,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
672672
save_commit_buffer = 0;
673673

674674
trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
675-
enable_fscache(1);
675+
enable_fscache(0);
676676
for (ref = *refs; ref; ref = ref->next) {
677677
struct object *o;
678678

@@ -695,7 +695,7 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
695695
cutoff = commit->date;
696696
}
697697
}
698-
enable_fscache(0);
698+
disable_fscache();
699699
trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
700700

701701
/*

git-compat-util.h

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

1297+
#ifndef disable_fscache
1298+
#define disable_fscache() /* noop */
1299+
#endif
1300+
12971301
#ifndef is_fscache_enabled
12981302
#define is_fscache_enabled(path) (0)
12991303
#endif

preload-index.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void preload_index(struct index_state *index,
120120
pthread_mutex_init(&pd.mutex, NULL);
121121
}
122122

123-
enable_fscache(1);
123+
enable_fscache(index->cache_nr);
124124
for (i = 0; i < threads; i++) {
125125
struct thread_data *p = data+i;
126126
int err;
@@ -146,7 +146,7 @@ void preload_index(struct index_state *index,
146146
stop_progress(&pd.progress);
147147

148148
trace_performance_leave("preload index");
149-
enable_fscache(0);
149+
disable_fscache();
150150
}
151151

152152
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
@@ -1542,7 +1542,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
15421542
typechange_fmt = in_porcelain ? "T\t%s\n" : "%s: needs update\n";
15431543
added_fmt = in_porcelain ? "A\t%s\n" : "%s: needs update\n";
15441544
unmerged_fmt = in_porcelain ? "U\t%s\n" : "%s: needs merge\n";
1545-
enable_fscache(1);
1545+
enable_fscache(0);
15461546
/*
15471547
* Use the multi-threaded preload_index() to refresh most of the
15481548
* cache entries quickly then in the single threaded loop below,
@@ -1620,7 +1620,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
16201620
stop_progress(&progress);
16211621
}
16221622
trace_performance_leave("refresh index");
1623-
enable_fscache(0);
1623+
disable_fscache();
16241624
return has_errors;
16251625
}
16261626

0 commit comments

Comments
 (0)