Skip to content

Commit e100f2d

Browse files
chmouelzakisk
authored andcommitted
refactor: reenable prealloc and fix it properly
The `prealloc` linter was properly enabled in the `.golangci.yml` configuration. Optimized various parts of the codebase by pre-allocating slices and utilizing `make` with a capacity when appropriate. This change aimed to improve performance by reducing the overhead associated with repeated slice appends.
1 parent 806c41e commit e100f2d

File tree

13 files changed

+28
-30
lines changed

13 files changed

+28
-30
lines changed

pkg/cli/browser/browser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
func OpenWebBrowser(ctx context.Context, url string) error {
1111
var cmd string
1212

13-
args := []string{}
13+
args := make([]string, 0, 1)
1414
switch runtime.GOOS {
1515
case "windows":
1616
cmd = "cmd"

pkg/matcher/annotation_matcher_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,12 +1361,12 @@ func TestMatchPipelinerunAnnotationAndRepositories(t *testing.T) {
13611361
tt.args.runevent.Request = &info.Request{Header: http.Header{}, Payload: nil}
13621362
}
13631363
if len(tt.args.fileChanged) > 0 {
1364-
commitFiles := []*github.CommitFile{}
1365-
for _, v := range tt.args.fileChanged {
1366-
commitFiles = append(commitFiles, &github.CommitFile{
1364+
commitFiles := make([]*github.CommitFile, len(tt.args.fileChanged))
1365+
for i, v := range tt.args.fileChanged {
1366+
commitFiles[i] = &github.CommitFile{
13671367
Filename: github.Ptr(v.FileName),
13681368
Status: github.Ptr(v.Status),
1369-
})
1369+
}
13701370
}
13711371
if tt.args.runevent.TriggerTarget == "push" {
13721372
mux.HandleFunc(fmt.Sprintf("/repos/%s/%s/commits/%s",

pkg/pipelineascode/match_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,9 @@ func TestGetPipelineRunsFromRepo(t *testing.T) {
335335
p.eventEmitter = events.NewEventEmitter(stdata.Kube, logger)
336336
matchedPRs, err := p.getPipelineRunsFromRepo(ctx, tt.repositories)
337337
assert.NilError(t, err)
338-
matchedPRNames := []string{}
338+
matchedPRNames := make([]string, len(matchedPRs))
339339
for i := range matchedPRs {
340-
matchedPRNames = append(matchedPRNames, matchedPRs[i].PipelineRun.GetGenerateName())
340+
matchedPRNames[i] = matchedPRs[i].PipelineRun.GetGenerateName()
341341
}
342342
if tt.logSnippet != "" {
343343
assert.Assert(t, logCatcher.FilterMessageSnippet(tt.logSnippet).Len() > 0, logCatcher.All())

pkg/provider/gitea/test/setup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ func Setup(t *testing.T) (*gitea.Client, *http.ServeMux, func()) {
5252

5353
// SetupGitTree Take a dir and fake a full GitTree Gitea api calls reply recursively over a muxer.
5454
func SetupGitTree(t *testing.T, mux *http.ServeMux, dir string, event *info.Event, recursive bool) {
55-
entries := []gitea.GitEntry{}
5655
type file struct {
5756
sha, name string
5857
isdir bool
@@ -76,6 +75,7 @@ func SetupGitTree(t *testing.T, mux *http.ServeMux, dir string, event *info.Even
7675
files = append(files, file{name: filepath.Join(dir, f.Name()), sha: sha, isdir: f.IsDir()})
7776
}
7877
}
78+
entries := make([]gitea.GitEntry, 0, len(files))
7979
for _, f := range files {
8080
etype := "blob"
8181
mode := "100644"

pkg/secrets/secrets.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ func GetSecretsAttachedToPipelineRun(ctx context.Context, k kubeinteraction.Inte
6666
// if we don't sort by longest then if there two passwords with the same prefix
6767
// the shortest one will replace and would leak the end of the passwords of the longest after.
6868
func sortSecretsByLongests(values []ktypes.SecretValue) []ktypes.SecretValue {
69-
ret := []ktypes.SecretValue{}
70-
ret = append(ret, values...)
69+
ret := make([]ktypes.SecretValue, len(values))
70+
copy(ret, values)
7171
for i := 0; i < len(ret); i++ {
7272
for j := i + 1; j < len(ret); j++ {
7373
if len(ret[i].Value) < len(ret[j].Value) {

pkg/sort/repository_status.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ func (rs repoSortRunStatus) Less(i, j int) bool {
2929
}
3030

3131
func RepositorySortRunStatus(repoStatus []v1alpha1.RepositoryRunStatus) []v1alpha1.RepositoryRunStatus {
32-
rrstatus := repoSortRunStatus{}
33-
for _, status := range repoStatus {
34-
rrstatus = append(rrstatus, status)
35-
}
32+
rrstatus := make(repoSortRunStatus, len(repoStatus))
33+
copy(rrstatus, repoStatus)
3634
sort.Sort(rrstatus)
3735
return rrstatus
3836
}

pkg/sort/task_log_snippets.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func (s taskInfoSorter) Less(i, j int) bool {
1919
}
2020

2121
func TaskInfos(taskinfos map[string]pacv1alpha1.TaskInfos) []pacv1alpha1.TaskInfos {
22-
tis := taskInfoSorter{}
22+
tis := make(taskInfoSorter, 0, len(taskinfos))
2323
for _, ti := range taskinfos {
2424
tis = append(tis, ti)
2525
}

pkg/sync/queue_manager.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,15 @@ func (qm *QueueManager) RunningPipelineRuns(repo *v1alpha1.Repository) []string
290290
}
291291

292292
func sortPipelineRunsByCreationTimestamp(prs []tektonv1.PipelineRun) []*tektonv1.PipelineRun {
293-
runTimeObj := []runtime.Object{}
293+
runTimeObj := make([]runtime.Object, len(prs))
294294
for i := range prs {
295-
runTimeObj = append(runTimeObj, &prs[i])
295+
runTimeObj[i] = &prs[i]
296296
}
297297
sort.ByField(creationTimestamp, runTimeObj)
298-
sortedPRs := []*tektonv1.PipelineRun{}
299-
for _, run := range runTimeObj {
298+
sortedPRs := make([]*tektonv1.PipelineRun, len(runTimeObj))
299+
for i, run := range runTimeObj {
300300
pr, _ := run.(*tektonv1.PipelineRun)
301-
sortedPRs = append(sortedPRs, pr)
301+
sortedPRs[i] = pr
302302
}
303303
return sortedPRs
304304
}

pkg/sync/queue_manager_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,9 @@ func TestFilterPipelineRunByInProgress(t *testing.T) {
329329
PipelineRuns: pipelineRuns,
330330
}
331331

332-
orderList := []string{}
333-
for _, pr := range pipelineRuns {
334-
orderList = append(orderList, fmt.Sprintf("%s/%s", ns, pr.GetName()))
332+
orderList := make([]string, len(pipelineRuns))
333+
for i, pr := range pipelineRuns {
334+
orderList[i] = fmt.Sprintf("%s/%s", ns, pr.GetName())
335335
}
336336
stdata, _ := testclient.SeedTestData(t, ctx, tdata)
337337
filtered := FilterPipelineRunByState(ctx, stdata.Pipeline, orderList, tektonv1.PipelineRunSpecStatusPending, kubeinteraction.StateQueued)

pkg/sync/semaphore.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ func (s *prioritySemaphore) getLimit() int {
3939
}
4040

4141
func (s *prioritySemaphore) getCurrentPending() []string {
42-
keys := []string{}
42+
keys := make([]string, 0, len(s.pending.items))
4343
for _, item := range s.pending.items {
4444
keys = append(keys, item.key)
4545
}
4646
return keys
4747
}
4848

4949
func (s *prioritySemaphore) getCurrentRunning() []string {
50-
keys := []string{}
50+
keys := make([]string, 0, len(s.running))
5151
for k := range s.running {
5252
keys = append(keys, k)
5353
}

0 commit comments

Comments
 (0)