From 90badc872981b3a91168b15edd81a76155915dfe Mon Sep 17 00:00:00 2001 From: Denis Limarev Date: Wed, 11 Jan 2023 23:03:37 +0600 Subject: [PATCH] Performance: Add preallocation for some slices (#59593) --- pkg/services/alerting/conditions/query.go | 5 ++--- pkg/services/alerting/notifiers/googlechat.go | 1 + pkg/services/contexthandler/authproxy/authproxy.go | 2 +- pkg/services/live/pipeline/frame_output_remote_write.go | 2 +- pkg/services/live/pipeline/rule_builder_storage.go | 2 +- pkg/services/ngalert/notifier/alertmanager.go | 4 ++-- pkg/services/ngalert/notifier/receivers.go | 4 ++-- pkg/services/ngalert/state/cache.go | 2 +- 8 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pkg/services/alerting/conditions/query.go b/pkg/services/alerting/conditions/query.go index f14416c3271..a51c9d3b6ed 100644 --- a/pkg/services/alerting/conditions/query.go +++ b/pkg/services/alerting/conditions/query.go @@ -1,6 +1,7 @@ package conditions import ( + gocontext "context" "errors" "fmt" "strings" @@ -10,8 +11,6 @@ import ( "github.com/grafana/grafana/pkg/tsdb/legacydata/interval" "github.com/grafana/grafana/pkg/tsdb/prometheus" - gocontext "context" - "github.com/grafana/grafana-plugin-sdk-go/data" "github.com/grafana/grafana/pkg/components/null" @@ -61,7 +60,7 @@ func (c *QueryCondition) Eval(context *alerting.EvalContext, requestHandler lega // matches represents all the series that violate the alert condition var matches []*alerting.EvalMatch // allMatches capture all evaluation matches irregardless on whether the condition is met or not - var allMatches []*alerting.EvalMatch + allMatches := make([]*alerting.EvalMatch, 0, len(seriesList)) for _, series := range seriesList { reducedValue := c.Reducer.Reduce(series) diff --git a/pkg/services/alerting/notifiers/googlechat.go b/pkg/services/alerting/notifiers/googlechat.go index 0b1e8080f9e..90b2f2e1c1f 100644 --- a/pkg/services/alerting/notifiers/googlechat.go +++ b/pkg/services/alerting/notifiers/googlechat.go @@ -144,6 +144,7 @@ func (gcn *GoogleChatNotifier) Notify(evalContext *alerting.EvalContext) error { } // add a text paragraph widget for the fields + //nolint:prealloc // break block var fields []textParagraphWidget fieldLimitCount := 4 for index, evt := range evalContext.EvalMatches { diff --git a/pkg/services/contexthandler/authproxy/authproxy.go b/pkg/services/contexthandler/authproxy/authproxy.go index ee2305733db..73c9523d50a 100644 --- a/pkg/services/contexthandler/authproxy/authproxy.go +++ b/pkg/services/contexthandler/authproxy/authproxy.go @@ -110,7 +110,7 @@ func (auth *AuthProxy) IsAllowedIP(ip string) error { } proxies := strings.Split(auth.cfg.AuthProxyWhitelist, ",") - var proxyObjs []*net.IPNet + proxyObjs := make([]*net.IPNet, 0, len(proxies)) for _, proxy := range proxies { result, err := coerceProxyAddress(proxy) if err != nil { diff --git a/pkg/services/live/pipeline/frame_output_remote_write.go b/pkg/services/live/pipeline/frame_output_remote_write.go index cb4ff658016..6a75c2e9dbd 100644 --- a/pkg/services/live/pipeline/frame_output_remote_write.go +++ b/pkg/services/live/pipeline/frame_output_remote_write.go @@ -118,7 +118,7 @@ func (out *RemoteWriteFrameOutput) sample(timeSeries []prompb.TimeSeries) []prom sample.Samples = append(sample.Samples, filteredSamples...) samples[name] = sample } - var toReturn []prompb.TimeSeries + toReturn := make([]prompb.TimeSeries, 0, len(samples)) for _, ts := range samples { toReturn = append(toReturn, ts) } diff --git a/pkg/services/live/pipeline/rule_builder_storage.go b/pkg/services/live/pipeline/rule_builder_storage.go index 99c5002d3bb..a8500e766e8 100644 --- a/pkg/services/live/pipeline/rule_builder_storage.go +++ b/pkg/services/live/pipeline/rule_builder_storage.go @@ -309,7 +309,7 @@ func (f *StorageRuleBuilder) BuildRules(ctx context.Context, orgID int64) ([]*Li return nil, err } - var rules []*LiveChannelRule + rules := make([]*LiveChannelRule, 0, len(channelRules)) for _, ruleConfig := range channelRules { rule := &LiveChannelRule{ diff --git a/pkg/services/ngalert/notifier/alertmanager.go b/pkg/services/ngalert/notifier/alertmanager.go index 2906ff3617d..4653f87749b 100644 --- a/pkg/services/ngalert/notifier/alertmanager.go +++ b/pkg/services/ngalert/notifier/alertmanager.go @@ -430,7 +430,7 @@ func (am *Alertmanager) applyConfig(cfg *apimodels.PostableUserConfig, rawConfig am.dispatcher = dispatch.NewDispatcher(am.alerts, am.route, routingStage, am.marker, am.timeoutFunc, &nilLimits{}, am.logger, am.dispatcherMetrics) // Check which receivers are active and create the receiver stage. - var receivers []*notify.Receiver + receivers := make([]*notify.Receiver, 0, len(integrationsMap)) activeReceivers := am.getActiveReceiversMap(am.route) for name := range integrationsMap { stage := am.createReceiverStage(name, integrationsMap[name], am.waitFunc, am.notificationLog) @@ -479,7 +479,7 @@ func (am *Alertmanager) buildIntegrationsMap(receivers []*apimodels.PostableApiR // buildReceiverIntegrations builds a list of integration notifiers off of a receiver config. func (am *Alertmanager) buildReceiverIntegrations(receiver *apimodels.PostableApiReceiver, tmpl *template.Template) ([]*notify.Integration, error) { - var integrations []*notify.Integration + integrations := make([]*notify.Integration, 0, len(receiver.GrafanaManagedReceivers)) for i, r := range receiver.GrafanaManagedReceivers { n, err := am.buildReceiverIntegration(r, tmpl) if err != nil { diff --git a/pkg/services/ngalert/notifier/receivers.go b/pkg/services/ngalert/notifier/receivers.go index 1b9200e0e0c..3d029f034c7 100644 --- a/pkg/services/ngalert/notifier/receivers.go +++ b/pkg/services/ngalert/notifier/receivers.go @@ -203,10 +203,10 @@ func (am *Alertmanager) GetReceivers(ctx context.Context) []apimodels.Receiver { am.reloadConfigMtx.RLock() defer am.reloadConfigMtx.RUnlock() - var apiReceivers []apimodels.Receiver + apiReceivers := make([]apimodels.Receiver, 0, len(am.receivers)) for _, rcv := range am.receivers { // Build integrations slice for each receiver. - var integrations []*models.Integration + integrations := make([]*models.Integration, 0, len(rcv.Integrations())) for _, integration := range rcv.Integrations() { name := integration.Name() sendResolved := integration.SendResolved() diff --git a/pkg/services/ngalert/state/cache.go b/pkg/services/ngalert/state/cache.go index e3d3d0d52e5..7cee0e1a30d 100644 --- a/pkg/services/ngalert/state/cache.go +++ b/pkg/services/ngalert/state/cache.go @@ -222,7 +222,6 @@ func (c *cache) getAll(orgID int64) []*State { } func (c *cache) getStatesForRuleUID(orgID int64, alertRuleUID string) []*State { - var result []*State c.mtxStates.RLock() defer c.mtxStates.RUnlock() orgRules, ok := c.states[orgID] @@ -233,6 +232,7 @@ func (c *cache) getStatesForRuleUID(orgID int64, alertRuleUID string) []*State { if !ok { return nil } + result := make([]*State, 0, len(rs.states)) for _, state := range rs.states { result = append(result, state) }