* [PATCH] refs: exit early from the loop if it is not a main worktree
@ 2024-12-18 2:20 AreaZR via GitGitGadget
2024-12-18 13:31 ` shejialuo
0 siblings, 1 reply; 8+ messages in thread
From: AreaZR via GitGitGadget @ 2024-12-18 2:20 UTC (permalink / raw)
To: git; +Cc: AreaZR, Seija Kijin
From: Seija Kijin <doremylover123@gmail.com>
The is_main_worktree function just checks for !wt->id,
but the compiler doesn't know this as it is in a different
file, so just exit out early.
Signed-off-by: Seija Kijin <doremylover123@gmail.com>
---
refs: exit early from the loop if it is not a main worktree
The is_main_worktree function just checks for !wt->id, but the compiler
doesn't know this as it is in a different file, so just exit out early.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1848%2FAreaZR%2Fexit-early-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1848/AreaZR/exit-early-v1
Pull-Request: https://github.com/git/git/pull/1848
refs.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/refs.c b/refs.c
index 8b713692359..cce63a618d7 100644
--- a/refs.c
+++ b/refs.c
@@ -2791,6 +2791,7 @@ static int has_worktrees(void)
if (is_main_worktree(worktrees[i]))
continue;
ret = 1;
+ break;
}
free_worktrees(worktrees);
base-commit: 063bcebf0c917140ca0e705cbe0fdea127e90086
--
gitgitgadget
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH] refs: exit early from the loop if it is not a main worktree 2024-12-18 2:20 [PATCH] refs: exit early from the loop if it is not a main worktree AreaZR via GitGitGadget @ 2024-12-18 13:31 ` shejialuo 2024-12-18 23:52 ` Eric Sunshine 0 siblings, 1 reply; 8+ messages in thread From: shejialuo @ 2024-12-18 13:31 UTC (permalink / raw) To: AreaZR via GitGitGadget; +Cc: git, AreaZR, Seija Kijin, Patrick Steinhardt On Wed, Dec 18, 2024 at 02:20:45AM +0000, AreaZR via GitGitGadget wrote: > From: Seija Kijin <doremylover123@gmail.com> > > The is_main_worktree function just checks for !wt->id, > but the compiler doesn't know this as it is in a different > file, so just exit out early. > I think maybe we should exit out the loop early. However, the above statement is confusing. As you have said, `is_main_worktree` checks whether the `wt->id` is NULL. Why compiler doesn't know this? And why we need to exit out the loop due to above reason? > Signed-off-by: Seija Kijin <doremylover123@gmail.com> > --- > refs: exit early from the loop if it is not a main worktree > > The is_main_worktree function just checks for !wt->id, but the compiler > doesn't know this as it is in a different file, so just exit out early. > > Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1848%2FAreaZR%2Fexit-early-v1 > Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1848/AreaZR/exit-early-v1 > Pull-Request: https://github.com/git/git/pull/1848 > > refs.c | 1 + > 1 file changed, 1 insertion(+) > > diff --git a/refs.c b/refs.c > index 8b713692359..cce63a618d7 100644 > --- a/refs.c > +++ b/refs.c > @@ -2791,6 +2791,7 @@ static int has_worktrees(void) > if (is_main_worktree(worktrees[i])) > continue; > ret = 1; > + break; So, when we find a linked worktree, we just return the value. From my perspective, if we decide to optimize like this way, we could drop the loop because the first element of the result of `get_worktrees` is the main worktree. And we could just check whether the "worktrees[1]" is NULL to do above. However, I don't know whether it's a good idea to exit the loop early in the first place. CC Patrick to help. Thanks, Jialuo ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] refs: exit early from the loop if it is not a main worktree 2024-12-18 13:31 ` shejialuo @ 2024-12-18 23:52 ` Eric Sunshine 2024-12-19 1:10 ` Junio C Hamano 2024-12-27 14:34 ` Patrick Steinhardt 0 siblings, 2 replies; 8+ messages in thread From: Eric Sunshine @ 2024-12-18 23:52 UTC (permalink / raw) To: shejialuo Cc: AreaZR via GitGitGadget, git, AreaZR, Seija Kijin, Patrick Steinhardt On Wed, Dec 18, 2024 at 8:30 AM shejialuo <shejialuo@gmail.com> wrote: > On Wed, Dec 18, 2024 at 02:20:45AM +0000, AreaZR via GitGitGadget wrote: > > if (is_main_worktree(worktrees[i])) > > continue; > > ret = 1; > > + break; > > So, when we find a linked worktree, we just return the value. From my > perspective, if we decide to optimize like this way, we could drop the > loop because the first element of the result of `get_worktrees` is the > main worktree. And we could just check whether the "worktrees[1]" is > NULL to do above. You're correct. get_worktrees() guarantees that the main worktree (or bare repository) is the first item in the list, so merely checking whether `worktrees[1]` is non-NULL would be sufficient to answer whether linked worktrees are present; no looping is required. > However, I don't know whether it's a good idea to exit the loop early > in the first place. CC Patrick to help. If the loop is retained for some reason (though it really isn't needed), then exiting early is indeed desirable. I suspect that the missing `break` was just a silly oversight on Patrick's part. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] refs: exit early from the loop if it is not a main worktree 2024-12-18 23:52 ` Eric Sunshine @ 2024-12-19 1:10 ` Junio C Hamano 2024-12-19 5:54 ` Eric Sunshine 2024-12-27 14:34 ` Patrick Steinhardt 1 sibling, 1 reply; 8+ messages in thread From: Junio C Hamano @ 2024-12-19 1:10 UTC (permalink / raw) To: Eric Sunshine Cc: shejialuo, AreaZR via GitGitGadget, git, AreaZR, Seija Kijin, Patrick Steinhardt Eric Sunshine <sunshine@sunshineco.com> writes: > On Wed, Dec 18, 2024 at 8:30 AM shejialuo <shejialuo@gmail.com> wrote: >> On Wed, Dec 18, 2024 at 02:20:45AM +0000, AreaZR via GitGitGadget wrote: >> > if (is_main_worktree(worktrees[i])) >> > continue; >> > ret = 1; >> > + break; >> >> So, when we find a linked worktree, we just return the value. From my >> perspective, if we decide to optimize like this way, we could drop the >> loop because the first element of the result of `get_worktrees` is the >> main worktree. And we could just check whether the "worktrees[1]" is >> NULL to do above. > > You're correct. get_worktrees() guarantees that the main worktree (or > bare repository) is the first item in the list, so merely checking > whether `worktrees[1]` is non-NULL would be sufficient to answer > whether linked worktrees are present; no looping is required. Thanks for a well-reasoned write-up. Would many other callers potentially want to know if the repository has more than one worktree? It looks to me that the has_worktrees() helper function in refs.c is a sign that the worktree API is missing a function. Calling get_worktrees() to prepare a list of worktrees and then counting the result, only to see if there are more than one, sounds a bit wasteful if we need to do so too often. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] refs: exit early from the loop if it is not a main worktree 2024-12-19 1:10 ` Junio C Hamano @ 2024-12-19 5:54 ` Eric Sunshine 2024-12-19 6:12 ` Junio C Hamano 2024-12-27 14:34 ` Patrick Steinhardt 0 siblings, 2 replies; 8+ messages in thread From: Eric Sunshine @ 2024-12-19 5:54 UTC (permalink / raw) To: Junio C Hamano Cc: shejialuo, AreaZR via GitGitGadget, git, AreaZR, Seija Kijin, Patrick Steinhardt On Wed, Dec 18, 2024 at 8:10 PM Junio C Hamano <gitster@pobox.com> wrote: > Eric Sunshine <sunshine@sunshineco.com> writes: > > On Wed, Dec 18, 2024 at 8:30 AM shejialuo <shejialuo@gmail.com> wrote: > >> On Wed, Dec 18, 2024 at 02:20:45AM +0000, AreaZR via GitGitGadget wrote: > >> > if (is_main_worktree(worktrees[i])) > >> > continue; > >> > ret = 1; > >> > + break; > >> > >> So, when we find a linked worktree, we just return the value. From my > >> perspective, if we decide to optimize like this way, we could drop the > >> loop because the first element of the result of `get_worktrees` is the > >> main worktree. And we could just check whether the "worktrees[1]" is > >> NULL to do above. > > > > You're correct. get_worktrees() guarantees that the main worktree (or > > bare repository) is the first item in the list, so merely checking > > whether `worktrees[1]` is non-NULL would be sufficient to answer > > whether linked worktrees are present; no looping is required. > > Would many other callers potentially want to know if the repository > has more than one worktree? It looks to me that the has_worktrees() > helper function in refs.c is a sign that the worktree API is missing > a function. Calling get_worktrees() to prepare a list of worktrees > and then counting the result, only to see if there are more than > one, sounds a bit wasteful if we need to do so too often. If the need to answer this question does become common, then I can imagine a function being added to the worktree API which tries to be smart about it by only calling readdir() -- and validating a .git/worktrees/<id>/ metainformation -- enough times to be able to answer the question. However, although I haven't audited the code, I suspect the question "are there any linked worktrees" is rare, possibly only asked by `refs.c`. And in that case, it is asked only at the start of a refs-migration operation. Moreover, it appears that even that case of asking the question is probably temporary, existing only until someone extends the migration logic to work correctly in the presence of worktrees. (I'm sure Patrick can shed more light on this, though.) ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] refs: exit early from the loop if it is not a main worktree 2024-12-19 5:54 ` Eric Sunshine @ 2024-12-19 6:12 ` Junio C Hamano 2024-12-27 14:34 ` Patrick Steinhardt 1 sibling, 0 replies; 8+ messages in thread From: Junio C Hamano @ 2024-12-19 6:12 UTC (permalink / raw) To: Eric Sunshine Cc: shejialuo, AreaZR via GitGitGadget, git, AreaZR, Seija Kijin, Patrick Steinhardt Eric Sunshine <sunshine@sunshineco.com> writes: > However, although I haven't audited the code, I suspect the question > "are there any linked worktrees" is rare, possibly only asked by > `refs.c`. And in that case, it is asked only at the start of a > refs-migration operation. That matches my gut feeling. Thanks. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] refs: exit early from the loop if it is not a main worktree 2024-12-19 5:54 ` Eric Sunshine 2024-12-19 6:12 ` Junio C Hamano @ 2024-12-27 14:34 ` Patrick Steinhardt 1 sibling, 0 replies; 8+ messages in thread From: Patrick Steinhardt @ 2024-12-27 14:34 UTC (permalink / raw) To: Eric Sunshine Cc: Junio C Hamano, shejialuo, AreaZR via GitGitGadget, git, AreaZR, Seija Kijin On Thu, Dec 19, 2024 at 12:54:55AM -0500, Eric Sunshine wrote: > On Wed, Dec 18, 2024 at 8:10 PM Junio C Hamano <gitster@pobox.com> wrote: > > Eric Sunshine <sunshine@sunshineco.com> writes: > > > On Wed, Dec 18, 2024 at 8:30 AM shejialuo <shejialuo@gmail.com> wrote: > > >> On Wed, Dec 18, 2024 at 02:20:45AM +0000, AreaZR via GitGitGadget wrote: > > >> > if (is_main_worktree(worktrees[i])) > > >> > continue; > > >> > ret = 1; > > >> > + break; > > >> > > >> So, when we find a linked worktree, we just return the value. From my > > >> perspective, if we decide to optimize like this way, we could drop the > > >> loop because the first element of the result of `get_worktrees` is the > > >> main worktree. And we could just check whether the "worktrees[1]" is > > >> NULL to do above. > > > > > > You're correct. get_worktrees() guarantees that the main worktree (or > > > bare repository) is the first item in the list, so merely checking > > > whether `worktrees[1]` is non-NULL would be sufficient to answer > > > whether linked worktrees are present; no looping is required. > > > > Would many other callers potentially want to know if the repository > > has more than one worktree? It looks to me that the has_worktrees() > > helper function in refs.c is a sign that the worktree API is missing > > a function. Calling get_worktrees() to prepare a list of worktrees > > and then counting the result, only to see if there are more than > > one, sounds a bit wasteful if we need to do so too often. > > If the need to answer this question does become common, then I can > imagine a function being added to the worktree API which tries to be > smart about it by only calling readdir() -- and validating a > .git/worktrees/<id>/ metainformation -- enough times to be able to > answer the question. > > However, although I haven't audited the code, I suspect the question > "are there any linked worktrees" is rare, possibly only asked by > `refs.c`. And in that case, it is asked only at the start of a > refs-migration operation. Moreover, it appears that even that case of > asking the question is probably temporary, existing only until someone > extends the migration logic to work correctly in the presence of > worktrees. (I'm sure Patrick can shed more light on this, though.) Overall I have to say that I found the worktree APIs to be quite lacking. It's surprising that you don't even have a way to load a single worktree, for example. In any case, for this specific case I'm not aware of any other users of the functionality, so there is no immediate need to lift it up into the worktree subsystem. Patrick ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] refs: exit early from the loop if it is not a main worktree 2024-12-18 23:52 ` Eric Sunshine 2024-12-19 1:10 ` Junio C Hamano @ 2024-12-27 14:34 ` Patrick Steinhardt 1 sibling, 0 replies; 8+ messages in thread From: Patrick Steinhardt @ 2024-12-27 14:34 UTC (permalink / raw) To: Eric Sunshine Cc: shejialuo, AreaZR via GitGitGadget, git, AreaZR, Seija Kijin On Wed, Dec 18, 2024 at 06:52:02PM -0500, Eric Sunshine wrote: > On Wed, Dec 18, 2024 at 8:30 AM shejialuo <shejialuo@gmail.com> wrote: > > On Wed, Dec 18, 2024 at 02:20:45AM +0000, AreaZR via GitGitGadget wrote: > > > if (is_main_worktree(worktrees[i])) > > > continue; > > > ret = 1; > > > + break; > > > > So, when we find a linked worktree, we just return the value. From my > > perspective, if we decide to optimize like this way, we could drop the > > loop because the first element of the result of `get_worktrees` is the > > main worktree. And we could just check whether the "worktrees[1]" is > > NULL to do above. > > You're correct. get_worktrees() guarantees that the main worktree (or > bare repository) is the first item in the list, so merely checking > whether `worktrees[1]` is non-NULL would be sufficient to answer > whether linked worktrees are present; no looping is required. > > > However, I don't know whether it's a good idea to exit the loop early > > in the first place. CC Patrick to help. > > If the loop is retained for some reason (though it really isn't > needed), then exiting early is indeed desirable. I suspect that the > missing `break` was just a silly oversight on Patrick's part. Yes, indeed, breaking out of the loop feels sensible to me. Checking whether `worktrees[1]` is non-NULL would be fine, as well. Patrick ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2024-12-27 14:34 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2024-12-18 2:20 [PATCH] refs: exit early from the loop if it is not a main worktree AreaZR via GitGitGadget 2024-12-18 13:31 ` shejialuo 2024-12-18 23:52 ` Eric Sunshine 2024-12-19 1:10 ` Junio C Hamano 2024-12-19 5:54 ` Eric Sunshine 2024-12-19 6:12 ` Junio C Hamano 2024-12-27 14:34 ` Patrick Steinhardt 2024-12-27 14:34 ` Patrick Steinhardt
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).