Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/vt/sqlparser: improve performance in TrackedBuffer formatting #16364

Merged
merged 2 commits into from
Sep 16, 2024

Commits on Jul 10, 2024

  1. 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>
    mattrobenolt committed Jul 10, 2024
    Configuration menu
    Copy the full SHA
    4cc46b0 View commit details
    Browse the repository at this point in the history

Commits on Sep 16, 2024

  1. Merge remote-tracking branch 'upstream/main' into tracker-buffer-perf

    Signed-off-by: Manan Gupta <manan@planetscale.com>
    GuptaManan100 committed Sep 16, 2024
    Configuration menu
    Copy the full SHA
    57b4035 View commit details
    Browse the repository at this point in the history