Skip to content

Commit 780cb82

Browse files
committed
update
1 parent d837566 commit 780cb82

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

modules/git/commit_reader.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,31 @@ import (
88
"bytes"
99
"io"
1010
"strings"
11+
"sync"
1112
)
1213

14+
var commitPool = sync.Pool{
15+
New: func() any {
16+
return &Commit{}
17+
},
18+
}
19+
1320
// CommitFromReader will generate a Commit from a provided reader
1421
// We need this to interpret commits from cat-file or cat-file --batch
1522
//
1623
// If used as part of a cat-file --batch stream you need to limit the reader to the correct size
1724
func CommitFromReader(gitRepo *Repository, objectID ObjectID, reader io.Reader) (*Commit, error) {
18-
commit := &Commit{
19-
ID: objectID,
20-
Author: &Signature{},
21-
Committer: &Signature{},
22-
}
25+
commit := commitPool.Get().(*Commit)
26+
defer func() {
27+
// Reset the commit for reuse
28+
commit = commitPool.Get().(*Commit)
29+
// Put it back in the pool
30+
commitPool.Put(commit)
31+
}()
32+
33+
commit.ID = objectID
34+
commit.Author = &Signature{}
35+
commit.Committer = &Signature{}
2336

2437
payloadSB := new(strings.Builder)
2538
signatureSB := new(strings.Builder)

modules/git/last_commit_cache.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,12 @@ package git
66
import (
77
"crypto/sha256"
88
"fmt"
9-
"sync"
109

1110
"code.gitea.io/gitea/modules/cache"
1211
"code.gitea.io/gitea/modules/log"
1312
"code.gitea.io/gitea/modules/setting"
1413
)
1514

16-
var lastCommitPool = sync.Pool{
17-
New: func() any {
18-
return &Commit{}
19-
},
20-
}
21-
2215
func getCacheKey(repoPath, commitID, entryPath string) string {
2316
hashBytes := sha256.Sum256([]byte(fmt.Sprintf("%s:%s:%s", repoPath, commitID, entryPath)))
2417
return fmt.Sprintf("last_commit:%x", hashBytes)
@@ -65,25 +58,20 @@ func (c *LastCommitCache) Get(ref, entryPath string) (*Commit, error) {
6558
return nil, nil
6659
}
6760

68-
commit := lastCommitPool.Get().(*Commit)
69-
defer lastCommitPool.Put(commit)
70-
var err error
71-
var ok bool
72-
7361
commitID, ok := c.cache.Get(getCacheKey(c.repoPath, ref, entryPath))
7462
if !ok || commitID == "" {
7563
return nil, nil
7664
}
7765

7866
log.Debug("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, commitID)
7967
if c.commitCache != nil {
80-
if commit, ok = c.commitCache[commitID]; ok {
68+
if commit, ok := c.commitCache[commitID]; ok {
8169
log.Debug("LastCommitCache hit level 2: [%s:%s:%s]", ref, entryPath, commitID)
8270
return commit, nil
8371
}
8472
}
8573

86-
commit, err = c.repo.GetCommit(commitID)
74+
commit, err := c.repo.GetCommit(commitID)
8775
if err != nil {
8876
return nil, err
8977
}

0 commit comments

Comments
 (0)