git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2] checkout: eliminate unnecessary merge for trivial checkout

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 9, 2016, 21:55 UTC
Message-ID
<xmqq1t0sagcm.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20160909192520.4812-1-benpeart@microsoft.com>
Ben Peart <peartben@gmail.com> writes:
Show 15 quoted lines
> @@ -802,6 +806,87 @@ static void orphaned_commit_warning(struct commit *old, struct commit *new)
>  	free(refs.objects);
>  }
>  
> +static int needs_working_tree_merge(const struct checkout_opts *opts,
> +	const struct branch_info *old,
> +	const struct branch_info *new)
> +{
> +	/*
> +	 * We must do the merge if we are actually moving to a new
> +	 * commit tree.
> +	 */
> +	if (!old->commit || !new->commit ||
> +		oidcmp(&old->commit->tree->object.oid, &new->commit->tree->object.oid))
> +		return 1;

A huge helper function helps it somewhat, compared with the earlier unreadable mess ;-).

Are we certain that at this point the commit objects are both parsed and their tree->object.oid are both valid?

Show 6 quoted lines
> +	/*
> +	 * Honor the explicit request for a three-way merge or to throw away
> +	 * local changes
> +	 */
> +	if (opts->merge || opts->force)
> +		return 1;

Hmph, "git checkout -m HEAD" wouldn't have to do anything wrt the index status, no?

For that matter, neither "git checkout -f HEAD". Unless we rely on unpack_trees() to write over the working tree files.

    ... me goes and looks, and finds that merge_working_tree()
    indeed does have a logic to do quite different thing when
    "--force" is given.

This makes me wonder if the "merge_working_tree() is expensive, so selectively skip calling it" approach is working at a wrong level. Wouldn't the merge_working_tree() function itself a better place to do this kind of "we may not have to do the full two-way merge" optimization? It already looks at opts and does things differently (e.g. when running with "--force", it does not even call unpack). If you can optimize even more by looking at other fields in opts to avoid unpack, that would fit better with the structure of the code that we already have.

Show 6 quoted lines
> +	/*
> +	 * Checking out the requested commit may require updating the working
> +	 * directory and index, let the merge handle it.
> +	 */
> +	if (opts->force_detach)
> +		return 1;

This does not make much sense to me. After "git branch -f foo HEAD", there is no difference in what is done to the index and the working directory between "git checkout --detach HEAD" and "git checkout foo", is there?

Show 11 quoted lines
> +	/*
> +	 * opts->writeout_stage cannot be used with switching branches so is
> +	 * not tested here
> +	 */
> +
> +	 /*
> +	  * Honor the explicit ignore requests
> +	  */
> +	if (!opts->overwrite_ignore || opts->ignore_skipworktree
> +		|| opts->ignore_other_worktrees)
> +		return 1;
Style.  I think you earlier had
	if (a || b ||
            c)
and here you are doing
	if (a || b
            || c)
Please pick one and stick to it (I'd pick the former).
Show 6 quoted lines
> +	 /*
> +	 * If we're not creating a new branch, by definition we're changing
> +	 * the existing one so need to do the merge
> +	 */
> +	if (!opts->new_branch)
> +		return 1;

Sorry, but I fail to follow that line of thought. Starting from a state where your HEAD points at commit A,

 - switching to a detached HEAD pointing at a commit A,
 - switching to an existing branch that already points at the same
   commit A, and
 - force updating an existing branch that was pointing at something
   else to point at the same commit A,

would have the same effect as creating a new branch at commit A and switching to it, no? The same comment applies to the remainder of this function.

More importantly, merge_working_tree() checks things other than what this function is checking. For example, it prevents you from branch-switching (whether it is to switch to an existing branch that has the same commit as the current HEAD, to switch to detached HEAD state at the same commit as the current HEAD, or to switch to a new branch that points at the same commit as the current HEAD) if your index is unmerged (i.e. you are in the middle of a mergy operation).

So my gut feeling is that this:
Show 6 quoted lines
> +	/*
> +	 * Optimize the performance of "git checkout foo" by skipping the call
> +	 * to merge_working_tree where possible.
> +	 */
> +	if (needs_working_tree_merge(opts, &old, new)) {
> +		ret = merge_working_tree(opts, &old, new, &writeout_error);

works at the wrong level. The comment up to 'Optimize the performance of "git checkout foo"' may correctly state what we want to achieve, but I think we should do so not with "by skipping the call to", but with "by optimizing merge_working_tree()".

Thanks.
Previous: Ben PeartNext: Ben Peart
Message 2 of 5 in “checkout: eliminate unnecessary merge for trivial checkout”
  1. checkout: eliminate unnecessary merge for trivial checkoutBen Peart, Sep 9, 2016
  2. Junio C HamanoSep 9, 2016
  3. Ben PeartSep 12, 2016
  4. Junio C HamanoSep 12, 2016
  5. Ben PeartSep 13, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.