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 all 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
26 changes: 18 additions & 8 deletions internal/collections/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ func (c *Map) Get(key string) []string {
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, len(values))
for i, v := range values {
result[i] = v.value
}
return values
return result
}

// FindRegex returns all map elements whose key matches the regular expression.
Expand Down Expand Up @@ -120,16 +124,22 @@ func (c *Map) Add(key string, value string) {
c.data[key] = append(c.data[key], aVal)
}

// Set sets the value of a key with the array of strings passed. If the key already exists, it will be overwritten.
// 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, exists := c.data[key]
if !exists || cap(dataSlice) < len(values) {
dataSlice = make([]keyValue, len(values))
} else {
dataSlice = dataSlice[:len(values)] // Reuse existing slice with the same length
}
for i, v := range values {
dataSlice[i] = keyValue{key: originalKey, 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
22 changes: 22 additions & 0 deletions internal/collections/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,25 @@ func TestNewCaseSensitiveKeyMap(t *testing.T) {
}

}

func BenchmarkTxSetGet(b *testing.B) {
keys := make(map[int]string, b.N)
for i := 0; i < b.N; i++ {
keys[i] = fmt.Sprintf("key%d", i)
}
c := NewCaseSensitiveKeyMap(variables.RequestHeaders)

b.Run("Set", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Set(keys[i], []string{"value2"})
}
})
b.Run("Get", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Get(keys[i])
}
})
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