Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf: Improve Performance, reduce heap allocations #1202

Merged
merged 20 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions internal/collections/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,18 @@ func NewCaseSensitiveKeyMap(variable variables.RuleVariable) *Map {
}

func (c *Map) Get(key string) []string {
if len(c.data) == 0 {
soujanyanmbri marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
if !c.isCaseSensitive {
key = strings.ToLower(key)
}
var values []string
for _, a := range c.data[key] {
values = append(values, a.value)
values := c.data[key]
if len(values) == 0 {
return nil
}
result := make([]string, 0, len(values))
for _, a := range values {
result = append(result, a.value)
}
return values
return result
}

// FindRegex returns all map elements whose key matches the regular expression.
Expand Down Expand Up @@ -122,14 +123,14 @@ func (c *Map) Add(key string, value string) {

// Set sets the value of a key with the array of strings passed. If the key already exists, it will be overwritten.
func (c *Map) Set(key string, values []string) {
originalKey := key
jptosso marked this conversation as resolved.
Show resolved Hide resolved
if !c.isCaseSensitive {
key = strings.ToLower(key)
}
c.data[key] = make([]keyValue, 0, len(values))
for _, v := range values {
c.data[key] = append(c.data[key], keyValue{key: originalKey, value: v})
dataSlice := make([]keyValue, len(values))
for i, v := range values {
dataSlice[i] = keyValue{key: key, value: v}
}
c.data[key] = dataSlice
}

// SetIndex sets the value of a key at the specified index. If the key already exists, it will be overwritten.
Expand Down
23 changes: 23 additions & 0 deletions internal/collections/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,26 @@ func TestNewCaseSensitiveKeyMap(t *testing.T) {
}

}

func BenchmarkTxSetGet(b *testing.B) {
c := NewCaseSensitiveKeyMap(variables.RequestHeaders)
b.Run("Set", func(b *testing.B) {
for i := 0; i < b.N; i++ {
key := fmt.Sprintf("key%d", i)
soujanyanmbri marked this conversation as resolved.
Show resolved Hide resolved
c.Set(key, []string{"value2"})
}
})
// Benchmark the Get operation
b.Run("Get", func(b *testing.B) {
for i := 0; i < b.N; i++ {
key := fmt.Sprintf("key%d", i)
c.Set(key, []string{"value2"})
}

for i := 0; i < b.N; i++ {
key := fmt.Sprintf("key%d", i)
c.Get(key)
}
})
b.ReportAllocs()
}
8 changes: 4 additions & 4 deletions internal/collections/named.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ func (c *NamedCollection) Len() int {

// Data is an internal method used for serializing to JSON
func (c *NamedCollection) Data() map[string][]string {
result := map[string][]string{}
result := make(map[string][]string, len(c.data))
for k, v := range c.data {
result[k] = make([]string, 0, len(v))
for _, a := range v {
result[k] = append(result[k], a.value)
result[k] = make([]string, len(v))
for i, a := range v {
result[k][i] = a.value
soujanyanmbri marked this conversation as resolved.
Show resolved Hide resolved
}
}
return result
Expand Down
12 changes: 6 additions & 6 deletions internal/corazarules/rule_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,27 @@ type MatchData struct {

var _ types.MatchData = (*MatchData)(nil)

func (m *MatchData) Variable() variables.RuleVariable {
func (m MatchData) Variable() variables.RuleVariable {
return m.Variable_
}

func (m *MatchData) Key() string {
func (m MatchData) Key() string {
return m.Key_
}

func (m *MatchData) Value() string {
func (m MatchData) Value() string {
return m.Value_
}

func (m *MatchData) Message() string {
func (m MatchData) Message() string {
return m.Message_
}

func (m *MatchData) Data() string {
func (m MatchData) Data() string {
return m.Data_
}

func (m *MatchData) ChainLevel() int {
func (m MatchData) ChainLevel() int {
return m.ChainLevel_
}

Expand Down
Loading