diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2024-12-10 13:15:06 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-11 05:15:06 +0800 |
commit | fbe6d9dc6b9f04e8be219ad4b442df9e08c16b7b (patch) | |
tree | 73d380093f37c44bf90c55cd67ed092ec4d37a59 /routers | |
parent | 2ac6f2b129fd6d955ac0fdb4dcf46efd5163f3b3 (diff) | |
download | gitea-fbe6d9dc6b9f04e8be219ad4b442df9e08c16b7b.tar.gz gitea-fbe6d9dc6b9f04e8be219ad4b442df9e08c16b7b.zip |
Use batch database operations instead of one by one to optimze api pulls (#32680)
Resolve #31492
The response time for the Pull Requests API has improved significantly,
dropping from over `2000ms` to about `350ms` on my local machine. It's
about `6` times faster.
A key area for further optimization lies in batch-fetching data for
`apiPullRequest.ChangedFiles, apiPullRequest.Additions, and
apiPullRequest.Deletions`.
Tests `TestAPIViewPulls` does exist and new tests added.
- This PR also fixes some bugs in `GetDiff` functions.
- This PR also fixes data inconsistent in test data. For a pull request,
the head branch's reference should be equal to the reference in
`pull/xxx/head`.
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/repo/pull.go | 35 |
1 files changed, 2 insertions, 33 deletions
diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 1116a4e9b1..86b204f51e 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -139,42 +139,11 @@ func ListPullRequests(ctx *context.APIContext) { return } - apiPrs := make([]*api.PullRequest, len(prs)) - // NOTE: load repository first, so that issue.Repo will be filled with pr.BaseRepo - if err := prs.LoadRepositories(ctx); err != nil { - ctx.Error(http.StatusInternalServerError, "LoadRepositories", err) - return - } - issueList, err := prs.LoadIssues(ctx) + apiPrs, err := convert.ToAPIPullRequests(ctx, ctx.Repo.Repository, prs, ctx.Doer) if err != nil { - ctx.Error(http.StatusInternalServerError, "LoadIssues", err) - return - } - - if err := issueList.LoadLabels(ctx); err != nil { - ctx.Error(http.StatusInternalServerError, "LoadLabels", err) - return - } - if err := issueList.LoadPosters(ctx); err != nil { - ctx.Error(http.StatusInternalServerError, "LoadPoster", err) + ctx.Error(http.StatusInternalServerError, "ToAPIPullRequests", err) return } - if err := issueList.LoadAttachments(ctx); err != nil { - ctx.Error(http.StatusInternalServerError, "LoadAttachments", err) - return - } - if err := issueList.LoadMilestones(ctx); err != nil { - ctx.Error(http.StatusInternalServerError, "LoadMilestones", err) - return - } - if err := issueList.LoadAssignees(ctx); err != nil { - ctx.Error(http.StatusInternalServerError, "LoadAssignees", err) - return - } - - for i := range prs { - apiPrs[i] = convert.ToAPIPullRequest(ctx, prs[i], ctx.Doer) - } ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) ctx.SetTotalCountHeader(maxResults) |