From: Junio C Hamano Date: Wed, 14 Oct 2026 18:09:40 GMT Subject: Re: [PATCH v2 4/7] stash push: avoid duplicate calls to preload_index() Message-ID: In-Reply-To: <6ca7e1c567a9e31d6668e59df81cd793e2b3411e.1791992404.git.phillip.wood@dunelm.org.uk> Phillip Wood writes: > From: Phillip Wood > > do_stash_push() needs to load the index so it can check that any Perhaps you meant do_push_stash()? > pathspec argument refers to tracked files. To do that it calls > repo_read_index_preload() which does not just load the index, it also > calls preload_index() which lstat()s all the files to see which ones > are up-to-date. do_create_stash() then calls repo_read_index_preload() > for a second time which lstat()s all the changed paths again and then > calls repo_refresh_and_write_index() which invokes preload_index() for a > third time. So we're preloading the index three times when we only need > to do it once. Avoid this by making the callers of do_create_stash() > responsible for reading the index and use repo_read_index() rather > than repo_read_index_preload(). This saves two lstat() calls per > changed path. The other caller of do_create_stash() is create_stash(). And it does not need to call the preload version for the same reason. > Signed-off-by: Phillip Wood > --- > builtin/stash.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/builtin/stash.c b/builtin/stash.c > index aec79ed473..b4fe7465e4 100644 > --- a/builtin/stash.c > +++ b/builtin/stash.c > @@ -1526,7 +1526,6 @@ static enum create_result do_create_stash(const struct pathspec *ps, > > prepare_fallback_ident("git stash", "git@stash"); > > - repo_read_index_preload(the_repository, NULL, 0); > if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0, > NULL, NULL, NULL) < 0) { > ret = error(_("could not write index")); > @@ -1662,6 +1661,7 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED, > > memset(&ps, 0, sizeof(ps)); > > + repo_read_index(the_repository); > ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info, > NULL, 0); > if (ret == CREATE_SUCCESS) > @@ -1707,7 +1707,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q > goto done; > } > > - repo_read_index_preload(the_repository, NULL, 0); > + repo_read_index(the_repository); > if (!include_untracked && ps->nr) { > char *ps_matched = xcalloc(ps->nr, 1);