Perfomance: Preallocate slices (#61580)
This commit is contained in:
@@ -403,7 +403,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 *alerting.Template) ([]*alerting.Integration, error) {
|
||||
var integrations []*alerting.Integration
|
||||
integrations := make([]*alerting.Integration, 0, len(receiver.GrafanaManagedReceivers))
|
||||
for i, r := range receiver.GrafanaManagedReceivers {
|
||||
n, err := am.buildReceiverIntegration(r, tmpl)
|
||||
if err != nil {
|
||||
|
||||
@@ -130,7 +130,7 @@ 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.Base.GetReceivers()))
|
||||
for _, rcv := range am.Base.GetReceivers() {
|
||||
// Build integrations slice for each receiver.
|
||||
integrations := make([]*models.Integration, 0, len(rcv.Integrations()))
|
||||
|
||||
@@ -184,7 +184,7 @@ func (st *Manager) ResetStateByRuleUID(ctx context.Context, ruleKey ngModels.Ale
|
||||
func (st *Manager) ProcessEvalResults(ctx context.Context, evaluatedAt time.Time, alertRule *ngModels.AlertRule, results eval.Results, extraLabels data.Labels) []StateTransition {
|
||||
logger := st.log.FromContext(ctx)
|
||||
logger.Debug("State manager processing evaluation results", "resultCount", len(results))
|
||||
var states []StateTransition
|
||||
states := make([]StateTransition, 0, len(results))
|
||||
|
||||
for _, result := range results {
|
||||
s := st.setNextState(ctx, alertRule, result, extraLabels, logger)
|
||||
@@ -383,10 +383,10 @@ func (st *Manager) deleteStaleStatesFromCache(ctx context.Context, logger log.Lo
|
||||
// TODO: We will need to change this when we support images without screenshots as each series will have a different image
|
||||
var resolvedImage *ngModels.Image
|
||||
|
||||
var resolvedStates []StateTransition
|
||||
staleStates := st.cache.deleteRuleStates(alertRule.GetKey(), func(s *State) bool {
|
||||
return stateIsStale(evaluatedAt, s.LastEvaluationTime, alertRule.IntervalSeconds)
|
||||
})
|
||||
resolvedStates := make([]StateTransition, 0, len(staleStates))
|
||||
|
||||
for _, s := range staleStates {
|
||||
logger.Info("Detected stale state entry", "cacheID", s.CacheID, "state", s.State, "reason", s.StateReason)
|
||||
|
||||
@@ -58,7 +58,9 @@ func CalculateChanges(ctx context.Context, ruleReader RuleReader, groupKey model
|
||||
existingGroupRulesUIDs[r.UID] = r
|
||||
}
|
||||
|
||||
var toAdd, toDelete []*models.AlertRule
|
||||
//nolint:prealloc // difficult logic
|
||||
var toAdd []*models.AlertRule
|
||||
//nolint:prealloc // difficult logic
|
||||
var toUpdate []RuleDelta
|
||||
loadedRulesByUID := map[string]*models.AlertRule{} // auxiliary cache to avoid unnecessary queries if there are multiple moves from the same group
|
||||
for _, r := range submittedRules {
|
||||
@@ -110,6 +112,7 @@ func CalculateChanges(ctx context.Context, ruleReader RuleReader, groupKey model
|
||||
continue
|
||||
}
|
||||
|
||||
toDelete := make([]*models.AlertRule, 0, len(existingGroupRulesUIDs))
|
||||
for _, rule := range existingGroupRulesUIDs {
|
||||
toDelete = append(toDelete, rule)
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ func (s *Service) GetPluginSettings(ctx context.Context, args *pluginsettings.Ge
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []*pluginsettings.InfoDTO
|
||||
result := make([]*pluginsettings.InfoDTO, 0, len(ps))
|
||||
for _, p := range ps {
|
||||
result = append(result, &pluginsettings.InfoDTO{
|
||||
OrgID: p.OrgID,
|
||||
|
||||
@@ -255,8 +255,8 @@ func transformInterface(i interface{}) (interface{}, interface{}, error) {
|
||||
}
|
||||
|
||||
func transformSlice(i []interface{}) (interface{}, interface{}, error) {
|
||||
var transformedSlice []interface{}
|
||||
var rawSlice []interface{}
|
||||
transformedSlice := make([]interface{}, 0, len(i))
|
||||
rawSlice := make([]interface{}, 0, len(i))
|
||||
for _, val := range i {
|
||||
transformed, raw, err := transformInterface(val)
|
||||
if err != nil {
|
||||
|
||||
@@ -56,7 +56,7 @@ func getDatasourcePluginSlugs(baseUrl string) ([]string, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var slugs []string
|
||||
slugs := make([]string, 0, len(res.Items))
|
||||
for _, meta := range res.Items {
|
||||
slugs = append(slugs, meta.Slug)
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ func (pd *PublicDashboardServiceImpl) FindAnnotations(ctx context.Context, reqDT
|
||||
}
|
||||
}
|
||||
|
||||
var results []models.AnnotationEvent
|
||||
results := make([]models.AnnotationEvent, 0, len(uniqueEvents))
|
||||
for _, result := range uniqueEvents {
|
||||
results = append(results, result)
|
||||
}
|
||||
|
||||
@@ -100,7 +100,7 @@ func (s *StandardSearchService) createAllowedActions(ctx context.Context, orgId
|
||||
dsActionsByUid = make(map[string][]string)
|
||||
}
|
||||
|
||||
var out [][]allowedActions
|
||||
out := make([][]allowedActions, 0, len(references))
|
||||
for _, ref := range references {
|
||||
var actions []allowedActions
|
||||
|
||||
|
||||
@@ -776,13 +776,12 @@ func (i *searchIndex) updateDashboard(ctx context.Context, orgID int64, index *o
|
||||
return err
|
||||
}
|
||||
|
||||
var actualPanelIDs []string
|
||||
|
||||
if location != "" {
|
||||
location += "/"
|
||||
}
|
||||
location += dash.uid
|
||||
panelDocs := getDashboardPanelDocs(dash, location)
|
||||
actualPanelIDs := make([]string, 0, len(panelDocs))
|
||||
for _, panelDoc := range panelDocs {
|
||||
actualPanelIDs = append(actualPanelIDs, string(panelDoc.ID().Term()))
|
||||
batch.Update(panelDoc.ID(), panelDoc)
|
||||
|
||||
@@ -190,7 +190,7 @@ func (kv *SecretsKVStorePlugin) WithFallbackEnabled(fn func() error) error {
|
||||
}
|
||||
|
||||
func parseKeys(keys []*smp.Key) []Key {
|
||||
var newKeys []Key
|
||||
newKeys := make([]Key, 0, len(keys))
|
||||
|
||||
for _, k := range keys {
|
||||
newKey := Key{OrgId: k.OrgId, Namespace: k.Namespace, Type: k.Type}
|
||||
@@ -201,7 +201,7 @@ func parseKeys(keys []*smp.Key) []Key {
|
||||
}
|
||||
|
||||
func parseItems(items []*smp.Item) []Item {
|
||||
var newItems []Item
|
||||
newItems := make([]Item, 0, len(items))
|
||||
|
||||
for _, i := range items {
|
||||
newItem := Item{OrgId: &i.Key.OrgId, Namespace: &i.Key.Namespace, Type: &i.Key.Type, Value: i.Value}
|
||||
|
||||
@@ -54,7 +54,8 @@ func (m *actionNameMigrator) migrateActionNames() error {
|
||||
"alert.rules:update": accesscontrol.ActionAlertingRuleUpdate,
|
||||
}
|
||||
|
||||
var oldActionNames, newActionNames []interface{}
|
||||
oldActionNames := make([]interface{}, 0, len(actionNameMapping))
|
||||
newActionNames := make([]interface{}, 0, len(actionNameMapping))
|
||||
for oldName, newName := range actionNameMapping {
|
||||
oldActionNames = append(oldActionNames, oldName)
|
||||
newActionNames = append(newActionNames, newName)
|
||||
|
||||
@@ -47,7 +47,7 @@ func (p *teamPermissionMigrator) setRolePermissions(roleID int64, permissions []
|
||||
}
|
||||
|
||||
// Then insert new permissions
|
||||
var newPermissions []accesscontrol.Permission
|
||||
newPermissions := make([]accesscontrol.Permission, 0, len(permissions))
|
||||
now := time.Now()
|
||||
for _, permission := range permissions {
|
||||
permission.RoleID = roleID
|
||||
|
||||
Reference in New Issue
Block a user