Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go/vt/sqlparser: improve performance in TrackedBuffer formatting
Two minor things: * decently improve formatting integers (%d) * WriteArg calls buf.Grow to avoid potentially 2 allocations For integer formatting, buf.WriteString(fmt.Sprintf(...)) was about the worst way to do it. fmt.Sprintf itself allocates a new string, plus it's %d formatting is much more robust in handling padding and whatnot. First alternative for free win was using `fmt.Fprintf(&buf, ...)` instead, which simply avoids the extra allocation and just writes directly to the buffer. But strconv.Format{I,Ui}nt is quite fast, especially since it has a fast path for "small integers". Here's a trivial benchmark of all 3 options: ``` $ benchstat fmt.txt goos: darwin goarch: arm64 pkg: x │ fmt.txt │ │ sec/op │ FormatInt/Sprintf-10 55.51n ± 1% FormatInt/Fprintf-10 50.76n ± 1% FormatInt/FormatInt-10 17.16n ± 2% geomean 36.43n │ fmt.txt │ │ B/op │ FormatInt/Sprintf-10 16.00 ± 0% FormatInt/Fprintf-10 8.000 ± 0% FormatInt/FormatInt-10 8.000 ± 0% geomean 10.08 │ fmt.txt │ │ allocs/op │ FormatInt/Sprintf-10 2.000 ± 0% FormatInt/Fprintf-10 1.000 ± 0% FormatInt/FormatInt-10 1.000 ± 0% geomean 1.260 ``` So each %d within a format string is considerably faster. This obviously scales linearly with the number of %d's there are. Signed-off-by: Matt Robenolt <matt@ydekproductions.com>
- Loading branch information