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

Commit d21211b

Browse files
committed
plumbing: idxfile, avoid unnecessary building of reverse offset/hash map
Signed-off-by: Filip Navara <[email protected]>
1 parent aa6f288 commit d21211b

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

Diff for: plumbing/format/idxfile/idxfile.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ type MemoryIndex struct {
5555
PackfileChecksum [20]byte
5656
IdxChecksum [20]byte
5757

58-
offsetHash map[int64]plumbing.Hash
58+
offsetHash map[int64]plumbing.Hash
59+
offsetHashIsFull bool
5960
}
6061

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

124-
return idx.getOffset(k, i)
125+
offset, err := idx.getOffset(k, i)
126+
127+
if !idx.offsetHashIsFull {
128+
// Save the offset for reverse lookup
129+
if idx.offsetHash == nil {
130+
idx.offsetHash = make(map[int64]plumbing.Hash)
131+
}
132+
idx.offsetHash[offset] = h
133+
}
134+
135+
return offset, err
125136
}
126137

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

168179
// FindHash implements the Index interface.
169180
func (idx *MemoryIndex) FindHash(o int64) (plumbing.Hash, error) {
181+
if !idx.offsetHashIsFull && idx.offsetHash != nil {
182+
if hash, ok := idx.offsetHash[o]; ok {
183+
return hash, nil
184+
}
185+
}
186+
170187
// Lazily generate the reverse offset/hash map if required.
171188
if idx.offsetHash == nil {
172189
if err := idx.genOffsetHash(); err != nil {
@@ -190,6 +207,7 @@ func (idx *MemoryIndex) genOffsetHash() error {
190207
}
191208

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

194212
iter, err := idx.Entries()
195213
if err != nil {

0 commit comments

Comments
 (0)