Performance: Add preallocation for some slices (#59593)

This commit is contained in:
Denis Limarev 2023-01-11 23:03:37 +06:00 committed by GitHub
parent 8bda8b8272
commit 90badc8729
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 11 additions and 11 deletions

View File

@ -1,6 +1,7 @@
package conditions package conditions
import ( import (
gocontext "context"
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
@ -10,8 +11,6 @@ import (
"github.com/grafana/grafana/pkg/tsdb/legacydata/interval" "github.com/grafana/grafana/pkg/tsdb/legacydata/interval"
"github.com/grafana/grafana/pkg/tsdb/prometheus" "github.com/grafana/grafana/pkg/tsdb/prometheus"
gocontext "context"
"github.com/grafana/grafana-plugin-sdk-go/data" "github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/components/null" "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 // matches represents all the series that violate the alert condition
var matches []*alerting.EvalMatch var matches []*alerting.EvalMatch
// allMatches capture all evaluation matches irregardless on whether the condition is met or not // 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 { for _, series := range seriesList {
reducedValue := c.Reducer.Reduce(series) reducedValue := c.Reducer.Reduce(series)

View File

@ -144,6 +144,7 @@ func (gcn *GoogleChatNotifier) Notify(evalContext *alerting.EvalContext) error {
} }
// add a text paragraph widget for the fields // add a text paragraph widget for the fields
//nolint:prealloc // break block
var fields []textParagraphWidget var fields []textParagraphWidget
fieldLimitCount := 4 fieldLimitCount := 4
for index, evt := range evalContext.EvalMatches { for index, evt := range evalContext.EvalMatches {

View File

@ -110,7 +110,7 @@ func (auth *AuthProxy) IsAllowedIP(ip string) error {
} }
proxies := strings.Split(auth.cfg.AuthProxyWhitelist, ",") proxies := strings.Split(auth.cfg.AuthProxyWhitelist, ",")
var proxyObjs []*net.IPNet proxyObjs := make([]*net.IPNet, 0, len(proxies))
for _, proxy := range proxies { for _, proxy := range proxies {
result, err := coerceProxyAddress(proxy) result, err := coerceProxyAddress(proxy)
if err != nil { if err != nil {

View File

@ -118,7 +118,7 @@ func (out *RemoteWriteFrameOutput) sample(timeSeries []prompb.TimeSeries) []prom
sample.Samples = append(sample.Samples, filteredSamples...) sample.Samples = append(sample.Samples, filteredSamples...)
samples[name] = sample samples[name] = sample
} }
var toReturn []prompb.TimeSeries toReturn := make([]prompb.TimeSeries, 0, len(samples))
for _, ts := range samples { for _, ts := range samples {
toReturn = append(toReturn, ts) toReturn = append(toReturn, ts)
} }

View File

@ -309,7 +309,7 @@ func (f *StorageRuleBuilder) BuildRules(ctx context.Context, orgID int64) ([]*Li
return nil, err return nil, err
} }
var rules []*LiveChannelRule rules := make([]*LiveChannelRule, 0, len(channelRules))
for _, ruleConfig := range channelRules { for _, ruleConfig := range channelRules {
rule := &LiveChannelRule{ rule := &LiveChannelRule{

View File

@ -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) 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. // 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) activeReceivers := am.getActiveReceiversMap(am.route)
for name := range integrationsMap { for name := range integrationsMap {
stage := am.createReceiverStage(name, integrationsMap[name], am.waitFunc, am.notificationLog) 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. // 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) { 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 { for i, r := range receiver.GrafanaManagedReceivers {
n, err := am.buildReceiverIntegration(r, tmpl) n, err := am.buildReceiverIntegration(r, tmpl)
if err != nil { if err != nil {

View File

@ -203,10 +203,10 @@ func (am *Alertmanager) GetReceivers(ctx context.Context) []apimodels.Receiver {
am.reloadConfigMtx.RLock() am.reloadConfigMtx.RLock()
defer am.reloadConfigMtx.RUnlock() defer am.reloadConfigMtx.RUnlock()
var apiReceivers []apimodels.Receiver apiReceivers := make([]apimodels.Receiver, 0, len(am.receivers))
for _, rcv := range am.receivers { for _, rcv := range am.receivers {
// Build integrations slice for each receiver. // Build integrations slice for each receiver.
var integrations []*models.Integration integrations := make([]*models.Integration, 0, len(rcv.Integrations()))
for _, integration := range rcv.Integrations() { for _, integration := range rcv.Integrations() {
name := integration.Name() name := integration.Name()
sendResolved := integration.SendResolved() sendResolved := integration.SendResolved()

View File

@ -222,7 +222,6 @@ func (c *cache) getAll(orgID int64) []*State {
} }
func (c *cache) getStatesForRuleUID(orgID int64, alertRuleUID string) []*State { func (c *cache) getStatesForRuleUID(orgID int64, alertRuleUID string) []*State {
var result []*State
c.mtxStates.RLock() c.mtxStates.RLock()
defer c.mtxStates.RUnlock() defer c.mtxStates.RUnlock()
orgRules, ok := c.states[orgID] orgRules, ok := c.states[orgID]
@ -233,6 +232,7 @@ func (c *cache) getStatesForRuleUID(orgID int64, alertRuleUID string) []*State {
if !ok { if !ok {
return nil return nil
} }
result := make([]*State, 0, len(rs.states))
for _, state := range rs.states { for _, state := range rs.states {
result = append(result, state) result = append(result, state)
} }