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

Commit 3abbe14

Browse files
committed
plumbing: format/idxfile, avoid creating temporary buffers to decode integers
Signed-off-by: Filip Navara <filip.navara@gmail.com>
1 parent 830ee5b commit 3abbe14

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

plumbing/format/idxfile/idxfile.go

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
"io"
66
"sort"
77

8+
encbin "encoding/binary"
9+
810
"gopkg.in/src-d/go-git.v4/plumbing"
9-
"gopkg.in/src-d/go-git.v4/utils/binary"
1011
)
1112

1213
const (
@@ -122,41 +123,32 @@ func (idx *MemoryIndex) FindOffset(h plumbing.Hash) (int64, error) {
122123
return 0, plumbing.ErrObjectNotFound
123124
}
124125

125-
offset, err := idx.getOffset(k, i)
126+
offset := idx.getOffset(k, i)
126127

127128
if !idx.offsetHashIsFull {
128129
// Save the offset for reverse lookup
129130
if idx.offsetHash == nil {
130131
idx.offsetHash = make(map[int64]plumbing.Hash)
131132
}
132-
idx.offsetHash[offset] = h
133+
idx.offsetHash[int64(offset)] = h
133134
}
134135

135-
return offset, err
136+
return int64(offset), nil
136137
}
137138

138139
const isO64Mask = uint64(1) << 31
139140

140-
func (idx *MemoryIndex) getOffset(firstLevel, secondLevel int) (int64, error) {
141+
func (idx *MemoryIndex) getOffset(firstLevel, secondLevel int) uint64 {
141142
offset := secondLevel << 2
142-
buf := bytes.NewBuffer(idx.Offset32[firstLevel][offset : offset+4])
143-
ofs, err := binary.ReadUint32(buf)
144-
if err != nil {
145-
return -1, err
146-
}
143+
ofs := encbin.BigEndian.Uint32(idx.Offset32[firstLevel][offset : offset+4])
147144

148145
if (uint64(ofs) & isO64Mask) != 0 {
149146
offset := 8 * (uint64(ofs) & ^isO64Mask)
150-
buf := bytes.NewBuffer(idx.Offset64[offset : offset+8])
151-
n, err := binary.ReadUint64(buf)
152-
if err != nil {
153-
return -1, err
154-
}
155-
156-
return int64(n), nil
147+
n := encbin.BigEndian.Uint64(idx.Offset64[offset : offset+8])
148+
return n
157149
}
158150

159-
return int64(ofs), nil
151+
return uint64(ofs)
160152
}
161153

162154
// FindCRC32 implements the Index interface.
@@ -167,13 +159,12 @@ func (idx *MemoryIndex) FindCRC32(h plumbing.Hash) (uint32, error) {
167159
return 0, plumbing.ErrObjectNotFound
168160
}
169161

170-
return idx.getCRC32(k, i)
162+
return idx.getCRC32(k, i), nil
171163
}
172164

173-
func (idx *MemoryIndex) getCRC32(firstLevel, secondLevel int) (uint32, error) {
165+
func (idx *MemoryIndex) getCRC32(firstLevel, secondLevel int) uint32 {
174166
offset := secondLevel << 2
175-
buf := bytes.NewBuffer(idx.CRC32[firstLevel][offset : offset+4])
176-
return binary.ReadUint32(buf)
167+
return encbin.BigEndian.Uint32(idx.CRC32[firstLevel][offset : offset+4])
177168
}
178169

179170
// FindHash implements the Index interface.
@@ -303,16 +294,8 @@ func (i *idxfileEntryIter) Next() (*Entry, error) {
303294

304295
pos := i.idx.FanoutMapping[entry.Hash[0]]
305296

306-
offset, err := i.idx.getOffset(pos, i.secondLevel)
307-
if err != nil {
308-
return nil, err
309-
}
310-
entry.Offset = uint64(offset)
311-
312-
entry.CRC32, err = i.idx.getCRC32(pos, i.secondLevel)
313-
if err != nil {
314-
return nil, err
315-
}
297+
entry.Offset = i.idx.getOffset(pos, i.secondLevel)
298+
entry.CRC32 = i.idx.getCRC32(pos, i.secondLevel)
316299

317300
i.secondLevel++
318301
i.total++

0 commit comments

Comments
 (0)