From 962eeb399ce322daa4dd6522cdc91d277fbdba2c Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Wed, 20 Dec 2017 15:11:38 +0100 Subject: Enforce the use of cache in packfile decoder Decoder object can make use of an object cache to speed up processing. Previously the only way to specify it was changing manually the struct generated by NewDecodeForFile. This lead to some instances to be created without it and penalized performance. Now the cache should be explicitly passed to the constructor function. NewDecoder now creates objects with a cache using the default size. A new helper function was added to create cache objects with the default size as this becomes a common task now: cache.NewObjectLRUDefault() Signed-off-by: Javi Fontan --- plumbing/format/packfile/decoder.go | 16 +++++++++++----- plumbing/format/packfile/decoder_test.go | 23 ++++++++++++++++------- 2 files changed, 27 insertions(+), 12 deletions(-) (limited to 'plumbing/format') diff --git a/plumbing/format/packfile/decoder.go b/plumbing/format/packfile/decoder.go index ad72ea0..24493ec 100644 --- a/plumbing/format/packfile/decoder.go +++ b/plumbing/format/packfile/decoder.go @@ -80,15 +80,20 @@ 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()) } // 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 an ObjectLRU that is used to speed up the process. If cache +// is not needed you can pass nil. To create a cache object with the default +// size you an 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 +106,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), diff --git a/plumbing/format/packfile/decoder_test.go b/plumbing/format/packfile/decoder_test.go index 1a1a74a..b5bc7b7 100644 --- a/plumbing/format/packfile/decoder_test.go +++ b/plumbing/format/packfile/decoder_test.go @@ -4,6 +4,7 @@ import ( "io" "gopkg.in/src-d/go-git.v4/plumbing" + "gopkg.in/src-d/go-git.v4/plumbing/cache" "gopkg.in/src-d/go-git.v4/plumbing/format/idxfile" "gopkg.in/src-d/go-git.v4/plumbing/format/packfile" "gopkg.in/src-d/go-git.v4/plumbing/storer" @@ -51,7 +52,8 @@ func (s *ReaderSuite) TestDecodeByTypeRefDelta(c *C) { storage := memory.NewStorage() scanner := packfile.NewScanner(f.Packfile()) - d, err := packfile.NewDecoderForType(scanner, storage, plumbing.CommitObject) + d, err := packfile.NewDecoderForType(scanner, storage, plumbing.CommitObject, + cache.NewObjectLRUDefault()) c.Assert(err, IsNil) // Index required to decode by ref-delta. @@ -77,7 +79,8 @@ func (s *ReaderSuite) TestDecodeByTypeRefDeltaError(c *C) { fixtures.Basic().ByTag("ref-delta").Test(c, func(f *fixtures.Fixture) { storage := memory.NewStorage() scanner := packfile.NewScanner(f.Packfile()) - d, err := packfile.NewDecoderForType(scanner, storage, plumbing.CommitObject) + d, err := packfile.NewDecoderForType(scanner, storage, + plumbing.CommitObject, cache.NewObjectLRUDefault()) c.Assert(err, IsNil) defer d.Close() @@ -111,7 +114,8 @@ func (s *ReaderSuite) TestDecodeByType(c *C) { for _, t := range ts { storage := memory.NewStorage() scanner := packfile.NewScanner(f.Packfile()) - d, err := packfile.NewDecoderForType(scanner, storage, t) + d, err := packfile.NewDecoderForType(scanner, storage, t, + cache.NewObjectLRUDefault()) c.Assert(err, IsNil) // when the packfile is ref-delta based, the offsets are required @@ -141,13 +145,17 @@ func (s *ReaderSuite) TestDecodeByTypeConstructor(c *C) { storage := memory.NewStorage() scanner := packfile.NewScanner(f.Packfile()) - _, err := packfile.NewDecoderForType(scanner, storage, plumbing.OFSDeltaObject) + _, err := packfile.NewDecoderForType(scanner, storage, + plumbing.OFSDeltaObject, cache.NewObjectLRUDefault()) c.Assert(err, Equals, plumbing.ErrInvalidType) - _, err = packfile.NewDecoderForType(scanner, storage, plumbing.REFDeltaObject) + _, err = packfile.NewDecoderForType(scanner, storage, + plumbing.REFDeltaObject, cache.NewObjectLRUDefault()) + c.Assert(err, Equals, plumbing.ErrInvalidType) - _, err = packfile.NewDecoderForType(scanner, storage, plumbing.InvalidObject) + _, err = packfile.NewDecoderForType(scanner, storage, plumbing.InvalidObject, + cache.NewObjectLRUDefault()) c.Assert(err, Equals, plumbing.ErrInvalidType) } @@ -313,7 +321,8 @@ func (s *ReaderSuite) TestDecodeObjectAt(c *C) { func (s *ReaderSuite) TestDecodeObjectAtForType(c *C) { f := fixtures.Basic().One() scanner := packfile.NewScanner(f.Packfile()) - d, err := packfile.NewDecoderForType(scanner, nil, plumbing.TreeObject) + d, err := packfile.NewDecoderForType(scanner, nil, plumbing.TreeObject, + cache.NewObjectLRUDefault()) c.Assert(err, IsNil) // when the packfile is ref-delta based, the offsets are required -- cgit