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

Commit

Permalink
plumbing: TreeWalker performance improvement, bufio pool for objects
Browse files Browse the repository at this point in the history
Signed-off-by: Arran Walker <[email protected]>
  • Loading branch information
saracen committed Apr 21, 2019
1 parent e5268e9 commit ae112cc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
4 changes: 3 additions & 1 deletion plumbing/object/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ func (c *Commit) Decode(o plumbing.EncodedObject) (err error) {
}
defer ioutil.CheckClose(reader, &err)

r := bufio.NewReader(reader)
r := bufPool.Get().(*bufio.Reader)
defer bufPool.Put(r)
r.Reset(reader)

var message bool
var pgpsig bool
Expand Down
12 changes: 12 additions & 0 deletions plumbing/object/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package object

import (
"bufio"
"sync"
)

var bufPool = sync.Pool{
New: func() interface{} {
return bufio.NewReader(nil)
},
}
4 changes: 3 additions & 1 deletion plumbing/object/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ func (t *Tag) Decode(o plumbing.EncodedObject) (err error) {
}
defer ioutil.CheckClose(reader, &err)

r := bufio.NewReader(reader)
r := bufPool.Get().(*bufio.Reader)
defer bufPool.Put(r)
r.Reset(reader)
for {
var line []byte
line, err = r.ReadBytes('\n')
Expand Down
24 changes: 17 additions & 7 deletions plumbing/object/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ func (t *Tree) Decode(o plumbing.EncodedObject) (err error) {
}
defer ioutil.CheckClose(reader, &err)

r := bufio.NewReader(reader)
r := bufPool.Get().(*bufio.Reader)
defer bufPool.Put(r)
r.Reset(reader)
for {
str, err := r.ReadString(' ')
if err != nil {
Expand Down Expand Up @@ -383,7 +385,7 @@ func NewTreeWalker(t *Tree, recursive bool, seen map[plumbing.Hash]bool) *TreeWa
// underlying repository will be skipped automatically. It is possible that this
// may change in future versions.
func (w *TreeWalker) Next() (name string, entry TreeEntry, err error) {
var obj Object
var obj *Tree
for {
current := len(w.stack) - 1
if current < 0 {
Expand All @@ -403,7 +405,7 @@ func (w *TreeWalker) Next() (name string, entry TreeEntry, err error) {
// Finished with the current tree, move back up to the parent
w.stack = w.stack[:current]
w.base, _ = path.Split(w.base)
w.base = path.Clean(w.base) // Remove trailing slash
w.base = strings.TrimSuffix(w.base, "/")
continue
}

Expand All @@ -419,7 +421,11 @@ func (w *TreeWalker) Next() (name string, entry TreeEntry, err error) {
obj, err = GetTree(w.s, entry.Hash)
}

name = path.Join(w.base, entry.Name)
if len(w.base) > 0 {
name = w.base + "/" + entry.Name
} else {
name = entry.Name
}

if err != nil {
err = io.EOF
Expand All @@ -433,9 +439,13 @@ func (w *TreeWalker) Next() (name string, entry TreeEntry, err error) {
return
}

if t, ok := obj.(*Tree); ok {
w.stack = append(w.stack, &treeEntryIter{t, 0})
w.base = path.Join(w.base, entry.Name)
if obj != nil {
w.stack = append(w.stack, &treeEntryIter{obj, 0})
if len(w.base) > 0 {
w.base = w.base + "/" + entry.Name
} else {
w.base = entry.Name
}
}

return
Expand Down

0 comments on commit ae112cc

Please sign in to comment.