diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-12-20 19:11:16 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-20 19:11:16 +0100 |
commit | 50725139e8b96f88d74e253c8312fd4864640771 (patch) | |
tree | 70d0c7ce9b0d9201a3e94b19948d14b815337c76 /plumbing/format/packfile/decoder.go | |
parent | ca59809a0c74f6af498347a102a64242e26b0ab0 (diff) | |
parent | e39559fba6ea936c5f4659c03c22f9a5a256a4d7 (diff) | |
download | go-git-50725139e8b96f88d74e253c8312fd4864640771.tar.gz |
Merge pull request #698 from jfontan/improvement/use-decoder-cache
plumbing: cache, enforce the use of cache in packfile decoder
Diffstat (limited to 'plumbing/format/packfile/decoder.go')
-rw-r--r-- | plumbing/format/packfile/decoder.go | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/plumbing/format/packfile/decoder.go b/plumbing/format/packfile/decoder.go index ad72ea0..cb78701 100644 --- a/plumbing/format/packfile/decoder.go +++ b/plumbing/format/packfile/decoder.go @@ -52,7 +52,7 @@ var ( // is destroyed. The Offsets and CRCs are calculated whether an // ObjectStorer was provided or not. type Decoder struct { - DeltaBaseCache cache.Object + deltaBaseCache cache.Object s *Scanner o storer.EncodedObjectStorer @@ -80,15 +80,27 @@ type Decoder struct { // If the ObjectStorer implements storer.Transactioner, a transaction is created // during the Decode execution. If anything fails, Rollback is called func NewDecoder(s *Scanner, o storer.EncodedObjectStorer) (*Decoder, error) { - return NewDecoderForType(s, o, plumbing.AnyObject) + return NewDecoderForType(s, o, plumbing.AnyObject, + cache.NewObjectLRUDefault()) +} + +// NewDecoderWithCache is a version of NewDecoder where cache can be specified. +func NewDecoderWithCache(s *Scanner, o storer.EncodedObjectStorer, + cacheObject cache.Object) (*Decoder, error) { + + return NewDecoderForType(s, o, plumbing.AnyObject, cacheObject) } // NewDecoderForType returns a new Decoder but in this case for a specific object type. // When an object is read using this Decoder instance and it is not of the same type of // the specified one, nil will be returned. This is intended to avoid the content -// deserialization of all the objects +// deserialization of all the objects. +// +// cacheObject is a cache.Object implementation that is used to speed up the +// process. If cache is not needed you can pass nil. To create an LRU cache +// object with the default size you can use the helper cache.ObjectLRUDefault(). func NewDecoderForType(s *Scanner, o storer.EncodedObjectStorer, - t plumbing.ObjectType) (*Decoder, error) { + t plumbing.ObjectType, cacheObject cache.Object) (*Decoder, error) { if t == plumbing.OFSDeltaObject || t == plumbing.REFDeltaObject || @@ -101,8 +113,9 @@ func NewDecoderForType(s *Scanner, o storer.EncodedObjectStorer, } return &Decoder{ - s: s, - o: o, + s: s, + o: o, + deltaBaseCache: cacheObject, idx: NewIndex(0), offsetToType: make(map[int64]plumbing.ObjectType), @@ -404,19 +417,19 @@ func (d *Decoder) fillOFSDeltaObjectContent(obj plumbing.EncodedObject, offset i } func (d *Decoder) cacheGet(h plumbing.Hash) (plumbing.EncodedObject, bool) { - if d.DeltaBaseCache == nil { + if d.deltaBaseCache == nil { return nil, false } - return d.DeltaBaseCache.Get(h) + return d.deltaBaseCache.Get(h) } func (d *Decoder) cachePut(obj plumbing.EncodedObject) { - if d.DeltaBaseCache == nil { + if d.deltaBaseCache == nil { return } - d.DeltaBaseCache.Put(obj) + d.deltaBaseCache.Put(obj) } func (d *Decoder) recallByOffset(o int64) (plumbing.EncodedObject, error) { |