-
Notifications
You must be signed in to change notification settings - Fork 661
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce fmt.Sprintf allocations in query encoding (#2919)
* Pre-compute prefix when array is not flat * Switch to string concat for object keys * Change array key formatting and add comments about why sprintf is not used * Add changelog entry * Update .changelog/fd3c62c5-c1cf-48de-a223-ea0fdf4136c9.json --------- Co-authored-by: Luc Talatinian <[email protected]>
- Loading branch information
Showing
5 changed files
with
88 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"id": "fd3c62c5-c1cf-48de-a223-ea0fdf4136c9", | ||
"type": "feature", | ||
"description": "Reduce allocations in query encoding.", | ||
"modules": [ | ||
"." | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package query | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
var output string | ||
|
||
func Benchmark_sprintf_strings(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
output = fmt.Sprintf("%s.%s", "foo", "bar") | ||
} | ||
} | ||
|
||
func Benchmark_concat_strings(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
output = "foo" + keySeparator + "bar" | ||
} | ||
} | ||
|
||
func Benchmark_int_formatting(b *testing.B) { | ||
benchmarkFuncs := []struct { | ||
name string | ||
formatter func(val int32) | ||
}{ | ||
{ | ||
name: "array - sprintf", formatter: func(val int32) { | ||
output = fmt.Sprintf("%s.%d", "foo", val) | ||
}, | ||
}, | ||
{ | ||
name: "array - concat strconv", formatter: func(val int32) { | ||
output = "foo" + keySeparator + strconv.FormatInt(int64(val), 10) | ||
}, | ||
}, | ||
{ | ||
name: "map - sprintf", formatter: func(val int32) { | ||
output = fmt.Sprintf("%s.%d.%s", "foo", val, "bar") | ||
output = fmt.Sprintf("%s.%d.%s", "foo", val, "bar") | ||
}, | ||
}, | ||
{ | ||
name: "map - concat strconv", formatter: func(val int32) { | ||
valString := strconv.FormatInt(int64(val), 10) | ||
output = "foo" + keySeparator + valString + keySeparator + "bar" | ||
output = "foo" + keySeparator + valString + keySeparator + "bar" | ||
}, | ||
}, | ||
} | ||
|
||
sizesToTest := []int32{1, 10, 100, 250, 500, 1000} | ||
|
||
for _, bm := range benchmarkFuncs { | ||
for _, size := range sizesToTest { | ||
b.Run(fmt.Sprintf("%s with %d size", bm.name, size), func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
bm.formatter(size) | ||
} | ||
}) | ||
} | ||
} | ||
} |