loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Replace `util.SliceXxx` with `slices.Xxx` (#26958)

authored by

CaiCandong and committed by
GitHub
a78c2eae e97e883a

+46 -110
+2 -1
models/actions/run.go
··· 6 6 import ( 7 7 "context" 8 8 "fmt" 9 + "slices" 9 10 "strings" 10 11 "time" 11 12 ··· 350 351 // It's impossible that the run is not found, since Gitea never deletes runs. 351 352 } 352 353 353 - if run.Status != 0 || util.SliceContains(cols, "status") { 354 + if run.Status != 0 || slices.Contains(cols, "status") { 354 355 if run.RepoID == 0 { 355 356 run, err = GetRunByID(ctx, run.ID) 356 357 if err != nil {
+3 -2
models/actions/run_job.go
··· 6 6 import ( 7 7 "context" 8 8 "fmt" 9 + "slices" 9 10 "time" 10 11 11 12 "code.gitea.io/gitea/models/db" ··· 107 108 return 0, err 108 109 } 109 110 110 - if affected == 0 || (!util.SliceContains(cols, "status") && job.Status == 0) { 111 + if affected == 0 || (!slices.Contains(cols, "status") && job.Status == 0) { 111 112 return affected, nil 112 113 } 113 114 114 - if affected != 0 && util.SliceContains(cols, "status") && job.Status.IsWaiting() { 115 + if affected != 0 && slices.Contains(cols, "status") && job.Status.IsWaiting() { 115 116 // if the status of job changes to waiting again, increase tasks version. 116 117 if err := IncreaseTaskVersion(ctx, job.OwnerID, job.RepoID); err != nil { 117 118 return 0, err
+2 -1
models/git/protected_branch.go
··· 7 7 "context" 8 8 "errors" 9 9 "fmt" 10 + "slices" 10 11 "strings" 11 12 12 13 "code.gitea.io/gitea/models/db" ··· 435 436 436 437 whitelist = make([]int64, 0, len(teams)) 437 438 for i := range teams { 438 - if util.SliceContains(newWhitelist, teams[i].ID) { 439 + if slices.Contains(newWhitelist, teams[i].ID) { 439 440 whitelist = append(whitelist, teams[i].ID) 440 441 } 441 442 }
+3 -2
models/issues/issue.go
··· 8 8 "context" 9 9 "fmt" 10 10 "regexp" 11 + "slices" 11 12 12 13 "code.gitea.io/gitea/models/db" 13 14 project_model "code.gitea.io/gitea/models/project" ··· 605 606 log.Error(err.Error()) 606 607 return false 607 608 } 608 - return util.SliceContains(userIDs, user.ID) 609 + return slices.Contains(userIDs, user.ID) 609 610 } 610 611 611 612 // DependencyInfo represents high level information about an issue which is a dependency of another issue. ··· 630 631 Find(&userIDs); err != nil { 631 632 return nil, fmt.Errorf("get poster IDs: %w", err) 632 633 } 633 - if !util.SliceContains(userIDs, issue.PosterID) { 634 + if !slices.Contains(userIDs, issue.PosterID) { 634 635 return append(userIDs, issue.PosterID), nil 635 636 } 636 637 return userIDs, nil
+2 -1
models/repo/repo_unit.go
··· 6 6 import ( 7 7 "context" 8 8 "fmt" 9 + "slices" 9 10 "strings" 10 11 11 12 "code.gitea.io/gitea/models/db" ··· 176 177 } 177 178 178 179 func (cfg *ActionsConfig) IsWorkflowDisabled(file string) bool { 179 - return util.SliceContains(cfg.DisabledWorkflows, file) 180 + return slices.Contains(cfg.DisabledWorkflows, file) 180 181 } 181 182 182 183 func (cfg *ActionsConfig) DisableWorkflow(file string) {
+5 -5
modules/indexer/code/indexer.go
··· 7 7 "context" 8 8 "os" 9 9 "runtime/pprof" 10 + "slices" 10 11 "sync/atomic" 11 12 "time" 12 13 ··· 20 21 "code.gitea.io/gitea/modules/process" 21 22 "code.gitea.io/gitea/modules/queue" 22 23 "code.gitea.io/gitea/modules/setting" 23 - "code.gitea.io/gitea/modules/util" 24 24 ) 25 25 26 26 var ( ··· 54 54 } 55 55 56 56 // skip forks from being indexed if unit is not present 57 - if !util.SliceContains(repoTypes, "forks") && repo.IsFork { 57 + if !slices.Contains(repoTypes, "forks") && repo.IsFork { 58 58 return nil 59 59 } 60 60 61 61 // skip mirrors from being indexed if unit is not present 62 - if !util.SliceContains(repoTypes, "mirrors") && repo.IsMirror { 62 + if !slices.Contains(repoTypes, "mirrors") && repo.IsMirror { 63 63 return nil 64 64 } 65 65 66 66 // skip templates from being indexed if unit is not present 67 - if !util.SliceContains(repoTypes, "templates") && repo.IsTemplate { 67 + if !slices.Contains(repoTypes, "templates") && repo.IsTemplate { 68 68 return nil 69 69 } 70 70 71 71 // skip regular repos from being indexed if unit is not present 72 - if !util.SliceContains(repoTypes, "sources") && !repo.IsFork && !repo.IsMirror && !repo.IsTemplate { 72 + if !slices.Contains(repoTypes, "sources") && !repo.IsFork && !repo.IsMirror && !repo.IsTemplate { 73 73 return nil 74 74 } 75 75
+5 -4
modules/indexer/issues/internal/tests/tests.go
··· 10 10 import ( 11 11 "context" 12 12 "fmt" 13 + "slices" 13 14 "testing" 14 15 "time" 15 16 ··· 457 458 assert.Contains(t, data[v.ID].MentionIDs, int64(1)) 458 459 } 459 460 assert.Equal(t, countIndexerData(data, func(v *internal.IndexerData) bool { 460 - return util.SliceContains(v.MentionIDs, 1) 461 + return slices.Contains(v.MentionIDs, 1) 461 462 }), result.Total) 462 463 }, 463 464 }, ··· 478 479 assert.Contains(t, data[v.ID].ReviewedIDs, int64(1)) 479 480 } 480 481 assert.Equal(t, countIndexerData(data, func(v *internal.IndexerData) bool { 481 - return util.SliceContains(v.ReviewedIDs, 1) 482 + return slices.Contains(v.ReviewedIDs, 1) 482 483 }), result.Total) 483 484 }, 484 485 }, ··· 499 500 assert.Contains(t, data[v.ID].ReviewRequestedIDs, int64(1)) 500 501 } 501 502 assert.Equal(t, countIndexerData(data, func(v *internal.IndexerData) bool { 502 - return util.SliceContains(v.ReviewRequestedIDs, 1) 503 + return slices.Contains(v.ReviewRequestedIDs, 1) 503 504 }), result.Total) 504 505 }, 505 506 }, ··· 520 521 assert.Contains(t, data[v.ID].SubscriberIDs, int64(1)) 521 522 } 522 523 assert.Equal(t, countIndexerData(data, func(v *internal.IndexerData) bool { 523 - return util.SliceContains(v.SubscriberIDs, 1) 524 + return slices.Contains(v.SubscriberIDs, 1) 524 525 }), result.Total) 525 526 }, 526 527 },
+3 -3
modules/templates/base.go
··· 4 4 package templates 5 5 6 6 import ( 7 + "slices" 7 8 "strings" 8 9 9 10 "code.gitea.io/gitea/modules/assetfs" 10 11 "code.gitea.io/gitea/modules/setting" 11 - "code.gitea.io/gitea/modules/util" 12 12 ) 13 13 14 14 func AssetFS() *assetfs.LayeredFS { ··· 24 24 if err != nil { 25 25 return nil, err 26 26 } 27 - return util.SliceRemoveAllFunc(files, func(file string) bool { 27 + return slices.DeleteFunc(files, func(file string) bool { 28 28 return strings.HasPrefix(file, "mail/") || !strings.HasSuffix(file, ".tmpl") 29 29 }), nil 30 30 } ··· 34 34 if err != nil { 35 35 return nil, err 36 36 } 37 - return util.SliceRemoveAllFunc(files, func(file string) bool { 37 + return slices.DeleteFunc(files, func(file string) bool { 38 38 return !strings.HasPrefix(file, "mail/") || !strings.HasSuffix(file, ".tmpl") 39 39 }), nil 40 40 }
+7 -50
modules/util/slice.go
··· 1 1 // Copyright 2022 The Gitea Authors. All rights reserved. 2 2 // SPDX-License-Identifier: MIT 3 3 4 - // Most of the functions in this file can have better implementations with "golang.org/x/exp/slices". 5 - // However, "golang.org/x/exp" is experimental and unreliable, we shouldn't use it. 6 - // So lets waiting for the "slices" has be promoted to the main repository one day. 7 - 8 4 package util 9 5 10 - import "strings" 11 - 12 - // SliceContains returns true if the target exists in the slice. 13 - func SliceContains[T comparable](slice []T, target T) bool { 14 - return SliceContainsFunc(slice, func(t T) bool { return t == target }) 15 - } 16 - 17 - // SliceContainsFunc returns true if any element in the slice satisfies the targetFunc. 18 - func SliceContainsFunc[T any](slice []T, targetFunc func(T) bool) bool { 19 - for _, v := range slice { 20 - if targetFunc(v) { 21 - return true 22 - } 23 - } 24 - return false 25 - } 6 + import ( 7 + "slices" 8 + "strings" 9 + ) 26 10 27 11 // SliceContainsString sequential searches if string exists in slice. 28 12 func SliceContainsString(slice []string, target string, insensitive ...bool) bool { 29 13 if len(insensitive) != 0 && insensitive[0] { 30 14 target = strings.ToLower(target) 31 - return SliceContainsFunc(slice, func(t string) bool { return strings.ToLower(t) == target }) 15 + return slices.ContainsFunc(slice, func(t string) bool { return strings.ToLower(t) == target }) 32 16 } 33 17 34 - return SliceContains(slice, target) 18 + return slices.Contains(slice, target) 35 19 } 36 20 37 21 // SliceSortedEqual returns true if the two slices will be equal when they get sorted. ··· 57 41 return true 58 42 } 59 43 60 - // SliceEqual returns true if the two slices are equal. 61 - func SliceEqual[T comparable](s1, s2 []T) bool { 62 - if len(s1) != len(s2) { 63 - return false 64 - } 65 - 66 - for i, v := range s1 { 67 - if s2[i] != v { 68 - return false 69 - } 70 - } 71 - return true 72 - } 73 - 74 44 // SliceRemoveAll removes all the target elements from the slice. 75 45 func SliceRemoveAll[T comparable](slice []T, target T) []T { 76 - return SliceRemoveAllFunc(slice, func(t T) bool { return t == target }) 77 - } 78 - 79 - // SliceRemoveAllFunc removes all elements which satisfy the targetFunc from the slice. 80 - func SliceRemoveAllFunc[T comparable](slice []T, targetFunc func(T) bool) []T { 81 - idx := 0 82 - for _, v := range slice { 83 - if targetFunc(v) { 84 - continue 85 - } 86 - slice[idx] = v 87 - idx++ 88 - } 89 - return slice[:idx] 46 + return slices.DeleteFunc(slice, func(t T) bool { return t == target }) 90 47 }
-33
modules/util/slice_test.go
··· 9 9 "github.com/stretchr/testify/assert" 10 10 ) 11 11 12 - func TestSliceContains(t *testing.T) { 13 - assert.True(t, SliceContains([]int{2, 0, 2, 3}, 2)) 14 - assert.True(t, SliceContains([]int{2, 0, 2, 3}, 0)) 15 - assert.True(t, SliceContains([]int{2, 0, 2, 3}, 3)) 16 - 17 - assert.True(t, SliceContains([]string{"2", "0", "2", "3"}, "0")) 18 - assert.True(t, SliceContains([]float64{2, 0, 2, 3}, 0)) 19 - assert.True(t, SliceContains([]bool{false, true, false}, true)) 20 - 21 - assert.False(t, SliceContains([]int{2, 0, 2, 3}, 4)) 22 - assert.False(t, SliceContains([]int{}, 4)) 23 - assert.False(t, SliceContains(nil, 4)) 24 - } 25 - 26 12 func TestSliceContainsString(t *testing.T) { 27 13 assert.True(t, SliceContainsString([]string{"c", "b", "a", "b"}, "a")) 28 14 assert.True(t, SliceContainsString([]string{"c", "b", "a", "b"}, "b")) ··· 52 38 assert.False(t, SliceSortedEqual(nil, []int{2, 0, 2, 3})) 53 39 assert.False(t, SliceSortedEqual([]int{2, 0, 2, 4}, []int{2, 0, 2, 3})) 54 40 assert.False(t, SliceSortedEqual([]int{2, 0, 0, 3}, []int{2, 0, 2, 3})) 55 - } 56 - 57 - func TestSliceEqual(t *testing.T) { 58 - assert.True(t, SliceEqual([]int{2, 0, 2, 3}, []int{2, 0, 2, 3})) 59 - assert.True(t, SliceEqual([]int{}, []int{})) 60 - assert.True(t, SliceEqual([]int(nil), nil)) 61 - assert.True(t, SliceEqual([]int(nil), []int{})) 62 - assert.True(t, SliceEqual([]int{}, []int{})) 63 - 64 - assert.True(t, SliceEqual([]string{"2", "0", "2", "3"}, []string{"2", "0", "2", "3"})) 65 - assert.True(t, SliceEqual([]float64{2, 0, 2, 3}, []float64{2, 0, 2, 3})) 66 - assert.True(t, SliceEqual([]bool{false, true, false}, []bool{false, true, false})) 67 - 68 - assert.False(t, SliceEqual([]int{3, 0, 2, 2}, []int{2, 0, 2, 3})) 69 - assert.False(t, SliceEqual([]int{2, 0, 2}, []int{2, 0, 2, 3})) 70 - assert.False(t, SliceEqual([]int{}, []int{2, 0, 2, 3})) 71 - assert.False(t, SliceEqual(nil, []int{2, 0, 2, 3})) 72 - assert.False(t, SliceEqual([]int{2, 0, 2, 4}, []int{2, 0, 2, 3})) 73 - assert.False(t, SliceEqual([]int{2, 0, 0, 3}, []int{2, 0, 2, 3})) 74 41 } 75 42 76 43 func TestSliceRemoveAll(t *testing.T) {
+2 -1
routers/api/packages/chef/auth.go
··· 16 16 "net/http" 17 17 "path" 18 18 "regexp" 19 + "slices" 19 20 "strconv" 20 21 "strings" 21 22 "time" ··· 265 266 } 266 267 } 267 268 268 - if !util.SliceEqual(out[skip:], data) { 269 + if !slices.Equal(out[skip:], data) { 269 270 return fmt.Errorf("could not verify signature") 270 271 } 271 272
+2 -1
routers/api/v1/repo/repo.go
··· 7 7 import ( 8 8 "fmt" 9 9 "net/http" 10 + "slices" 10 11 "strings" 11 12 "time" 12 13 ··· 235 236 } 236 237 237 238 // If the readme template does not exist, a 400 will be returned. 238 - if opt.AutoInit && len(opt.Readme) > 0 && !util.SliceContains(repo_module.Readmes, opt.Readme) { 239 + if opt.AutoInit && len(opt.Readme) > 0 && !slices.Contains(repo_module.Readmes, opt.Readme) { 239 240 ctx.Error(http.StatusBadRequest, "", fmt.Errorf("readme template does not exist, available templates: %v", repo_module.Readmes)) 240 241 return 241 242 }
+2 -1
routers/web/repo/issue.go
··· 12 12 "math/big" 13 13 "net/http" 14 14 "net/url" 15 + "slices" 15 16 "sort" 16 17 "strconv" 17 18 "strings" ··· 3628 3629 if search == "" && ctx.Doer != nil { 3629 3630 // the returned posters slice only contains limited number of users, 3630 3631 // to make the current user (doer) can quickly filter their own issues, always add doer to the posters slice 3631 - if !util.SliceContainsFunc(posters, func(user *user_model.User) bool { return user.ID == ctx.Doer.ID }) { 3632 + if !slices.ContainsFunc(posters, func(user *user_model.User) bool { return user.ID == ctx.Doer.ID }) { 3632 3633 posters = append(posters, ctx.Doer) 3633 3634 } 3634 3635 }
+3 -2
routers/web/repo/repo.go
··· 8 8 "errors" 9 9 "fmt" 10 10 "net/http" 11 + "slices" 11 12 "strings" 12 13 13 14 "code.gitea.io/gitea/models" ··· 659 660 } 660 661 resp := &branchTagSearchResponse{} 661 662 // always put default branch on the top if it exists 662 - if util.SliceContains(branches, ctx.Repo.Repository.DefaultBranch) { 663 + if slices.Contains(branches, ctx.Repo.Repository.DefaultBranch) { 663 664 branches = util.SliceRemoveAll(branches, ctx.Repo.Repository.DefaultBranch) 664 665 branches = append([]string{ctx.Repo.Repository.DefaultBranch}, branches...) 665 666 } ··· 693 694 return 694 695 } 695 696 // always put default branch on the top if it exists 696 - if util.SliceContains(brs, ctx.Repo.Repository.DefaultBranch) { 697 + if slices.Contains(brs, ctx.Repo.Repository.DefaultBranch) { 697 698 brs = util.SliceRemoveAll(brs, ctx.Repo.Repository.DefaultBranch) 698 699 brs = append([]string{ctx.Repo.Repository.DefaultBranch}, brs...) 699 700 }
+2 -1
routers/web/repo/view.go
··· 14 14 "net/http" 15 15 "net/url" 16 16 "path" 17 + "slices" 17 18 "strings" 18 19 "time" 19 20 ··· 370 371 if workFlowErr != nil { 371 372 ctx.Data["FileError"] = ctx.Locale.Tr("actions.runs.invalid_workflow_helper", workFlowErr.Error()) 372 373 } 373 - } else if util.SliceContains([]string{"CODEOWNERS", "docs/CODEOWNERS", ".gitea/CODEOWNERS"}, ctx.Repo.TreePath) { 374 + } else if slices.Contains([]string{"CODEOWNERS", "docs/CODEOWNERS", ".gitea/CODEOWNERS"}, ctx.Repo.TreePath) { 374 375 if data, err := blob.GetBlobContent(setting.UI.MaxDisplayFileSize); err == nil { 375 376 _, warnings := issue_model.GetCodeOwnersFromContent(ctx, data) 376 377 if len(warnings) > 0 {
+3 -2
routers/web/user/home.go
··· 9 9 "fmt" 10 10 "net/http" 11 11 "regexp" 12 + "slices" 12 13 "sort" 13 14 "strconv" 14 15 "strings" ··· 290 291 if len(repoIDs) == 0 { 291 292 repoIDs = showRepoIds.Values() 292 293 } 293 - repoIDs = util.SliceRemoveAllFunc(repoIDs, func(v int64) bool { 294 + repoIDs = slices.DeleteFunc(repoIDs, func(v int64) bool { 294 295 return !showRepoIds.Contains(v) 295 296 }) 296 297 ··· 534 535 // Gets set when clicking filters on the issues overview page. 535 536 selectedRepoIDs := getRepoIDs(ctx.FormString("repos")) 536 537 // Remove repo IDs that are not accessible to the user. 537 - selectedRepoIDs = util.SliceRemoveAllFunc(selectedRepoIDs, func(v int64) bool { 538 + selectedRepoIDs = slices.DeleteFunc(selectedRepoIDs, func(v int64) bool { 538 539 return !accessibleRepos.Contains(v) 539 540 }) 540 541 if len(selectedRepoIDs) > 0 {