From 0d1ade4626e627ea9737ed7a3659dccd0204b38a Mon Sep 17 00:00:00 2001 From: Kashiwa <13825170+ksw2000@users.noreply.github.com> Date: Mon, 24 Feb 2025 15:13:47 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor:=20Improve=20Perf?= =?UTF-8?q?ormance=20of=20getSplicedStrList=20(#3318)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ♻️ 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 --- helpers.go | 33 ++++++++++++++------------------- helpers_test.go | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/helpers.go b/helpers.go index eea6b370..18728e56 100644 --- a/helpers.go +++ b/helpers.go @@ -321,28 +321,23 @@ func getSplicedStrList(headerValue string, dst []string) []string { return nil } - var ( - index int - character rune - lastElementEndsAt int - insertIndex int - ) - for index, character = range headerValue + "$" { - if character == ',' || index == len(headerValue) { - if insertIndex >= len(dst) { - oldSlice := dst - dst = make([]string, len(dst)+(len(dst)>>1)+2) - copy(dst, oldSlice) - } - dst[insertIndex] = utils.TrimLeft(headerValue[lastElementEndsAt:index], ' ') - lastElementEndsAt = index + 1 - insertIndex++ + dst = dst[:0] + segmentStart := 0 + isLeadingSpace := true + for i, c := range headerValue { + switch { + case c == ',': + dst = append(dst, headerValue[segmentStart:i]) + segmentStart = i + 1 + isLeadingSpace = true + case c == ' ' && isLeadingSpace: + segmentStart = i + 1 + default: + isLeadingSpace = false } } + dst = append(dst, headerValue[segmentStart:]) - if len(dst) > insertIndex { - dst = dst[:insertIndex] - } return dst } diff --git a/helpers_test.go b/helpers_test.go index 75698d87..5664bc48 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -303,6 +303,26 @@ func Test_Utils_GetSplicedStrList(t *testing.T) { headerValue: "gzip,", expectedList: []string{"gzip", ""}, }, + { + description: "has a space between words", + headerValue: " foo bar, hello world", + expectedList: []string{"foo bar", "hello world"}, + }, + { + description: "single comma", + headerValue: ",", + expectedList: []string{"", ""}, + }, + { + description: "multiple comma", + headerValue: ",,", + expectedList: []string{"", "", ""}, + }, + { + description: "comma with space", + headerValue: ", ,", + expectedList: []string{"", "", ""}, + }, } for _, tc := range testCases {