diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2018-06-21 12:44:18 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-21 12:44:18 +0200 |
commit | b11eaabdfd82deb68537fd44176d58084a6f367d (patch) | |
tree | d1578365030c0179b221f43517929b551b0c5840 | |
parent | 0710c6cb710a0cdab04ab7f61cc62e23cfcacbee (diff) | |
parent | 2d9816a5e7daea58a1419fef70bfc8d220ffd6a2 (diff) | |
download | go-git-b11eaabdfd82deb68537fd44176d58084a6f367d.tar.gz |
Merge pull request #869 from dsymonds/compact
packfile: optimise NewIndexFromIdxFile for a very common case
-rw-r--r-- | plumbing/format/packfile/index.go | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/plumbing/format/packfile/index.go b/plumbing/format/packfile/index.go index 7d8f2ad..021b2d1 100644 --- a/plumbing/format/packfile/index.go +++ b/plumbing/format/packfile/index.go @@ -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 } |