Skip to content

Don't reload action.Repo when it has been loaded #19632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions models/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,11 @@ type GetFeedsOptions struct {
Date string // the day we want activity for: YYYY-MM-DD
}

type ExtAction struct {
Action `xorm:"extends"`
Repo *repo_model.Repository `xorm:"extends"`
}

// GetFeeds returns actions according to the provided options
func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, error) {
if opts.RequestedUser == nil && opts.RequestedTeam == nil && opts.RequestedRepo == nil {
Expand All @@ -345,12 +350,17 @@ func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, error) {
opts.SetDefaultValues()
sess = db.SetSessionPagination(sess, &opts)

actions := make([]*Action, 0, opts.PageSize)

if err := sess.Desc("`action`.created_unix").Find(&actions); err != nil {
extActions := make([]*ExtAction, 0, opts.PageSize)
if err := sess.Table("action").Desc("`action`.created_unix").Find(&extActions); err != nil {
return nil, fmt.Errorf("Find: %v", err)
}

actions := make(ActionList, 0, len(extActions))
for i := 0; i < len(extActions); i++ {
extActions[i].Action.Repo = extActions[i].Repo
actions = append(actions, &extActions[i].Action)
}

if err := ActionList(actions).loadAttributes(e); err != nil {
return nil, fmt.Errorf("LoadAttributes: %v", err)
}
Expand Down
7 changes: 7 additions & 0 deletions models/action_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func (actions ActionList) loadUsers(e db.Engine) (map[int64]*user_model.User, er
func (actions ActionList) getRepoIDs() []int64 {
repoIDs := make(map[int64]struct{}, len(actions))
for _, action := range actions {
if action.Repo != nil {
continue
}
if _, ok := repoIDs[action.RepoID]; !ok {
repoIDs[action.RepoID] = struct{}{}
}
Expand All @@ -62,6 +65,10 @@ func (actions ActionList) loadRepositories(e db.Engine) error {
}

repoIDs := actions.getRepoIDs()
if len(repoIDs) == 0 {
return nil
}

repoMaps := make(map[int64]*repo_model.Repository, len(repoIDs))
err := e.In("id", repoIDs).Find(&repoMaps)
if err != nil {
Expand Down