Skip to content

Commit d34f33b

Browse files
committed
Improve performance when there are multiple commits in the last commit cache
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 66f0fd0 commit d34f33b

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

modules/git/commit_info_nogogit.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,13 @@ func (tes Entries) GetCommitsInfo(commit *Commit, treePath string, cache *LastCo
102102
}
103103

104104
func getLastCommitForPathsByCache(commitID, treePath string, paths []string, cache *LastCommitCache) (map[string]*Commit, []string, error) {
105+
wr, rd, cancel := CatFileBatch(cache.repo.Path)
106+
defer cancel()
107+
105108
var unHitEntryPaths []string
106109
var results = make(map[string]*Commit)
107110
for _, p := range paths {
108-
lastCommit, err := cache.Get(commitID, path.Join(treePath, p))
111+
lastCommit, err := cache.Get(commitID, path.Join(treePath, p), wr, rd)
109112
if err != nil {
110113
return nil, nil, err
111114
}

modules/git/last_commit_cache_nogogit.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
package git
88

99
import (
10+
"bufio"
11+
"io"
1012
"path"
1113
)
1214

@@ -34,7 +36,7 @@ func NewLastCommitCache(repoPath string, gitRepo *Repository, ttl func() int64,
3436
}
3537

3638
// Get get the last commit information by commit id and entry path
37-
func (c *LastCommitCache) Get(ref, entryPath string) (interface{}, error) {
39+
func (c *LastCommitCache) Get(ref, entryPath string, wr *io.PipeWriter, rd *bufio.Reader) (interface{}, error) {
3840
v := c.cache.Get(c.getCacheKey(c.repoPath, ref, entryPath))
3941
if vs, ok := v.(string); ok {
4042
log("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, vs)
@@ -46,7 +48,10 @@ func (c *LastCommitCache) Get(ref, entryPath string) (interface{}, error) {
4648
if err != nil {
4749
return nil, err
4850
}
49-
commit, err := c.repo.getCommit(id)
51+
if _, err := wr.Write([]byte(vs + "\n")); err != nil {
52+
return nil, err
53+
}
54+
commit, err := c.repo.getCommitFromBatchReader(rd, id)
5055
if err != nil {
5156
return nil, err
5257
}

modules/git/repo_commit_nogogit.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package git
99
import (
1010
"bufio"
1111
"errors"
12-
"fmt"
1312
"io"
1413
"io/ioutil"
1514
"strings"
@@ -69,6 +68,11 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
6968
}()
7069

7170
bufReader := bufio.NewReader(stdoutReader)
71+
72+
return repo.getCommitFromReader(bufReader, id)
73+
}
74+
75+
func (repo *Repository) getCommitFromReader(bufReader *bufio.Reader, id SHA1) (*Commit, error) {
7276
_, typ, size, err := ReadBatchLine(bufReader)
7377
if err != nil {
7478
if errors.Is(err, io.EOF) {
@@ -106,7 +110,6 @@ func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
106110
case "commit":
107111
return CommitFromReader(repo, id, io.LimitReader(bufReader, size))
108112
default:
109-
_ = stdoutReader.CloseWithError(fmt.Errorf("unknown typ: %s", typ))
110113
log("Unknown typ: %s", typ)
111114
return nil, ErrNotExist{
112115
ID: id.String(),

0 commit comments

Comments
 (0)