Skip to content

Commit 0d1ade4

Browse files
authored
♻️ Refactor: Improve Performance of getSplicedStrList (gofiber#3318)
* ♻️ Refactor: improve performance of getSplicedStrList goos: linux goarch: amd64 pkg: github.com/gofiber/fiber/v3 cpu: AMD EPYC 7763 64-Core Processor │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ _Utils_GetSplicedStrList-4 66.12n ± 1% 51.05n ± 1% -22.79% (p=0.000 n=50) │ old.txt │ new.txt │ │ B/op │ B/op vs base │ _Utils_GetSplicedStrList-4 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=50) ¹ ¹ all samples are equal │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ _Utils_GetSplicedStrList-4 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=50) ¹ ¹ all samples are equal * 🚨 Test: add more test for getSplicedStrList * 🩹 Fix: golangci-lint ifElseChain * ♻️ Refactor: use more descriptive variable names
1 parent ef4effc commit 0d1ade4

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

helpers.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -321,28 +321,23 @@ func getSplicedStrList(headerValue string, dst []string) []string {
321321
return nil
322322
}
323323

324-
var (
325-
index int
326-
character rune
327-
lastElementEndsAt int
328-
insertIndex int
329-
)
330-
for index, character = range headerValue + "$" {
331-
if character == ',' || index == len(headerValue) {
332-
if insertIndex >= len(dst) {
333-
oldSlice := dst
334-
dst = make([]string, len(dst)+(len(dst)>>1)+2)
335-
copy(dst, oldSlice)
336-
}
337-
dst[insertIndex] = utils.TrimLeft(headerValue[lastElementEndsAt:index], ' ')
338-
lastElementEndsAt = index + 1
339-
insertIndex++
324+
dst = dst[:0]
325+
segmentStart := 0
326+
isLeadingSpace := true
327+
for i, c := range headerValue {
328+
switch {
329+
case c == ',':
330+
dst = append(dst, headerValue[segmentStart:i])
331+
segmentStart = i + 1
332+
isLeadingSpace = true
333+
case c == ' ' && isLeadingSpace:
334+
segmentStart = i + 1
335+
default:
336+
isLeadingSpace = false
340337
}
341338
}
339+
dst = append(dst, headerValue[segmentStart:])
342340

343-
if len(dst) > insertIndex {
344-
dst = dst[:insertIndex]
345-
}
346341
return dst
347342
}
348343

helpers_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,26 @@ func Test_Utils_GetSplicedStrList(t *testing.T) {
303303
headerValue: "gzip,",
304304
expectedList: []string{"gzip", ""},
305305
},
306+
{
307+
description: "has a space between words",
308+
headerValue: " foo bar, hello world",
309+
expectedList: []string{"foo bar", "hello world"},
310+
},
311+
{
312+
description: "single comma",
313+
headerValue: ",",
314+
expectedList: []string{"", ""},
315+
},
316+
{
317+
description: "multiple comma",
318+
headerValue: ",,",
319+
expectedList: []string{"", "", ""},
320+
},
321+
{
322+
description: "comma with space",
323+
headerValue: ", ,",
324+
expectedList: []string{"", "", ""},
325+
},
306326
}
307327

308328
for _, tc := range testCases {

0 commit comments

Comments
 (0)