From 03524dbc79125d009b702522bae55e4a966fe6a5 Mon Sep 17 00:00:00 2001 From: tyltr Date: Thu, 21 Apr 2022 17:15:59 +0800 Subject: [PATCH 1/4] opotimize --- args.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/args.go b/args.go index 844d9318f2..863a5e57b5 100644 --- a/args.go +++ b/args.go @@ -544,13 +544,22 @@ func (s *argsScanner) next(kv *argsKV) bool { } func decodeArgAppend(dst, src []byte) []byte { - if bytes.IndexByte(src, '%') < 0 && bytes.IndexByte(src, '+') < 0 { + idxPercent := bytes.IndexByte(src, '%') + idxPlus := bytes.IndexByte(src, '+') + if idxPercent < 0 && idxPlus < 0 { // fast path: src doesn't contain encoded chars return append(dst, src...) } + idx := min(idxPercent, idxPlus) + if idx > 0 { + dst := append(dst, src[:idx]...) + } else { + idx = 0 + } + // slow path - for i := 0; i < len(src); i++ { + for i := idx; i < len(src); i++ { c := src[i] if c == '%' { if i+2 >= len(src) { @@ -573,6 +582,13 @@ func decodeArgAppend(dst, src []byte) []byte { return dst } +func min(a, b int) int { + if a <= b { + return a + } + return b +} + // decodeArgAppendNoPlus is almost identical to decodeArgAppend, but it doesn't // substitute '+' with ' '. // From 6de13dbeb55506732b0726a3766fee0d668c90cc Mon Sep 17 00:00:00 2001 From: tyltr Date: Thu, 21 Apr 2022 17:29:29 +0800 Subject: [PATCH 2/4] lint --- args.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/args.go b/args.go index 863a5e57b5..d5c30b6f5e 100644 --- a/args.go +++ b/args.go @@ -553,7 +553,7 @@ func decodeArgAppend(dst, src []byte) []byte { idx := min(idxPercent, idxPlus) if idx > 0 { - dst := append(dst, src[:idx]...) + dst = append(dst, src[:idx]...) } else { idx = 0 } From 3fc02e511bb45e1f0eb08d300d7153a96e4ea35d Mon Sep 17 00:00:00 2001 From: tyltr Date: Sun, 24 Apr 2022 17:30:36 +0800 Subject: [PATCH 3/4] without min --- args.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/args.go b/args.go index d5c30b6f5e..db76f6b8b0 100644 --- a/args.go +++ b/args.go @@ -551,12 +551,21 @@ func decodeArgAppend(dst, src []byte) []byte { return append(dst, src...) } - idx := min(idxPercent, idxPlus) - if idx > 0 { - dst = append(dst, src[:idx]...) - } else { - idx = 0 + idx := 0 + if idxPercent < 0 { + idx = idxPlus + } + if idxPlus < 0 { + idx = idxPercent } + if idxPercent > 0 && idxPlus > 0 { + if idxPercent > idxPlus { + idx = idxPlus + } else { + idx = idxPercent + } + } + dst = append(dst, src[:idx]...) // slow path for i := idx; i < len(src); i++ { @@ -582,13 +591,6 @@ func decodeArgAppend(dst, src []byte) []byte { return dst } -func min(a, b int) int { - if a <= b { - return a - } - return b -} - // decodeArgAppendNoPlus is almost identical to decodeArgAppend, but it doesn't // substitute '+' with ' '. // From 71dfa60e8891c74b7ce6554ad5938e84f7273dfa Mon Sep 17 00:00:00 2001 From: tyltr Date: Sun, 24 Apr 2022 20:55:21 +0800 Subject: [PATCH 4/4] less comparisons --- args.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/args.go b/args.go index db76f6b8b0..dc10ea1c48 100644 --- a/args.go +++ b/args.go @@ -546,25 +546,22 @@ func (s *argsScanner) next(kv *argsKV) bool { func decodeArgAppend(dst, src []byte) []byte { idxPercent := bytes.IndexByte(src, '%') idxPlus := bytes.IndexByte(src, '+') - if idxPercent < 0 && idxPlus < 0 { + if idxPercent == -1 && idxPlus == -1 { // fast path: src doesn't contain encoded chars return append(dst, src...) } idx := 0 - if idxPercent < 0 { + if idxPercent == -1 { idx = idxPlus - } - if idxPlus < 0 { + } else if idxPlus == -1 { + idx = idxPercent + } else if idxPercent > idxPlus { + idx = idxPlus + } else { idx = idxPercent } - if idxPercent > 0 && idxPlus > 0 { - if idxPercent > idxPlus { - idx = idxPlus - } else { - idx = idxPercent - } - } + dst = append(dst, src[:idx]...) // slow path