From 302d0b1ad46306b378d81485e8c0b1c4887ffd0d Mon Sep 17 00:00:00 2001 From: Varakh Date: Sun, 2 Jun 2024 23:13:44 +0200 Subject: [PATCH] Use capacity to avoid arr copying #noissue --- server/api_handler_action.go | 2 +- server/api_handler_action_invocation.go | 2 +- server/api_handler_event.go | 2 +- server/api_handler_secret.go | 2 +- server/api_handler_update.go | 2 +- server/api_handler_webhook.go | 2 +- server/repository_action_invocation.go | 4 ++-- server/repository_event.go | 2 +- server/repository_update.go | 10 +++++----- server/service_action_invocation.go | 3 ++- 10 files changed, 16 insertions(+), 15 deletions(-) diff --git a/server/api_handler_action.go b/server/api_handler_action.go index 0125d62..7f5824a 100644 --- a/server/api_handler_action.go +++ b/server/api_handler_action.go @@ -29,7 +29,7 @@ func (h *actionHandler) paginate(c *gin.Context) { } var data []*api.ActionResponse - data = make([]*api.ActionResponse, 0) + data = make([]*api.ActionResponse, 0, len(actions)) for _, e := range actions { data = append(data, &api.ActionResponse{ diff --git a/server/api_handler_action_invocation.go b/server/api_handler_action_invocation.go index fbf5a3d..fd96c83 100644 --- a/server/api_handler_action_invocation.go +++ b/server/api_handler_action_invocation.go @@ -56,7 +56,7 @@ func (h *actionInvocationHandler) paginate(c *gin.Context) { } var data []*api.ActionInvocationResponse - data = make([]*api.ActionInvocationResponse, 0) + data = make([]*api.ActionInvocationResponse, 0, len(actionInvocations)) for _, e := range actionInvocations { data = append(data, &api.ActionInvocationResponse{ diff --git a/server/api_handler_event.go b/server/api_handler_event.go index ab40562..43aa472 100644 --- a/server/api_handler_event.go +++ b/server/api_handler_event.go @@ -29,7 +29,7 @@ func (h *eventHandler) window(c *gin.Context) { } var data []*api.EventResponse - data = make([]*api.EventResponse, 0) + data = make([]*api.EventResponse, 0, len(events)) for _, e := range events { data = append(data, &api.EventResponse{ diff --git a/server/api_handler_secret.go b/server/api_handler_secret.go index 51910ca..c8525fa 100644 --- a/server/api_handler_secret.go +++ b/server/api_handler_secret.go @@ -24,7 +24,7 @@ func (h *secretHandler) getAll(c *gin.Context) { } var data []*api.SecretResponse - data = make([]*api.SecretResponse, 0) + data = make([]*api.SecretResponse, 0, len(secrets)) for _, e := range secrets { data = append(data, &api.SecretResponse{ diff --git a/server/api_handler_update.go b/server/api_handler_update.go index fd0987c..e444878 100644 --- a/server/api_handler_update.go +++ b/server/api_handler_update.go @@ -28,7 +28,7 @@ func (h *updateHandler) paginate(c *gin.Context) { s, stateQueryContainsAtLeastOne := c.GetQueryArray("state") - var states []api.UpdateState + states := make([]api.UpdateState, 0) if stateQueryContainsAtLeastOne { for _, state := range s { states = append(states, api.UpdateState(state)) diff --git a/server/api_handler_webhook.go b/server/api_handler_webhook.go index 9cd158b..316829a 100644 --- a/server/api_handler_webhook.go +++ b/server/api_handler_webhook.go @@ -29,7 +29,7 @@ func (h *webhookHandler) paginate(c *gin.Context) { } var data []*api.WebhookResponse - data = make([]*api.WebhookResponse, 0) + data = make([]*api.WebhookResponse, 0, len(webhooks)) for _, e := range webhooks { data = append(data, &api.WebhookResponse{ diff --git a/server/repository_action_invocation.go b/server/repository_action_invocation.go index e07496e..d93b035 100644 --- a/server/repository_action_invocation.go +++ b/server/repository_action_invocation.go @@ -225,8 +225,8 @@ func (r *actionInvocationDbRepo) deleteByUpdatedAtBeforeAndStates(time time.Time } func translateActionInvocationState(state ...api.ActionInvocationState) []string { - states := make([]string, 0) - if len(state) > 0 { + states := make([]string, 0, len(state)) + if len(states) > 0 { for _, s := range state { states = append(states, s.Value()) } diff --git a/server/repository_event.go b/server/repository_event.go index 66842d6..8f717fb 100644 --- a/server/repository_event.go +++ b/server/repository_event.go @@ -125,7 +125,7 @@ func (r *eventDbRepo) deleteByUpdatedAtBeforeAndStates(time time.Time, state ... return 0, errorValidationNotEmpty } - states := make([]string, 0) + states := make([]string, 0, len(state)) for _, i := range state { states = append(states, i.Value()) } diff --git a/server/repository_update.go b/server/repository_update.go index 2749809..8c7fcfe 100644 --- a/server/repository_update.go +++ b/server/repository_update.go @@ -199,7 +199,7 @@ func (r *updateDbRepo) deleteByUpdatedAtBeforeAndStates(time time.Time, state .. return 0, errorValidationNotEmpty } - states := make([]string, 0) + states := make([]string, 0, len(state)) for _, i := range state { states = append(states, i.Value()) } @@ -231,8 +231,8 @@ func (r *updateDbRepo) paginate(page int, pageSize int, orderBy string, order st order = "desc" } - states := make([]string, 0) - if len(state) > 0 { + states := make([]string, 0, len(state)) + if len(states) > 0 { for _, s := range state { states = append(states, s.Value()) } @@ -248,8 +248,8 @@ func (r *updateDbRepo) paginate(page int, pageSize int, orderBy string, order st func (r *updateDbRepo) count(searchTerm string, searchIn string, state ...api.UpdateState) (int64, error) { var c int64 - states := make([]string, 0) - if len(state) > 0 { + states := make([]string, 0, len(state)) + if len(states) > 0 { for _, s := range state { states = append(states, s.Value()) } diff --git a/server/service_action_invocation.go b/server/service_action_invocation.go index 5d53268..013290b 100644 --- a/server/service_action_invocation.go +++ b/server/service_action_invocation.go @@ -65,7 +65,8 @@ func (s *actionInvocationService) enqueueFromEvent(event *Event, actions []*Acti return err } - var filteredActions []*Action + filteredActions := make([]*Action, 0) + for _, action := range actions { matchesEvent := action.MatchEvent == nil || *action.MatchEvent == event.Name matchesHost := action.MatchHost == nil || *action.MatchHost == eventPayload.Host