chore(perf): Pre-allocate where possible (enable prealloc linter) (#88952)

* chore(perf): Pre-allocate where possible (enable prealloc linter)

Signed-off-by: Dave Henderson <dave.henderson@grafana.com>

* fix TestAlertManagers_buildRedactedAMs

Signed-off-by: Dave Henderson <dave.henderson@grafana.com>

* prealloc a slice that appeared after rebase

Signed-off-by: Dave Henderson <dave.henderson@grafana.com>

---------

Signed-off-by: Dave Henderson <dave.henderson@grafana.com>
This commit is contained in:
Dave Henderson
2024-06-14 14:16:36 -04:00
committed by GitHub
parent e53e6e7caa
commit 6262c56132
43 changed files with 211 additions and 140 deletions

View File

@ -93,6 +93,8 @@ func TestDefaultStaticDetectorsInspector(t *testing.T) {
plugin *plugins.Plugin
exp bool
}
//nolint:prealloc // just a test, and it'd require too much refactoring to preallocate
var tcs []tc
// Angular imports

View File

@ -381,11 +381,13 @@ func TestFSPathSeparatorFiles(t *testing.T) {
}
func fileList(manifest *PluginManifest) []string {
var keys []string
keys := make([]string, 0, len(manifest.Files))
for k := range manifest.Files {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}

View File

@ -62,9 +62,10 @@ func DirAsLocalSources(pluginsPath string, class plugins.Class) ([]*LocalSource,
}
slices.Sort(pluginDirs)
var sources []*LocalSource
for _, dir := range pluginDirs {
sources = append(sources, NewLocalSource(class, []string{dir}))
sources := make([]*LocalSource, len(pluginDirs))
for i, dir := range pluginDirs {
sources[i] = NewLocalSource(class, []string{dir})
}
return sources, nil
}

View File

@ -46,7 +46,7 @@ func TestDirAsLocalSources(t *testing.T) {
{
name: "Directory with no subdirectories",
pluginsPath: filepath.Join(testdataDir, "pluginRootWithDist", "datasource"),
expected: nil,
expected: []*LocalSource{},
},
{
name: "Directory with a symlink to a directory",

View File

@ -38,22 +38,25 @@ func (s *Service) externalPluginSources() []plugins.PluginSource {
return []plugins.PluginSource{}
}
var srcs []plugins.PluginSource
for _, src := range localSrcs {
srcs = append(srcs, src)
srcs := make([]plugins.PluginSource, len(localSrcs))
for i, src := range localSrcs {
srcs[i] = src
}
return srcs
}
func (s *Service) pluginSettingSources() []plugins.PluginSource {
var sources []plugins.PluginSource
sources := make([]plugins.PluginSource, 0, len(s.cfg.PluginSettings))
for _, ps := range s.cfg.PluginSettings {
path, exists := ps["path"]
if !exists || path == "" {
continue
}
sources = append(sources, NewLocalSource(plugins.ClassExternal, []string{path}))
}
return sources
}