Performance: add preallocation for some slice/map (#57860)

This change preallocates slices and maps where the size of the data is known before the object is created.

Co-authored-by: Joe Blubaugh <joe.blubaugh@grafana.com>
This commit is contained in:
Denis Limarev 2022-11-22 18:24:36 +06:00 committed by GitHub
parent 460be70261
commit 4d8287b319
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 15 additions and 13 deletions

View File

@ -88,7 +88,7 @@ func (mr *MetricRequest) GetUniqueDatasourceTypes() []string {
}
}
res := make([]string, 0)
res := make([]string, 0, len(dsTypes))
for dsType := range dsTypes {
res = append(res, dsType)
}

View File

@ -59,7 +59,7 @@ func (value *FrontendSentryExceptionValue) FmtStacktrace(store *SourceMapStore)
}
func (exception *FrontendSentryException) FmtStacktraces(store *SourceMapStore) string {
var stacktraces []string
stacktraces := make([]string, 0, len(exception.Values))
for _, value := range exception.Values {
stacktraces = append(stacktraces, value.FmtStacktrace(store))
}

View File

@ -87,7 +87,7 @@ func (i *Initializer) awsEnvVars() []string {
type pluginSettings map[string]string
func (ps pluginSettings) asEnvVar(prefix string, hostEnv []string) []string {
var env []string
env := make([]string, 0, len(ps))
for k, v := range ps {
key := fmt.Sprintf("%s_%s", prefix, strings.ToUpper(k))
if value := os.Getenv(key); value != "" {

View File

@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strings"
@ -34,18 +33,21 @@ func NewAlertmanagerConfig(config *NotificationChannelConfig, fn GetDecryptedVal
if urlStr == "" {
return nil, errors.New("could not find url property in settings")
}
var urls []*url.URL
for _, uS := range strings.Split(urlStr, ",") {
urlParts := strings.Split(urlStr, ",")
urls := make([]*url.URL, 0, len(urlParts))
for _, uS := range urlParts {
uS = strings.TrimSpace(uS)
if uS == "" {
continue
}
uS = strings.TrimSuffix(uS, "/") + "/api/v1/alerts"
url, err := url.Parse(uS)
u, err := url.Parse(uS)
if err != nil {
return nil, fmt.Errorf("invalid url property in settings: %w", err)
}
urls = append(urls, url)
urls = append(urls, u)
}
return &AlertmanagerConfig{
NotificationChannelConfig: config,

View File

@ -74,7 +74,7 @@ func createDashboardJSON(data *simplejson.Json, lastModified time.Time, cfg *con
}
func mapV0ToDashboardsAsConfig(v0 []*configV0) ([]*config, error) {
var r []*config
r := make([]*config, 0, len(v0))
seen := make(map[string]bool)
for _, v := range v0 {
@ -101,7 +101,7 @@ func mapV0ToDashboardsAsConfig(v0 []*configV0) ([]*config, error) {
}
func (dc *configV1) mapToDashboardsAsConfig() ([]*config, error) {
var r []*config
r := make([]*config, 0, len(dc.Providers))
seen := make(map[string]bool)
for _, v := range dc.Providers {

View File

@ -212,7 +212,7 @@ func (m *migration) createNotifier(c *notificationChannel) (*PostableGrafanaRece
// Create one receiver for every unique notification channel.
func (m *migration) createReceivers(allChannels []*notificationChannel) (map[uidOrID]*PostableApiReceiver, []*PostableApiReceiver, error) {
var receivers []*PostableApiReceiver
receivers := make([]*PostableApiReceiver, 0, len(allChannels))
receiversMap := make(map[uidOrID]*PostableApiReceiver)
set := make(map[string]struct{}) // Used to deduplicate sanitized names.

View File

@ -146,7 +146,7 @@ func canUpdate(v1, v2 string) bool {
}
func (s *PluginsService) pluginIDsCSV(m map[string]plugins.PluginDTO) string {
var ids []string
ids := make([]string, 0, len(m))
for pluginID := range m {
ids = append(ids, pluginID)
}

View File

@ -185,7 +185,7 @@ func (s *QueryData) trace(ctx context.Context, q *models.Query) (context.Context
}
func sdkHeaderToHttpHeader(headers map[string]string) http.Header {
httpHeader := make(http.Header)
httpHeader := make(http.Header, len(headers))
for key, val := range headers {
httpHeader[key] = []string{val}
}