{"thread":{"id":"66951","subject":"[PATCH] worktree add: skip HEAD reads for destination checks","startedAt":"2026-11-28T23:06:42Z","lastAt":"2026-11-29T05:41:14Z","messageCount":3,"participants":["Maik Riechert via GitGitGadget","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"559621","messageId":"pull.2339.git.1795907202117.gitgitgadget@gmail.com","threadId":"66951","inReplyTo":null,"subject":"[PATCH] worktree add: skip HEAD reads for destination checks","fromName":"Maik Riechert via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-28T23:06:42Z","receivedAt":"2026-11-28T23:06:42Z","isPatch":true,"sender":{"key":"name:Maik Riechert","avatar":null},"body":"From: Maik Riechert <letmaik@outlook.com>\n\nadd_worktree() calls get_worktrees() to check whether the destination\nmatches a registered worktree. This also resolves HEAD for the\nenumerated worktrees, although check_candidate_path() only needs their\npaths, IDs and lock state.\n\nUse get_worktrees_without_reading_head() for the destination check to\navoid these unnecessary lookups.\n\nDetached git worktree add --no-checkout with 200 existing linked\nworktrees. Median of seven runs per version, with warm caches:\n\n              before     after\nfiles         11.13 ms    6.77 ms\nreftable      18.86 ms    7.60 ms\n\nSigned-off-by: Maik Riechert <letmaik@outlook.com>\n---\n    worktree add: skip HEAD reads for destination checks\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-2339%2Fletmaik%2Fworktree-add-path-checks-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2339/letmaik/worktree-add-path-checks-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/2339\n\n builtin/worktree.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/builtin/worktree.c b/builtin/worktree.c\nindex 77ecd0f71f..82a93b0adf 100644\n--- a/builtin/worktree.c\n+++ b/builtin/worktree.c\n@@ -473,7 +473,7 @@ static int add_worktree(const char *path, const char *refname,\n \tstruct ref_store *wt_refs;\n \tstruct repo_config_values *cfg = repo_config_values(the_repository);\n \n-\tworktrees = get_worktrees(the_repository);\n+\tworktrees = get_worktrees_without_reading_head(the_repository);\n \tcheck_candidate_path(path, opts->force, worktrees, \"add\");\n \tfree_worktrees(worktrees);\n \tworktrees = NULL;\n\nbase-commit: acba8bf9753134d9bde89f94369f775838a8c5fe\n-- \ngitgitgadget\n\n"},{"id":"559627","messageId":"xmqqzeusp6ya.fsf@gitster.g","threadId":"66951","inReplyTo":"pull.2339.git.1795907202117.gitgitgadget@gmail.com","subject":"Re: [PATCH] worktree add: skip HEAD reads for destination checks","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-29T05:01:01Z","receivedAt":"2026-11-29T05:01:01Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Maik Riechert via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Maik Riechert <letmaik@outlook.com>\n>\n> add_worktree() calls get_worktrees() to check whether the destination\n> matches a registered worktree. This also resolves HEAD for the\n> enumerated worktrees, although check_candidate_path() only needs their\n> paths, IDs and lock state.\n>\n> Use get_worktrees_without_reading_head() for the destination check to\n> avoid these unnecessary lookups.\n>\n> Detached git worktree add --no-checkout with 200 existing linked\n> worktrees. Median of seven runs per version, with warm caches:\n\nThis is a very minor nit, but the above reads like a note fragment\nwithout a verb.  Something like \"Measured with 'git worktree add\n--detach --no-checkout' on a repository with 200 existing linked\nworktrees (median of seven runs per version, warm caches)\" might read a\nbit more smoothly.\n\n>               before     after\n> files         11.13 ms    6.77 ms\n> reftable      18.86 ms    7.60 ms\n\nNice to see that avoiding extra work yields a simple, straightforward,\nand expected payoff ;-)\n\n\n"},{"id":"559628","messageId":"pull.2339.v2.git.1795930874388.gitgitgadget@gmail.com","threadId":"66951","inReplyTo":"pull.2339.git.1795907202117.gitgitgadget@gmail.com","subject":"[PATCH v2] worktree add: skip HEAD reads for destination checks","fromName":"Maik Riechert via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-29T05:41:14Z","receivedAt":"2026-11-29T05:41:14Z","isPatch":true,"sender":{"key":"name:Maik Riechert","avatar":null},"body":"From: Maik Riechert <letmaik@outlook.com>\n\nadd_worktree() calls get_worktrees() to check whether the destination\nmatches a registered worktree. This also resolves HEAD for the\nenumerated worktrees, although check_candidate_path() only needs their\npaths, IDs and lock state.\n\nUse get_worktrees_without_reading_head() for the destination check to\navoid these unnecessary lookups.\n\nMeasured with 'git worktree add --detach --no-checkout' on a repository\nwith 200 existing linked worktrees (median of seven runs per version,\nwarm caches):\n\n              before     after\nfiles         11.13 ms    6.77 ms\nreftable      18.86 ms    7.60 ms\n\nSigned-off-by: Maik Riechert <letmaik@outlook.com>\n---\n    worktree add: skip HEAD reads for destination checks\n    \n    Changes since v1:\n    \n    Reworded the benchmark introduction as suggested by Junio. No code\n    changes.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-2339%2Fletmaik%2Fworktree-add-path-checks-v2\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2339/letmaik/worktree-add-path-checks-v2\nPull-Request: https://github.com/gitgitgadget/git/pull/2339\n\nRange-diff vs v1:\n\n 1:  17946461ad ! 1:  054d208b66 worktree add: skip HEAD reads for destination checks\n     @@ Commit message\n          Use get_worktrees_without_reading_head() for the destination check to\n          avoid these unnecessary lookups.\n      \n     -    Detached git worktree add --no-checkout with 200 existing linked\n     -    worktrees. Median of seven runs per version, with warm caches:\n     +    Measured with 'git worktree add --detach --no-checkout' on a repository\n     +    with 200 existing linked worktrees (median of seven runs per version,\n     +    warm caches):\n      \n                        before     after\n          files         11.13 ms    6.77 ms\n\n\n builtin/worktree.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/builtin/worktree.c b/builtin/worktree.c\nindex 77ecd0f71f..82a93b0adf 100644\n--- a/builtin/worktree.c\n+++ b/builtin/worktree.c\n@@ -473,7 +473,7 @@ static int add_worktree(const char *path, const char *refname,\n \tstruct ref_store *wt_refs;\n \tstruct repo_config_values *cfg = repo_config_values(the_repository);\n \n-\tworktrees = get_worktrees(the_repository);\n+\tworktrees = get_worktrees_without_reading_head(the_repository);\n \tcheck_candidate_path(path, opts->force, worktrees, \"add\");\n \tfree_worktrees(worktrees);\n \tworktrees = NULL;\n\nbase-commit: acba8bf9753134d9bde89f94369f775838a8c5fe\n-- \ngitgitgadget\n\n"}]}