MM-21368: Optimize MapStringsToQueryParams (#13601)

- Use strings.Builder which minimizes allocations.
- Avoid fmt.Sprintf which is slower than simple string concat.

Results:

name                                      old time/op    new time/op    delta
MapStringsToQueryParams/one_item-8           512ns ± 2%     377ns ± 1%  -26.27%  (p=0.000 n=10+9)
MapStringsToQueryParams/multiple_items-8     858ns ± 5%     748ns ± 2%  -12.82%  (p=0.000 n=10+9)

name                                      old alloc/op   new alloc/op   delta
MapStringsToQueryParams/one_item-8            456B ± 0%      384B ± 0%  -15.79%  (p=0.000 n=10+9)
MapStringsToQueryParams/multiple_items-8      608B ± 0%      592B ± 0%   -2.63%  (p=0.000 n=10+10)

name                                      old allocs/op  new allocs/op  delta
MapStringsToQueryParams/one_item-8            8.00 ± 0%      6.00 ± 0%  -25.00%  (p=0.000 n=10+10)
MapStringsToQueryParams/multiple_items-8      12.0 ± 0%      12.0 ± 0%     ~     (all equal)
Этот коммит содержится в:
Agniva De Sarker
2020-01-09 21:14:01 +05:30
коммит произвёл Christopher Speller
родитель 62b57143c8
Коммит 2dba5dbf86
2 изменённых файлов: 20 добавлений и 4 удалений

Просмотреть файл

@@ -33,6 +33,24 @@ func TestMapStringsToQueryParams(t *testing.T) {
})
}
var keys string
var params map[string]interface{}
func BenchmarkMapStringsToQueryParams(b *testing.B) {
b.Run("one item", func(b *testing.B) {
input := []string{"apple"}
for i := 0; i < b.N; i++ {
keys, params = MapStringsToQueryParams(input, "Fruit")
}
})
b.Run("multiple items", func(b *testing.B) {
input := []string{"carrot", "tomato", "potato"}
for i := 0; i < b.N; i++ {
keys, params = MapStringsToQueryParams(input, "Vegetable")
}
})
}
func TestSanitizeSearchTerm(t *testing.T) {
term := "test"
result := sanitizeSearchTerm(term, "\\")