aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/packfile
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2018-08-17 08:46:19 +0200
committerGitHub <noreply@github.com>2018-08-17 08:46:19 +0200
commitba0f659cbf9982846de731cca426ce2498601130 (patch)
treed4816a29f6460de9d481861cb36c4248212c6270 /plumbing/format/packfile
parenta28c2ce44695f13ddf28748958f236afd8e0b544 (diff)
parenteb2aa9b2c3bf7af93fd261228be1b96e61c52bcf (diff)
downloadgo-git-ba0f659cbf9982846de731cca426ce2498601130.tar.gz
Merge pull request #916 from jfontan/improvement/memory-consumption-new-packfile-parser
Improvement/memory consumption new packfile parser
Diffstat (limited to 'plumbing/format/packfile')
-rw-r--r--plumbing/format/packfile/parser.go90
1 files changed, 37 insertions, 53 deletions
diff --git a/plumbing/format/packfile/parser.go b/plumbing/format/packfile/parser.go
index 581c334..28582b5 100644
--- a/plumbing/format/packfile/parser.go
+++ b/plumbing/format/packfile/parser.go
@@ -48,7 +48,7 @@ type Parser struct {
pendingRefDeltas map[plumbing.Hash][]*objectInfo
checksum plumbing.Hash
- cache *cache.ObjectLRU
+ cache *cache.BufferLRU
// delta content by offset, only used if source is not seekable
deltas map[int64][]byte
@@ -82,7 +82,7 @@ func NewParserWithStorage(
scanner: scanner,
ob: ob,
count: 0,
- cache: cache.NewObjectLRUDefault(),
+ cache: cache.NewBufferLRUDefault(),
pendingRefDeltas: make(map[plumbing.Hash][]*objectInfo),
deltas: deltas,
}, nil
@@ -221,21 +221,22 @@ func (p *Parser) indexObjects() error {
ota = newBaseObject(oh.Offset, oh.Length, t)
}
- size, crc, err := p.scanner.NextObject(buf)
+ _, crc, err := p.scanner.NextObject(buf)
if err != nil {
return err
}
ota.Crc32 = crc
- ota.PackSize = size
ota.Length = oh.Length
data := buf.Bytes()
if !delta {
- if _, err := ota.Write(data); err != nil {
+ sha1, err := getSHA1(ota.Type, data)
+ if err != nil {
return err
}
- ota.SHA1 = ota.Sum()
+
+ ota.SHA1 = sha1
p.oiByHash[ota.SHA1] = ota
}
@@ -291,41 +292,35 @@ func (p *Parser) resolveDeltas() error {
delete(p.deltas, obj.Offset)
}
}
-
- obj.Content = nil
}
return nil
}
func (p *Parser) get(o *objectInfo) ([]byte, error) {
- if len(o.Content) > 0 {
- return o.Content, nil
- }
-
- e, ok := p.cache.Get(o.SHA1)
+ b, ok := p.cache.Get(o.Offset)
// If it's not on the cache and is not a delta we can try to find it in the
// storage, if there's one.
if !ok && p.storage != nil && !o.Type.IsDelta() {
var err error
- e, err = p.storage.EncodedObject(plumbing.AnyObject, o.SHA1)
+ e, err := p.storage.EncodedObject(plumbing.AnyObject, o.SHA1)
if err != nil {
return nil, err
}
- }
- if e != nil {
r, err := e.Reader()
if err != nil {
return nil, err
}
- buf := make([]byte, e.Size())
- if _, err = r.Read(buf); err != nil {
+ b = make([]byte, e.Size())
+ if _, err = r.Read(b); err != nil {
return nil, err
}
+ }
- return buf, nil
+ if b != nil {
+ return b, nil
}
var data []byte
@@ -348,11 +343,7 @@ func (p *Parser) get(o *objectInfo) ([]byte, error) {
}
if len(o.Children) > 0 {
- m := &plumbing.MemoryObject{}
- m.Write(data)
- m.SetType(o.Type)
- m.SetSize(o.Size())
- p.cache.Put(m)
+ p.cache.Put(o.Offset, data)
}
return data, nil
@@ -410,8 +401,6 @@ func (p *Parser) readData(o *objectInfo) ([]byte, error) {
return data, nil
}
- // TODO: skip header. Header size can be calculated with the offset of the
- // next offset in the first pass.
if _, err := p.scanner.SeekFromStart(o.Offset); err != nil {
return nil, err
}
@@ -434,34 +423,40 @@ func applyPatchBase(ota *objectInfo, data, base []byte) ([]byte, error) {
return nil, err
}
- ota.Type = ota.Parent.Type
- ota.Hasher = plumbing.NewHasher(ota.Type, int64(len(patched)))
- if _, err := ota.Write(patched); err != nil {
- return nil, err
+ if ota.SHA1 == plumbing.ZeroHash {
+ ota.Type = ota.Parent.Type
+ sha1, err := getSHA1(ota.Type, patched)
+ if err != nil {
+ return nil, err
+ }
+
+ ota.SHA1 = sha1
+ ota.Length = int64(len(patched))
}
- ota.SHA1 = ota.Sum()
- ota.Length = int64(len(patched))
return patched, nil
}
-type objectInfo struct {
- plumbing.Hasher
+func getSHA1(t plumbing.ObjectType, data []byte) (plumbing.Hash, error) {
+ hasher := plumbing.NewHasher(t, int64(len(data)))
+ if _, err := hasher.Write(data); err != nil {
+ return plumbing.ZeroHash, err
+ }
+
+ return hasher.Sum(), nil
+}
- Offset int64
- Length int64
- HeaderLength int64
- PackSize int64
- Type plumbing.ObjectType
- DiskType plumbing.ObjectType
+type objectInfo struct {
+ Offset int64
+ Length int64
+ Type plumbing.ObjectType
+ DiskType plumbing.ObjectType
Crc32 uint32
Parent *objectInfo
Children []*objectInfo
SHA1 plumbing.Hash
-
- Content []byte
}
func newBaseObject(offset, length int64, t plumbing.ObjectType) *objectInfo {
@@ -473,29 +468,18 @@ func newDeltaObject(
t plumbing.ObjectType,
parent *objectInfo,
) *objectInfo {
- children := make([]*objectInfo, 0)
-
obj := &objectInfo{
- Hasher: plumbing.NewHasher(t, length),
Offset: offset,
Length: length,
- PackSize: 0,
Type: t,
DiskType: t,
Crc32: 0,
Parent: parent,
- Children: children,
}
return obj
}
-func (o *objectInfo) Write(b []byte) (int, error) {
- o.Content = make([]byte, len(b))
- copy(o.Content, b)
- return o.Hasher.Write(b)
-}
-
func (o *objectInfo) IsDelta() bool {
return o.Type.IsDelta()
}