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

Commit fec6217

Browse files
committed
performance increase for repos with many files and directories
1 parent 07e4c43 commit fec6217

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

objects_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ func (s *ObjectsSuite) TestParseTree(c *C) {
5353
c.Assert(err, IsNil)
5454

5555
c.Assert(tree.Entries, HasLen, 8)
56-
c.Assert(tree.Entries[0].Name, Equals, ".gitignore")
57-
c.Assert(tree.Entries[0].Mode.String(), Equals, "-rw-r--r--")
58-
c.Assert(tree.Entries[0].Hash.String(), Equals, "32858aad3c383ed1ff0a0f9bdf231d54a00c9e88")
56+
c.Assert(tree.Entries[".gitignore"].Name, Equals, ".gitignore")
57+
c.Assert(tree.Entries[".gitignore"].Mode.String(), Equals, "-rw-r--r--")
58+
c.Assert(tree.Entries[".gitignore"].Hash.String(), Equals, "32858aad3c383ed1ff0a0f9bdf231d54a00c9e88")
5959

6060
count := 0
6161
ch := tree.Files()

tree.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
// Tree is basically like a directory - it references a bunch of other trees
1616
// and/or blobs (i.e. files and sub-directories)
1717
type Tree struct {
18-
Entries []TreeEntry
18+
Entries map[string]TreeEntry
1919
Hash core.Hash
2020

2121
r *Repository
@@ -97,13 +97,12 @@ func (t *Tree) dir(baseName string) (*Tree, error) {
9797
var errEntryNotFound = errors.New("entry not found")
9898

9999
func (t *Tree) entry(baseName string) (*TreeEntry, error) {
100-
for _, entry := range t.Entries {
101-
if entry.Name == baseName {
102-
return &entry, nil
103-
}
100+
entry, ok := t.Entries[baseName]
101+
if !ok {
102+
return nil, errEntryNotFound
104103
}
105104

106-
return nil, errEntryNotFound
105+
return &entry, nil
107106
}
108107

109108
func (t *Tree) Files() chan *File {
@@ -145,6 +144,8 @@ func (t *Tree) Decode(o core.Object) error {
145144
return nil
146145
}
147146

147+
t.Entries = make(map[string]TreeEntry)
148+
148149
r := bufio.NewReader(o.Reader())
149150
for {
150151
mode, err := r.ReadString(' ')
@@ -172,11 +173,12 @@ func (t *Tree) Decode(o core.Object) error {
172173
return err
173174
}
174175

175-
t.Entries = append(t.Entries, TreeEntry{
176+
baseName := name[:len(name)-1]
177+
t.Entries[baseName] = TreeEntry{
176178
Hash: hash,
177179
Mode: os.FileMode(fm),
178-
Name: name[:len(name)-1],
179-
})
180+
Name: baseName,
181+
}
180182
}
181183

182184
return nil

0 commit comments

Comments
 (0)