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

Re: [PATCH v1] diffcore-rename: speed up register_rename_src

From
JHJeff Hostetler <git@jeffhostetler.com>
Date
Apr 20, 2017, 14:00 UTC
Message-ID
<40228c69-7946-3ef1-35de-4cea9b0312e4@jeffhostetler.com>
In-Reply-To
<20170419031839.m2zgwywa2soejiqk@sigill.intra.peff.net>
On 4/18/2017 11:18 PM, Jeff King wrote:
Show 53 quoted lines
> On Tue, Apr 18, 2017 at 10:56:08PM -0400, Jeff King wrote:
>
>>> When adding many things, we often just append and then sort at the
>>> end after we finished adding.  I wonder if recent "check the last
>>> one and append" optimization beats that strategy.
>>
>> The big question is whether we need to detect duplicates while we're
>> appending to the list, which is hard on an unsorted list.  In this
>> function, at least, we do detect when the path already exists and return
>> the existing entry. I'm not sure under what circumstances we would see
>> such a duplicate, though, as each filename should appear only once in
>> the tree diff. I would think.
>>
>> Doing:
>>
>> diff --git a/diffcore-rename.c b/diffcore-rename.c
>> index f7444c86b..56a493d97 100644
>> --- a/diffcore-rename.c
>> +++ b/diffcore-rename.c
>> @@ -86,7 +86,7 @@ static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
>>  		struct diff_rename_src *src = &(rename_src[next]);
>>  		int cmp = strcmp(one->path, src->p->one->path);
>>  		if (!cmp)
>> -			return src;
>> +			die("BUG: duplicate rename src: %s", one->path);
>>  		if (cmp < 0) {
>>  			last = next;
>>  			continue;
>>
>> passes the test suite, at least. :)
>
> Maybe relevant: 4d6be03b9 (diffcore-rename: avoid processing duplicate
> destinations, 2015-02-26). That's on the dst side, but possibly we
> should do something similar on the src side.
>
> BTW, I think the return value from register_rename_src() is
> questionable. It points to a "struct diff_rename_src" that may be
> reallocated by further calls to the function. Fortunately nobody
> actually looks at it, let alone saves it, so there's no bug.
>
> We may want to convert that return value to a void (if not just return
> an int for "hey, there's a duplicate", like we do for add_rename_dst()).
>
> Also, presumably that function could learn the same "check the last one"
> trick that the src side does. Which leads me back to "surely we can
> generalize this". I don't think bsearch() is quite what we want, because
> its interface doesn't tell us where to put the item when it isn't found.
> But I think we could make a general bsearch-like function that has
> similar semantics to index_name_pos(), with its negative-integer return.
>
> And then that would be a general lookup function, and we could easily
> build a general "look up and add" function around that. And the "check
> the last one" optimization would go in the latter.

I do see your point. And I do get tired of littering the code with "check the last before binary searching" tricks. It would be nice if we could better isolate this. I've tried to follow the path of least change/damage -- don't change functionality, but just short-cut when possible, so the patches I've pushed up this month have mostly taken that form.

And you're right, the gains on this particular patch are relatively minor and it is a bit of a contrived case (lots of renames??), but it did come up while testing on the Windows repo. It doesn't happen often, but it did happen.

So I'm OK with rejecting this one.

Perhaps the thing to learn from this (and the other ones) is that we have lots of places where we are building a sorted list by iterating over a sorted list. The insert routines are general purpose and cannot assume this, so they search first. Perhaps it would be clearer to have independent _append and _insert functions and have the caller explicitly call the appropriate one. The mainline iterations on the existing index could just call the _append form and never have to worry about searching or the negative-integer return trick. Whereas, the random iterations (such as on the command's arg list), would always call the _insert form.

Jeff
Previous: Jeff KingNext: Jeff King
Message 7 of 13 in “diffcore-rename speedup”
  1. diffcore-rename speedupgit@jeffhostetler.com, Apr 18, 2017
  2. diffcore-rename: speed up register_rename_srcgit@jeffhostetler.com, Apr 18, 2017
  3. Jeff KingApr 19, 2017
  4. Junio C HamanoApr 19, 2017
  5. Jeff KingApr 19, 2017
  6. Jeff KingApr 19, 2017
  7. Jeff HostetlerApr 20, 2017
  8. Jeff KingApr 20, 2017
  9. Jeff HostetlerApr 20, 2017
  10. Jeff KingApr 20, 2017
  11. Junio C HamanoApr 21, 2017
  12. Johannes SchindelinApr 20, 2017
  13. Jeff KingApr 20, 2017

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.