Enable prealloc linter

This will give us nice little performance gains in some code paths.

Signed-off-by: Milos Gajdos <milosthegajdos@gmail.com>
This commit is contained in:
Milos Gajdos 2023-09-03 22:41:51 +01:00
parent a2e65220ae
commit 59fd8656ac
No known key found for this signature in database
GPG key ID: 01300E5E6D417439
10 changed files with 20 additions and 12 deletions

View file

@ -10,6 +10,7 @@ linters:
- unused - unused
- misspell - misspell
- bodyclose - bodyclose
- prealloc
disable: disable:
- errcheck - errcheck

View file

@ -90,7 +90,7 @@ func (ErrManifestUnverified) Error() string {
type ErrManifestVerification []error type ErrManifestVerification []error
func (errs ErrManifestVerification) Error() string { func (errs ErrManifestVerification) Error() string {
var parts []string parts := make([]string, 0, len(errs))
for _, err := range errs { for _, err := range errs {
parts = append(parts, err.Error()) parts = append(parts, err.Error())
} }

View file

@ -441,6 +441,7 @@ func (app *App) register(routeName string, dispatch dispatchFunc) {
// configureEvents prepares the event sink for action. // configureEvents prepares the event sink for action.
func (app *App) configureEvents(configuration *configuration.Configuration) { func (app *App) configureEvents(configuration *configuration.Configuration) {
// Configure all of the endpoint sinks. // Configure all of the endpoint sinks.
// nolint:prealloc
var sinks []events.Sink var sinks []events.Sink
for _, endpoint := range configuration.Notifications.Endpoints { for _, endpoint := range configuration.Notifications.Endpoints {
if endpoint.Disabled { if endpoint.Disabled {

View file

@ -49,7 +49,7 @@ func (m *mockTagStore) All(ctx context.Context) ([]string, error) {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
var tags []string tags := make([]string, 0, len(m.mapping))
for tag := range m.mapping { for tag := range m.mapping {
tags = append(tags, tag) tags = append(tags, tag)
} }

View file

@ -416,7 +416,7 @@ func directDescendants(blobs []string, prefix string) []string {
} }
} }
var keys []string keys := make([]string, 0, len(out))
for k := range out { for k := range out {
keys = append(keys, k) keys = append(keys, k)
} }

View file

@ -1,6 +1,7 @@
package inmemory package inmemory
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"path" "path"
@ -97,8 +98,13 @@ func (d *dir) list(p string) ([]string, error) {
return nil, errIsNotDir return nil, errIsNotDir
} }
var children []string dir, ok := n.(*dir)
for _, child := range n.(*dir).children { if !ok {
return nil, errors.New("not a directory")
}
children := make([]string, 0, len(dir.children))
for _, child := range dir.children {
children = append(children, child.path()) children = append(children, child.path())
} }

View file

@ -1456,7 +1456,7 @@ func (w *writer) Commit() error {
} }
w.committed = true w.committed = true
var completedUploadedParts completedParts completedUploadedParts := make(completedParts, 0, len(w.parts))
for _, part := range w.parts { for _, part := range w.parts {
completedUploadedParts = append(completedUploadedParts, &s3.CompletedPart{ completedUploadedParts = append(completedUploadedParts, &s3.CompletedPart{
ETag: part.ETag, ETag: part.ETag,

View file

@ -500,6 +500,7 @@ func TestWalk(t *testing.T) {
} }
// create file structure matching fileset above // create file structure matching fileset above
// nolint:prealloc
var created []string var created []string
for _, p := range fileset { for _, p := range fileset {
err := drvr.PutContent(context.Background(), p, []byte("content "+p)) err := drvr.PutContent(context.Background(), p, []byte("content "+p))

View file

@ -24,25 +24,24 @@ type tagStore struct {
// All returns all tags // All returns all tags
func (ts *tagStore) All(ctx context.Context) ([]string, error) { func (ts *tagStore) All(ctx context.Context) ([]string, error) {
var tags []string
pathSpec, err := pathFor(manifestTagPathSpec{ pathSpec, err := pathFor(manifestTagPathSpec{
name: ts.repository.Named().Name(), name: ts.repository.Named().Name(),
}) })
if err != nil { if err != nil {
return tags, err return nil, err
} }
entries, err := ts.blobStore.driver.List(ctx, pathSpec) entries, err := ts.blobStore.driver.List(ctx, pathSpec)
if err != nil { if err != nil {
switch err := err.(type) { switch err := err.(type) {
case storagedriver.PathNotFoundError: case storagedriver.PathNotFoundError:
return tags, distribution.ErrRepositoryUnknown{Name: ts.repository.Named().Name()} return nil, distribution.ErrRepositoryUnknown{Name: ts.repository.Named().Name()}
default: default:
return tags, err return nil, err
} }
} }
tags := make([]string, 0, len(entries))
for _, entry := range entries { for _, entry := range entries {
_, filename := path.Split(entry) _, filename := path.Split(entry)
tags = append(tags, filename) tags = append(tags, filename)

View file

@ -14,7 +14,7 @@ import (
func MakeManifestList(blobstatter distribution.BlobStatter, manifestDigests []digest.Digest) (*manifestlist.DeserializedManifestList, error) { func MakeManifestList(blobstatter distribution.BlobStatter, manifestDigests []digest.Digest) (*manifestlist.DeserializedManifestList, error) {
ctx := context.Background() ctx := context.Background()
var manifestDescriptors []manifestlist.ManifestDescriptor manifestDescriptors := make([]manifestlist.ManifestDescriptor, 0, len(manifestDigests))
for _, manifestDigest := range manifestDigests { for _, manifestDigest := range manifestDigests {
descriptor, err := blobstatter.Stat(ctx, manifestDigest) descriptor, err := blobstatter.Stat(ctx, manifestDigest)
if err != nil { if err != nil {