git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael Haggerty <mhagger@alum.mit.edu>
To: Jeff King <peff@peff.net>
Cc: Junio C Hamano <gitster@pobox.com>,
	Martin Fick <mfick@codeaurora.org>,
	git@vger.kernel.org
Subject: Re: [PATCH] Avoid sorting if references are added to ref_cache in order
Date: Thu, 24 May 2012 23:00:29 +0200	[thread overview]
Message-ID: <4FBEA16D.4040204@alum.mit.edu> (raw)
In-Reply-To: <20120524174906.GC3161@sigill.intra.peff.net>

On 05/24/2012 07:49 PM, Jeff King wrote:
> Thanks for a nice analysis and the patch; this definitely fixes the
> regression I was seeing.

And thanks for your feedback and for helping me reproduce your problem.

> The fix feels a little bit like a hack to me. The real problem is that
> there is a mismatch in how the ref code wants to receive chunks of refs,
> and how the packed-refs code wants to feed them. The optimal way to feed
> refs would be breadth-first, giving an entire single level (say,
> "refs/remotes/*", but not "refs/remotes/*/*") at once, and then sorting
> the result. And as far as I can tell, that is what read_loose_refs does.
>
> But the packed-refs file is read sequentially, and we end up inserting
> the refs in a depth-first way, which triggers this problem. Your fix
> relies on the fact that our depth-first feed happens in sorted order,
> and we can avoid the extra sorts. But I think the problem would still
> exist if we did a depth-first feed of unsorted refs.
>
> The packed-refs file is always in sorted order. The only other source of
> feed-one-at-a-time refs seems to be clone.c:write_remote_refs. It gets
> its refs from the mapped_refs, which eventually come from the remote
> side of the connection (and git tends to list refs in sorted order).
>
> So I think in practice we are OK, but would go quadratic again if we
> ever fed an unsorted list of refs. So the right thing is probably to
> apply this patch (which makes sense _anyway_, as it is even cheaper than
> sorting afterwards when we can avoid it), and be aware of the issue for
> any future unsorted provider, and deal with it if and when it ever
> happens.

I agree with you that this patch is a bit hacky, but it is simple, 
harmless, and it happens to do the right thing in the cases that are 
important in today's git.  See below for other approaches that could be 
used in combination with this one.

>> +	/* optimize for the case that entries are added in order */
>> +	if (dir->nr == 1 ||
>> +	    (dir->nr == dir->sorted + 1&&
>> +	     strcmp(dir->entries[dir->nr - 2]->name,
>> +		    dir->entries[dir->nr - 1]->name)<  0))
>> +		dir->sorted = dir->nr;
>
> Technically we would still be sorted if strcmp(...) == 0. But I guess it
> probably doesn't matter, as we shouldn't ever be adding duplicates here.

Yes, duplicate refs should be an exceptional case and needn't be handled 
efficiently.  sort_ref_dir() explicitly accepts the possibility of 
duplicate references, de-dups them if they are consistent with each 
other, or dies if they are inconsistent.  We shouldn't add a way to 
bypass that logic.  We could implement the 
duplicate-detection-and-checking code again in add_entry_to_dir(), but 
my choice was to leave it to sort_ref_dir() to deal with duplicates.


More general approaches:

The optimization implemented above is unsatisfying in the sense that it 
only works because refs are inserted in order.  What could be done to 
handle the general case (i.e., handling references that are added to the 
cache out of order)?

If quick, repeated iteration is thought to be important, then the 
canonical answer would be to use something like a balanced tree or a 
skip list.  These data structures are somewhat involved and have extra 
space and time overhead for the case of serial processing.

If ordered iteration is expected to be rare but lookups, adds, and 
deletes are expected to be frequent, then one could use a hash map, and 
sort the entries when needed as part of the iteration.  But a hash map 
discards the ordering that is already present when reading packed-refs 
and requires an extra sort every time that packed-refs is written.

Something that would help read_packed_refs() would be to keep track of 
the directory that it is currently working in.  This would effectively 
remove the need to access a directory while working in one of its 
subdirectories, thereby avoiding any need to repeatedly sort 
intermediate directories.  It would also avoid having to traverse the 
directories starting at the root for each new entry, which itself would 
save time independent of the time for sorting.  I have some patches that 
implement this change but they are stale.  I want to do some 
benchmarking first though to see whether the extra complication brings 
measurable benefits.

Finally, I did some work on the idea of keeping the refs within a 
directory *mostly* sorted.  (Probably this technique is already known, 
but I have never run into it.)  One would keep the first part of the 
array sorted, and append new references to the tail of the array 
unsorted.  Searching would be via a binary search over the sorted part 
of the array, and a linear search over the unsorted tail.  The trick is 
that every so often the tail should be sorted and merged into the head. 
  How often?  It is a tradeoff between the work of sorting and merging 
versus the work saved by avoiding linear searches through the tail.  I 
worked out the theory, and I think the optimum was to re-sort the array 
when the size of the unsorted tail reached the squareroot of the total 
size or something like that.  This method could reduce the cost of 
(lookup, add, lookup, add, ...) sequences, albeit not to the extent of a 
more optimal data structure.

Michael

-- 
Michael Haggerty
mhagger@alum.mit.edu
http://softwareswirl.blogspot.com/

  reply	other threads:[~2012-05-24 21:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-24 12:16 [PATCH] Avoid sorting if references are added to ref_cache in order mhagger
2012-05-24 17:49 ` Jeff King
2012-05-24 21:00   ` Michael Haggerty [this message]
2012-05-24 21:10     ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4FBEA16D.4040204@alum.mit.edu \
    --to=mhagger@alum.mit.edu \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=mfick@codeaurora.org \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).