Replace sync.Map with normal maps (#18584)

* Replace `sync.Map` with normal maps

- These maps aren't being used in any kind of concurrent read/write and
thus don't need `sync.Map` and can instead use normal maps.
- Special thanks to dachary.
- Added in: https://github.com/go-gitea/gitea/pull/6290

* Remove unannounced feature
This commit is contained in:
Gusted 2022-02-03 20:18:18 +01:00 committed by GitHub
parent e2bbbc4876
commit cafd19c1a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,7 +12,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"time" "time"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
@ -42,9 +41,9 @@ type GiteaLocalUploader struct {
repoOwner string repoOwner string
repoName string repoName string
repo *repo_model.Repository repo *repo_model.Repository
labels sync.Map labels map[string]*models.Label
milestones sync.Map milestones map[string]int64
issues sync.Map issues map[int64]*models.Issue
gitRepo *git.Repository gitRepo *git.Repository
prHeadCache map[string]struct{} prHeadCache map[string]struct{}
userMap map[int64]int64 // external user id mapping to user id userMap map[int64]int64 // external user id mapping to user id
@ -59,6 +58,9 @@ func NewGiteaLocalUploader(ctx context.Context, doer *user_model.User, repoOwner
doer: doer, doer: doer,
repoOwner: repoOwner, repoOwner: repoOwner,
repoName: repoName, repoName: repoName,
labels: make(map[string]*models.Label),
milestones: make(map[string]int64),
issues: make(map[int64]*models.Issue),
prHeadCache: make(map[string]struct{}), prHeadCache: make(map[string]struct{}),
userMap: make(map[int64]int64), userMap: make(map[int64]int64),
prCache: make(map[int64]*models.PullRequest), prCache: make(map[int64]*models.PullRequest),
@ -201,7 +203,7 @@ func (g *GiteaLocalUploader) CreateMilestones(milestones ...*base.Milestone) err
} }
for _, ms := range mss { for _, ms := range mss {
g.milestones.Store(ms.Name, ms.ID) g.milestones[ms.Name] = ms.ID
} }
return nil return nil
} }
@ -223,7 +225,7 @@ func (g *GiteaLocalUploader) CreateLabels(labels ...*base.Label) error {
return err return err
} }
for _, lb := range lbs { for _, lb := range lbs {
g.labels.Store(lb.Name, lb) g.labels[lb.Name] = lb
} }
return nil return nil
} }
@ -333,19 +335,13 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
for _, issue := range issues { for _, issue := range issues {
var labels []*models.Label var labels []*models.Label
for _, label := range issue.Labels { for _, label := range issue.Labels {
lb, ok := g.labels.Load(label.Name) lb, ok := g.labels[label.Name]
if ok { if ok {
labels = append(labels, lb.(*models.Label)) labels = append(labels, lb)
} }
} }
var milestoneID int64 milestoneID := g.milestones[issue.Milestone]
if issue.Milestone != "" {
milestone, ok := g.milestones.Load(issue.Milestone)
if ok {
milestoneID = milestone.(int64)
}
}
if issue.Created.IsZero() { if issue.Created.IsZero() {
if issue.Closed != nil { if issue.Closed != nil {
@ -404,7 +400,7 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
} }
for _, is := range iss { for _, is := range iss {
g.issues.Store(is.Index, is) g.issues[is.Index] = is
} }
} }
@ -416,16 +412,14 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
cms := make([]*models.Comment, 0, len(comments)) cms := make([]*models.Comment, 0, len(comments))
for _, comment := range comments { for _, comment := range comments {
var issue *models.Issue var issue *models.Issue
issueInter, ok := g.issues.Load(comment.IssueIndex) issue, ok := g.issues[comment.IssueIndex]
if !ok { if !ok {
var err error var err error
issue, err = models.GetIssueByIndex(g.repo.ID, comment.IssueIndex) issue, err = models.GetIssueByIndex(g.repo.ID, comment.IssueIndex)
if err != nil { if err != nil {
return err return err
} }
g.issues.Store(comment.IssueIndex, issue) g.issues[comment.IssueIndex] = issue
} else {
issue = issueInter.(*models.Issue)
} }
if comment.Created.IsZero() { if comment.Created.IsZero() {
@ -487,7 +481,7 @@ func (g *GiteaLocalUploader) CreatePullRequests(prs ...*base.PullRequest) error
return err return err
} }
for _, pr := range gprs { for _, pr := range gprs {
g.issues.Store(pr.Issue.Index, pr.Issue) g.issues[pr.Issue.Index] = pr.Issue
pull.AddToTaskQueue(pr) pull.AddToTaskQueue(pr)
} }
return nil return nil
@ -496,19 +490,13 @@ func (g *GiteaLocalUploader) CreatePullRequests(prs ...*base.PullRequest) error
func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullRequest, error) { func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullRequest, error) {
var labels []*models.Label var labels []*models.Label
for _, label := range pr.Labels { for _, label := range pr.Labels {
lb, ok := g.labels.Load(label.Name) lb, ok := g.labels[label.Name]
if ok { if ok {
labels = append(labels, lb.(*models.Label)) labels = append(labels, lb)
} }
} }
var milestoneID int64 milestoneID := g.milestones[pr.Milestone]
if pr.Milestone != "" {
milestone, ok := g.milestones.Load(pr.Milestone)
if ok {
milestoneID = milestone.(int64)
}
}
// download patch file // download patch file
err := func() error { err := func() error {
@ -700,18 +688,15 @@ func (g *GiteaLocalUploader) CreateReviews(reviews ...*base.Review) error {
cms := make([]*models.Review, 0, len(reviews)) cms := make([]*models.Review, 0, len(reviews))
for _, review := range reviews { for _, review := range reviews {
var issue *models.Issue var issue *models.Issue
issueInter, ok := g.issues.Load(review.IssueIndex) issue, ok := g.issues[review.IssueIndex]
if !ok { if !ok {
var err error var err error
issue, err = models.GetIssueByIndex(g.repo.ID, review.IssueIndex) issue, err = models.GetIssueByIndex(g.repo.ID, review.IssueIndex)
if err != nil { if err != nil {
return err return err
} }
g.issues.Store(review.IssueIndex, issue) g.issues[review.IssueIndex] = issue
} else {
issue = issueInter.(*models.Issue)
} }
if review.CreatedAt.IsZero() { if review.CreatedAt.IsZero() {
review.CreatedAt = time.Unix(int64(issue.CreatedUnix), 0) review.CreatedAt = time.Unix(int64(issue.CreatedUnix), 0)
} }