Skip to content

Commit 4d8287b

Browse files
peaklejoeblubaugh
andauthored
Performance: add preallocation for some slice/map (grafana#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>
1 parent 460be70 commit 4d8287b

File tree

8 files changed

+15
-13
lines changed

8 files changed

+15
-13
lines changed

pkg/api/dtos/models.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (mr *MetricRequest) GetUniqueDatasourceTypes() []string {
8888
}
8989
}
9090

91-
res := make([]string, 0)
91+
res := make([]string, 0, len(dsTypes))
9292
for dsType := range dsTypes {
9393
res = append(res, dsType)
9494
}

pkg/api/frontendlogging/sentry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (value *FrontendSentryExceptionValue) FmtStacktrace(store *SourceMapStore)
5959
}
6060

6161
func (exception *FrontendSentryException) FmtStacktraces(store *SourceMapStore) string {
62-
var stacktraces []string
62+
stacktraces := make([]string, 0, len(exception.Values))
6363
for _, value := range exception.Values {
6464
stacktraces = append(stacktraces, value.FmtStacktrace(store))
6565
}

pkg/plugins/manager/loader/initializer/initializer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (i *Initializer) awsEnvVars() []string {
8787
type pluginSettings map[string]string
8888

8989
func (ps pluginSettings) asEnvVar(prefix string, hostEnv []string) []string {
90-
var env []string
90+
env := make([]string, 0, len(ps))
9191
for k, v := range ps {
9292
key := fmt.Sprintf("%s_%s", prefix, strings.ToUpper(k))
9393
if value := os.Getenv(key); value != "" {

pkg/services/ngalert/notifier/channels/alertmanager.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8-
98
"net/url"
109
"strings"
1110

@@ -34,18 +33,21 @@ func NewAlertmanagerConfig(config *NotificationChannelConfig, fn GetDecryptedVal
3433
if urlStr == "" {
3534
return nil, errors.New("could not find url property in settings")
3635
}
37-
var urls []*url.URL
38-
for _, uS := range strings.Split(urlStr, ",") {
36+
37+
urlParts := strings.Split(urlStr, ",")
38+
urls := make([]*url.URL, 0, len(urlParts))
39+
40+
for _, uS := range urlParts {
3941
uS = strings.TrimSpace(uS)
4042
if uS == "" {
4143
continue
4244
}
4345
uS = strings.TrimSuffix(uS, "/") + "/api/v1/alerts"
44-
url, err := url.Parse(uS)
46+
u, err := url.Parse(uS)
4547
if err != nil {
4648
return nil, fmt.Errorf("invalid url property in settings: %w", err)
4749
}
48-
urls = append(urls, url)
50+
urls = append(urls, u)
4951
}
5052
return &AlertmanagerConfig{
5153
NotificationChannelConfig: config,

pkg/services/provisioning/dashboards/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func createDashboardJSON(data *simplejson.Json, lastModified time.Time, cfg *con
7474
}
7575

7676
func mapV0ToDashboardsAsConfig(v0 []*configV0) ([]*config, error) {
77-
var r []*config
77+
r := make([]*config, 0, len(v0))
7878
seen := make(map[string]bool)
7979

8080
for _, v := range v0 {
@@ -101,7 +101,7 @@ func mapV0ToDashboardsAsConfig(v0 []*configV0) ([]*config, error) {
101101
}
102102

103103
func (dc *configV1) mapToDashboardsAsConfig() ([]*config, error) {
104-
var r []*config
104+
r := make([]*config, 0, len(dc.Providers))
105105
seen := make(map[string]bool)
106106

107107
for _, v := range dc.Providers {

pkg/services/sqlstore/migrations/ualert/channel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func (m *migration) createNotifier(c *notificationChannel) (*PostableGrafanaRece
212212

213213
// Create one receiver for every unique notification channel.
214214
func (m *migration) createReceivers(allChannels []*notificationChannel) (map[uidOrID]*PostableApiReceiver, []*PostableApiReceiver, error) {
215-
var receivers []*PostableApiReceiver
215+
receivers := make([]*PostableApiReceiver, 0, len(allChannels))
216216
receiversMap := make(map[uidOrID]*PostableApiReceiver)
217217

218218
set := make(map[string]struct{}) // Used to deduplicate sanitized names.

pkg/services/updatechecker/plugins.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func canUpdate(v1, v2 string) bool {
146146
}
147147

148148
func (s *PluginsService) pluginIDsCSV(m map[string]plugins.PluginDTO) string {
149-
var ids []string
149+
ids := make([]string, 0, len(m))
150150
for pluginID := range m {
151151
ids = append(ids, pluginID)
152152
}

pkg/tsdb/prometheus/querydata/request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (s *QueryData) trace(ctx context.Context, q *models.Query) (context.Context
185185
}
186186

187187
func sdkHeaderToHttpHeader(headers map[string]string) http.Header {
188-
httpHeader := make(http.Header)
188+
httpHeader := make(http.Header, len(headers))
189189
for key, val := range headers {
190190
httpHeader[key] = []string{val}
191191
}

0 commit comments

Comments
 (0)