Skip to content

Commit

Permalink
predicates/forwarded: use strings.Index to parse header (#3405)
Browse files Browse the repository at this point in the history
Use strings.Index instead of strings.Split to reduce memory allocations.

See #3403 for details.

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
  • Loading branch information
AlexanderYastrebov authored Feb 14, 2025
1 parent 6e5018e commit 87b289c
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions predicates/forwarded/forwarded.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,10 @@ type forwarded struct {
}

func parseForwarded(fh string) *forwarded {

f := &forwarded{}

for _, forwardedFull := range strings.Split(fh, ",") {
for _, forwardedPair := range strings.Split(strings.TrimSpace(forwardedFull), ";") {
for forwardedFull := range splitSeq(fh, ",") {
for forwardedPair := range splitSeq(strings.TrimSpace(forwardedFull), ";") {
token, value, found := strings.Cut(forwardedPair, "=")
value = strings.Trim(value, `"`)
if found && value != "" {
Expand All @@ -147,6 +146,23 @@ func parseForwarded(fh string) *forwarded {
}
}
}

return f
}

// TODO: use [strings.SplitSeq] added in go1.24 once go1.25 is released.
func splitSeq(s string, sep string) func(yield func(string) bool) {
return func(yield func(string) bool) {
for {
i := strings.Index(s, sep)
if i < 0 {
break
}
frag := s[:i]
if !yield(frag) {
return
}
s = s[i+len(sep):]
}
yield(s)
}
}

0 comments on commit 87b289c

Please sign in to comment.