{"thread":{"id":"66732","subject":"[PATCH] stash: reuse prepared state when creating stash","startedAt":"2026-10-31T20:01:07Z","lastAt":"2026-11-09T05:09:19Z","messageCount":3,"participants":["jasmeet.bhatia.us@gmail.com","Phillip Wood"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"557047","messageId":"20261031200107.285168-1-jasmeet.bhatia.us@gmail.com","threadId":"66732","inReplyTo":null,"subject":"[PATCH] stash: reuse prepared state when creating stash","fromName":"","fromEmail":"jasmeet.bhatia.us@gmail.com","sentAt":"2026-10-31T20:01:07Z","receivedAt":"2026-10-31T20:01:07Z","isPatch":true,"sender":{"key":"jasmeet.bhatia.us@gmail.com","avatar":null},"body":"From: \"Jasmeet (Jazz) Bhatia\" <jasmeet.bhatia.us@gmail.com>\n\ndo_push_stash() already preloads and refreshes the index and calls\ncheck_changes() before invoking do_create_stash(). However, Git then\nrepeats this same index preparation and change detection, including\nrebuilding the list of untracked files when requested, which is\nduplicate work.\n\nInstead, avoid this duplicate work by passing the already prepared\nuntracked file list to do_create_stash(). When the list is provided,\nreuse it and skip the redundant index preload, refresh, and\ncheck_changes() call.\n\nThis maintains the existing behavior of \"git stash create\", which\ninvokes do_create_stash() without prepared state and thus continues\nto perform its own index preparation and change detection.\n\nTesting this using the p3904 stash performance tests shows a decent\nperformance boost.\n\n    stash -p, no fsmonitor\n    0.12(0.11+0.17) -> 0.10(0.10+0.18)  -16.7%\n\n    stash -p, builtin fsmonitor\n    0.11(0.10+0.02) -> 0.09(0.08+0.01)  -18.2%\n\nTesting this on the Linux kernel on my system, \"git stash push\"\nimproved from 254 ms to about 239 ms, which is about 6%.\n\nSigned-off-by: Jasmeet (Jazz) Bhatia <jasmeet.bhatia.us@gmail.com>\n---\n builtin/stash.c | 25 +++++++++++++++----------\n 1 file changed, 15 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex e1a3a76bd0..58eb782597 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -1459,7 +1459,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n \t\t\t   int include_untracked, int patch_mode,\n \t\t\t   struct interactive_options *interactive_opts,\n \t\t\t   int only_staged, struct stash_info *info, struct strbuf *patch,\n-\t\t\t   int quiet)\n+\t\t\t   int quiet, struct strbuf *prepared_untracked_files)\n {\n \tint ret = 0;\n \tint flags = 0;\n@@ -1473,14 +1473,17 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n \tstruct strbuf msg = STRBUF_INIT;\n \tstruct strbuf commit_tree_label = STRBUF_INIT;\n \tstruct strbuf untracked_files = STRBUF_INIT;\n+\tstruct strbuf *untracked_files_p = prepared_untracked_files ? prepared_untracked_files : &untracked_files;\n \n \tprepare_fallback_ident(\"git stash\", \"git@stash\");\n \n-\trepo_read_index_preload(the_repository, NULL, 0);\n-\tif (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,\n-\t\t\t\t\t NULL, NULL, NULL) < 0) {\n-\t\tret = error(_(\"could not write index\"));\n-\t\tgoto done;\n+\tif (!prepared_untracked_files) {\n+\t\trepo_read_index_preload(the_repository, NULL, 0);\n+\t\tif (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,\n+\t\t\t\t\t\t NULL, NULL, NULL) < 0) {\n+\t\t\tret = error(_(\"could not write index\"));\n+\t\t\tgoto done;\n+\t\t}\n \t}\n \n \tif (repo_get_oid(the_repository, \"HEAD\", &info->b_commit)) {\n@@ -1493,7 +1496,8 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n \t\thead_commit = lookup_commit(the_repository, &info->b_commit);\n \t}\n \n-\tif (!check_changes(ps, include_untracked, &untracked_files)) {\n+\tif (!prepared_untracked_files &&\n+\t    !check_changes(ps, include_untracked, untracked_files_p)) {\n \t\tret = 1;\n \t\tgoto done;\n \t}\n@@ -1529,7 +1533,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n \tparents = NULL;\n \n \tif (include_untracked) {\n-\t\tif (save_untracked_files(info, &msg, &untracked_files)) {\n+\t\tif (save_untracked_files(info, &msg, untracked_files_p)) {\n \t\t\tif (!quiet)\n \t\t\t\tfprintf_ln(stderr, _(\"Cannot save \"\n \t\t\t\t\t\t     \"the untracked files\"));\n@@ -1615,7 +1619,7 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n \t\treturn 0;\n \n \tret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n-\t\t\t      NULL, 0);\n+\t\t\t      NULL, 0, NULL);\n \tif (!ret)\n \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n \n@@ -1698,7 +1702,8 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n \tif (stash_msg)\n \t\tstrbuf_addstr(&stash_msg_buf, stash_msg);\n \tif (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,\n-\t\t\t    interactive_opts, only_staged, &info, &patch, quiet)) {\n+\t\t\t    interactive_opts, only_staged, &info, &patch, quiet,\n+\t\t\t    &untracked_files)) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n-- \n2.56.0\n\n\n"},{"id":"557179","messageId":"845be1de-8d12-41c9-996d-038886bde4cf@gmail.com","threadId":"66732","inReplyTo":"20261031200107.285168-1-jasmeet.bhatia.us@gmail.com","subject":"Re: [PATCH] stash: reuse prepared state when creating stash","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-11-02T14:14:57Z","receivedAt":"2026-11-02T14:14:57Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Jasmeet\n\nOn 31/10/2026 20:01, jasmeet.bhatia.us@gmail.com wrote:\n> From: \"Jasmeet (Jazz) Bhatia\" <jasmeet.bhatia.us@gmail.com>\n> > do_push_stash() already preloads and refreshes the index and calls\n> check_changes() before invoking do_create_stash(). However, Git then\n> repeats this same index preparation and change detection, including\n> rebuilding the list of untracked files when requested, which is\n> duplicate work.\n\nGood observation\n\n> Instead, avoid this duplicate work by passing the already prepared\n> untracked file list to do_create_stash(). When the list is provided,\n> reuse it and skip the redundant index preload, refresh, and\n> check_changes() call.\n\nI've got some patches in \"seen\" [1] that take a slightly different approach and keep the call to check_changes() in do_create_stash() and remove it from create_stash() and do_push_stash() instead. That series also removes a number of unnecessary calls to preload_index(). If you have time to post a review that would be really helpful.\n\nThanks\n\nPhillip\n\n[1] The patches are at https://lore.kernel.org/git/cover.1792767794.git.phillip.wood@dunelm.org.uk/ and are merged into seen as branch 'pw/stash-change-check-only-once'\n\n\n> This maintains the existing behavior of \"git stash create\", which\n> invokes do_create_stash() without prepared state and thus continues\n> to perform its own index preparation and change detection.\n> > Testing this using the p3904 stash performance tests shows a decent\n> performance boost.\n> >      stash -p, no fsmonitor\n>      0.12(0.11+0.17) -> 0.10(0.10+0.18)  -16.7%\n> >      stash -p, builtin fsmonitor\n>      0.11(0.10+0.02) -> 0.09(0.08+0.01)  -18.2%\n> > Testing this on the Linux kernel on my system, \"git stash push\"\n> improved from 254 ms to about 239 ms, which is about 6%.\n> > Signed-off-by: Jasmeet (Jazz) Bhatia <jasmeet.bhatia.us@gmail.com>\n> ---\n>   builtin/stash.c | 25 +++++++++++++++----------\n>   1 file changed, 15 insertions(+), 10 deletions(-)\n> > diff --git a/builtin/stash.c b/builtin/stash.c\n> index e1a3a76bd0..58eb782597 100644\n> --- a/builtin/stash.c\n> +++ b/builtin/stash.c\n> @@ -1459,7 +1459,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n>   \t\t\t   int include_untracked, int patch_mode,\n>   \t\t\t   struct interactive_options *interactive_opts,\n>   \t\t\t   int only_staged, struct stash_info *info, struct strbuf *patch,\n> -\t\t\t   int quiet)\n> +\t\t\t   int quiet, struct strbuf *prepared_untracked_files)\n>   {\n>   \tint ret = 0;\n>   \tint flags = 0;\n> @@ -1473,14 +1473,17 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n>   \tstruct strbuf msg = STRBUF_INIT;\n>   \tstruct strbuf commit_tree_label = STRBUF_INIT;\n>   \tstruct strbuf untracked_files = STRBUF_INIT;\n> +\tstruct strbuf *untracked_files_p = prepared_untracked_files ? prepared_untracked_files : &untracked_files;\n>   >   \tprepare_fallback_ident(\"git stash\", \"git@stash\");\n>   > -\trepo_read_index_preload(the_repository, NULL, 0);\n> -\tif (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,\n> -\t\t\t\t\t NULL, NULL, NULL) < 0) {\n> -\t\tret = error(_(\"could not write index\"));\n> -\t\tgoto done;\n> +\tif (!prepared_untracked_files) {\n> +\t\trepo_read_index_preload(the_repository, NULL, 0);\n> +\t\tif (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,\n> +\t\t\t\t\t\t NULL, NULL, NULL) < 0) {\n> +\t\t\tret = error(_(\"could not write index\"));\n> +\t\t\tgoto done;\n> +\t\t}\n>   \t}\n>   >   \tif (repo_get_oid(the_repository, \"HEAD\", &info->b_commit)) {\n> @@ -1493,7 +1496,8 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n>   \t\thead_commit = lookup_commit(the_repository, &info->b_commit);\n>   \t}\n>   > -\tif (!check_changes(ps, include_untracked, &untracked_files)) {\n> +\tif (!prepared_untracked_files &&\n> +\t    !check_changes(ps, include_untracked, untracked_files_p)) {\n>   \t\tret = 1;\n>   \t\tgoto done;\n>   \t}\n> @@ -1529,7 +1533,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n>   \tparents = NULL;\n>   >   \tif (include_untracked) {\n> -\t\tif (save_untracked_files(info, &msg, &untracked_files)) {\n> +\t\tif (save_untracked_files(info, &msg, untracked_files_p)) {\n>   \t\t\tif (!quiet)\n>   \t\t\t\tfprintf_ln(stderr, _(\"Cannot save \"\n>   \t\t\t\t\t\t     \"the untracked files\"));\n> @@ -1615,7 +1619,7 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n>   \t\treturn 0;\n>   >   \tret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n> -\t\t\t      NULL, 0);\n> +\t\t\t      NULL, 0, NULL);\n>   \tif (!ret)\n>   \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n>   > @@ -1698,7 +1702,8 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n>   \tif (stash_msg)\n>   \t\tstrbuf_addstr(&stash_msg_buf, stash_msg);\n>   \tif (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,\n> -\t\t\t    interactive_opts, only_staged, &info, &patch, quiet)) {\n> +\t\t\t    interactive_opts, only_staged, &info, &patch, quiet,\n> +\t\t\t    &untracked_files)) {\n>   \t\tret = -1;\n>   \t\tgoto done;\n>   \t}\n\n\n"},{"id":"557870","messageId":"jerc-18e648444c4a0fd6-bf304-0@gmail.com","threadId":"66732","inReplyTo":"845be1de-8d12-41c9-996d-038886bde4cf@gmail.com","subject":"Re: [PATCH] stash: reuse prepared state when creating stash","fromName":"","fromEmail":"jasmeet.bhatia.us@gmail.com","sentAt":"2026-11-09T05:09:19Z","receivedAt":"2026-11-09T05:09:19Z","isPatch":true,"sender":{"key":"jasmeet.bhatia.us@gmail.com","avatar":null},"body":"Hi Phillip,\n\nOn Mon Nov 2, 2026 at 6:14 AM -08:00, Phillip Wood wrote:\n\n> I've got some patches in \"seen\" [1] that take a slightly different \n> approach and keep the call to check_changes() in do_create_stash() and \n> remove it from create_stash() and do_push_stash() instead. That series \n> also removes a number of unnecessary calls to preload_index(). If you \n> have time to post a review that would be really helpful.\n\nThanks for the opportunity to contribute! I'll take a look in the coming days and let you know any thoughts I have.\n\nThanks,\n\nJazz\n\n"}]}