From de409e378b912233f7a89a1e09b92d67f2b03f8d Mon Sep 17 00:00:00 2001 From: Matteo Pace Date: Tue, 5 Nov 2024 15:27:32 +0000 Subject: [PATCH 1/2] perf: GetField reduce allocations (#1195) reuse slice --- internal/corazawaf/transaction.go | 11 ++++++----- internal/corazawaf/transaction_test.go | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/internal/corazawaf/transaction.go b/internal/corazawaf/transaction.go index 7fce408f3..1a4785b90 100644 --- a/internal/corazawaf/transaction.go +++ b/internal/corazawaf/transaction.go @@ -587,9 +587,9 @@ func (tx *Transaction) GetField(rv ruleVariableParams) []types.MatchData { } // in the most common scenario filteredMatches length will be - // the same as matches length, so we avoid allocating per result - filteredMatches := make([]types.MatchData, 0, len(matches)) - + // the same as matches length, so we avoid allocating per result. + // We reuse the matches slice to store filtered results avoiding extra allocation. + filteredCount := 0 for _, c := range matches { isException := false lkey := strings.ToLower(c.Key()) @@ -600,10 +600,11 @@ func (tx *Transaction) GetField(rv ruleVariableParams) []types.MatchData { } } if !isException { - filteredMatches = append(filteredMatches, c) + matches[filteredCount] = c + filteredCount++ } } - matches = filteredMatches + matches = matches[:filteredCount] if rv.Count { count := len(matches) diff --git a/internal/corazawaf/transaction_test.go b/internal/corazawaf/transaction_test.go index 0bb0c4c65..64d548546 100644 --- a/internal/corazawaf/transaction_test.go +++ b/internal/corazawaf/transaction_test.go @@ -1291,6 +1291,20 @@ func TestTxGetField(t *testing.T) { } } +func BenchmarkTxGetField(b *testing.B) { + tx := makeTransaction(b) + rvp := ruleVariableParams{ + Variable: variables.Args, + } + for i := 0; i < b.N; i++ { + tx.GetField(rvp) + } + if err := tx.Close(); err != nil { + b.Fatalf("Failed to close transaction: %s", err.Error()) + } + b.ReportAllocs() +} + func TestTxProcessURI(t *testing.T) { waf := NewWAF() tx := waf.NewTransaction() From 5fbd62a876518b159f5093326aabed5d6d9e3b35 Mon Sep 17 00:00:00 2001 From: Matteo Pace Date: Thu, 7 Nov 2024 09:50:52 +0000 Subject: [PATCH 2/2] docs: nits and avoids mentioning not existing resources (#1203) nits:doc --- internal/corazawaf/transaction.go | 3 +-- internal/seclang/directives.go | 3 --- types/transaction.go | 1 - 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/internal/corazawaf/transaction.go b/internal/corazawaf/transaction.go index 1a4785b90..2fe3585c8 100644 --- a/internal/corazawaf/transaction.go +++ b/internal/corazawaf/transaction.go @@ -1292,8 +1292,7 @@ func (tx *Transaction) ProcessResponseBody() (*types.Interruption, error) { return tx.interruption, nil } -// ProcessLogging Logging all information relative to this transaction. -// An error log +// ProcessLogging logs all information relative to this transaction. // At this point there is not need to hold the connection, the response can be // delivered prior to the execution of this method. func (tx *Transaction) ProcessLogging() { diff --git a/internal/seclang/directives.go b/internal/seclang/directives.go index 7d02b89d8..30540da32 100644 --- a/internal/seclang/directives.go +++ b/internal/seclang/directives.go @@ -774,9 +774,6 @@ func directiveSecAuditLogRelevantStatus(options *DirectiveOptions) error { // Syntax: SecAuditLogParts [PARTLETTERS] // Default: ABCFHZ // --- -// The format of the audit log format is documented in detail in the Audit Log Data -// Format Documentation. -// // Example: // ```apache // SecAuditLogParts ABCFHZ diff --git a/types/transaction.go b/types/transaction.go index 97c7d7ce0..b16448a20 100644 --- a/types/transaction.go +++ b/types/transaction.go @@ -148,7 +148,6 @@ type Transaction interface { ReadResponseBodyFrom(io.Reader) (*Interruption, int, error) // ProcessLogging Logging all information relative to this transaction. - // An error log // At this point there is not need to hold the connection, the response can be // delivered prior to the execution of this method. ProcessLogging()