Skip to content
This repository has been archived by the owner on Sep 11, 2020. It is now read-only.

Commit

Permalink
packfile: optimise NewIndexFromIdxFile for a very common case
Browse files Browse the repository at this point in the history
Loading from an on-disk idxfile will usually already have the idxfile
entries in order, so check that before wasting time on sorting.

Signed-off-by: David Symonds <dsymonds@golang.org>
  • Loading branch information
dsymonds committed Jun 21, 2018
1 parent 0710c6c commit 2d9816a
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions plumbing/format/packfile/index.go
Expand Up @@ -31,10 +31,20 @@ func NewIndexFromIdxFile(idxf *idxfile.Idxfile) *Index {
byHash: make(map[plumbing.Hash]*idxfile.Entry, idxf.ObjectCount),
byOffset: make([]*idxfile.Entry, 0, idxf.ObjectCount),
}
for _, e := range idxf.Entries {
sorted := true
for i, e := range idxf.Entries {
idx.addUnsorted(e)
if i > 0 && idx.byOffset[i-1].Offset >= e.Offset {
sorted = false
}
}

// If the idxfile was loaded from a regular packfile index
// then it will already be in offset order, in which case we
// can avoid doing a relatively expensive idempotent sort.
if !sorted {
sort.Sort(orderByOffset(idx.byOffset))
}
sort.Sort(orderByOffset(idx.byOffset))

return idx
}
Expand Down

0 comments on commit 2d9816a

Please sign in to comment.