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

Commit

Permalink
plumbing: idxfile, avoid unnecessary building of reverse offset/hash map
Browse files Browse the repository at this point in the history
Signed-off-by: Filip Navara <navara@emclient.com>
  • Loading branch information
filipnavara committed Apr 20, 2019
1 parent aa6f288 commit d21211b
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions plumbing/format/idxfile/idxfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ type MemoryIndex struct {
PackfileChecksum [20]byte
IdxChecksum [20]byte

offsetHash map[int64]plumbing.Hash
offsetHash map[int64]plumbing.Hash
offsetHashIsFull bool
}

var _ Index = (*MemoryIndex)(nil)
Expand Down Expand Up @@ -121,7 +122,17 @@ func (idx *MemoryIndex) FindOffset(h plumbing.Hash) (int64, error) {
return 0, plumbing.ErrObjectNotFound
}

return idx.getOffset(k, i)
offset, err := idx.getOffset(k, i)

if !idx.offsetHashIsFull {
// Save the offset for reverse lookup
if idx.offsetHash == nil {
idx.offsetHash = make(map[int64]plumbing.Hash)
}
idx.offsetHash[offset] = h
}

return offset, err
}

const isO64Mask = uint64(1) << 31
Expand Down Expand Up @@ -167,6 +178,12 @@ func (idx *MemoryIndex) getCRC32(firstLevel, secondLevel int) (uint32, error) {

// FindHash implements the Index interface.
func (idx *MemoryIndex) FindHash(o int64) (plumbing.Hash, error) {
if !idx.offsetHashIsFull && idx.offsetHash != nil {
if hash, ok := idx.offsetHash[o]; ok {
return hash, nil
}
}

// Lazily generate the reverse offset/hash map if required.
if idx.offsetHash == nil {
if err := idx.genOffsetHash(); err != nil {
Expand All @@ -190,6 +207,7 @@ func (idx *MemoryIndex) genOffsetHash() error {
}

idx.offsetHash = make(map[int64]plumbing.Hash, count)
idx.offsetHashIsFull = true

iter, err := idx.Entries()
if err != nil {
Expand Down

0 comments on commit d21211b

Please sign in to comment.