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

protobuf: don't serialize zero-valued std duration #137479

Merged

Conversation

nvanbenschoten
Copy link
Member

@nvanbenschoten nvanbenschoten commented Dec 14, 2024

Picks up cockroachdb/gogoproto#10.

This commit updates the serialization of time.Duration (see gogo.stdduration) to avoid serializing the default (zero) value in proto3. In Go, time.Duration is a typedef of int64, so it was surprising that unlike int64, the zero value of time.Duration fields were being put on the wire.

While here, we also fix DurationFromProto to not cause its input to escape to the heap by passing it through fmt.Errorf. This function is called from StdDurationUnmarshal and its unintentional heap allocation what alerted me to the zero-value serialization issue.

Performance improvement on sysbench microbenchmarks:

name                                            old time/op    new time/op    delta
Sysbench/SQL/1node_remote/oltp_read_only-10       3.09ms ±12%    3.08ms ± 9%    ~     (p=0.905 n=9+10)
Sysbench/SQL/1node_remote/oltp_write_only-10      1.97ms ± 4%    1.96ms ± 3%    ~     (p=0.278 n=9+10)
Sysbench/SQL/1node_remote/oltp_read_write-10      5.73ms ± 1%    5.73ms ± 1%    ~     (p=1.000 n=7+8)
Sysbench/SQL/1node_remote/oltp_point_select-10     180µs ± 6%     181µs ±14%    ~     (p=0.853 n=10+10)
Sysbench/KV/1node_remote/oltp_read_only-10         736µs ± 3%     729µs ± 3%    ~     (p=0.190 n=10+10)
Sysbench/KV/1node_remote/oltp_write_only-10        594µs ± 2%     587µs ± 4%    ~     (p=0.211 n=9+10)
Sysbench/KV/1node_remote/oltp_read_write-10       1.44ms ± 6%    1.42ms ± 3%    ~     (p=0.156 n=10+9)
Sysbench/KV/1node_remote/oltp_point_select-10     33.7µs ±11%    34.9µs ± 9%    ~     (p=0.247 n=10+10)

name                                            old alloc/op   new alloc/op   delta
Sysbench/KV/1node_remote/oltp_point_select-10     6.69kB ± 1%    6.61kB ± 1%  -1.18%  (p=0.001 n=10+9)
Sysbench/SQL/1node_remote/oltp_point_select-10    30.0kB ± 0%    29.7kB ± 1%  -0.87%  (p=0.000 n=8+10)
Sysbench/SQL/1node_remote/oltp_write_only-10       476kB ± 0%     474kB ± 0%  -0.61%  (p=0.000 n=10+9)
Sysbench/KV/1node_remote/oltp_write_only-10        230kB ± 0%     229kB ± 0%  -0.53%  (p=0.000 n=10+10)
Sysbench/SQL/1node_remote/oltp_read_write-10      1.62MB ± 0%    1.62MB ± 0%  -0.34%  (p=0.000 n=10+10)
Sysbench/KV/1node_remote/oltp_read_only-10         647kB ± 0%     645kB ± 0%  -0.25%  (p=0.000 n=10+10)
Sysbench/KV/1node_remote/oltp_read_write-10        872kB ± 0%     870kB ± 0%  -0.25%  (p=0.000 n=10+9)
Sysbench/SQL/1node_remote/oltp_read_only-10       1.15MB ± 0%    1.15MB ± 0%  -0.23%  (p=0.000 n=10+10)

name                                            old allocs/op  new allocs/op  delta
Sysbench/KV/1node_remote/oltp_point_select-10       57.0 ± 0%      55.0 ± 0%  -3.51%  (p=0.000 n=10+9)
Sysbench/KV/1node_remote/oltp_read_only-10         1.83k ± 0%     1.80k ± 0%  -1.54%  (p=0.000 n=10+10)
Sysbench/KV/1node_remote/oltp_read_write-10        3.33k ± 0%     3.29k ± 0%  -1.43%  (p=0.000 n=10+9)
Sysbench/SQL/1node_remote/oltp_write_only-10       3.79k ± 0%     3.74k ± 0%  -1.38%  (p=0.000 n=10+9)
Sysbench/KV/1node_remote/oltp_write_only-10        1.51k ± 0%     1.49k ± 0%  -1.37%  (p=0.000 n=10+10)
Sysbench/SQL/1node_remote/oltp_read_write-10       8.40k ± 0%     8.31k ± 0%  -1.13%  (p=0.000 n=10+10)
Sysbench/SQL/1node_remote/oltp_point_select-10       253 ± 0%       251 ± 1%  -1.03%  (p=0.000 n=10+10)
Sysbench/SQL/1node_remote/oltp_read_only-10        4.60k ± 0%     4.56k ± 0%  -0.89%  (p=0.000 n=10+10)

Epic: None
Release note: None

@nvanbenschoten nvanbenschoten added the o-perf-efficiency Related to performance efficiency label Dec 14, 2024
@nvanbenschoten nvanbenschoten requested review from a team as code owners December 14, 2024 03:57
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Member

@tbg tbg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 4 of 4 files at r1, 1 of 1 files at r2, 5 of 5 files at r3, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @nvanbenschoten)

This commit adds a pair of tests to measure the size of empty
BatchRequest and BatchResponse messages when serialized.

Epic: None
Release note: None
@nvanbenschoten nvanbenschoten force-pushed the nvanbenschoten/duration-alloc branch from d84db25 to c647cdd Compare December 16, 2024 16:04
Picks up cockroachdb/gogoproto#10.

> This commit updates the serialization of `time.Duration` (see
> `gogo.stdduration`) to avoid serializing the default (zero) value in proto3. In
> Go, `time.Duration` is a typedef of int64, so it was surprising that unlike
> int64, the zero value of time.Duration fields were being put on the wire.
>
> While here, we also fix `DurationFromProto` to not cause its input to escape to
> the heap by passing it through `fmt.Errorf`. This function is called from
> `StdDurationUnmarshal` and its unintentional heap allocation what alerted me to
> the zero-value serialization issue.

Performance improvement on sysbench microbenchmarks:
```
name                                            old time/op    new time/op    delta
Sysbench/SQL/1node_remote/oltp_read_only-10       3.09ms ±12%    3.08ms ± 9%    ~     (p=0.905 n=9+10)
Sysbench/SQL/1node_remote/oltp_write_only-10      1.97ms ± 4%    1.96ms ± 3%    ~     (p=0.278 n=9+10)
Sysbench/SQL/1node_remote/oltp_read_write-10      5.73ms ± 1%    5.73ms ± 1%    ~     (p=1.000 n=7+8)
Sysbench/SQL/1node_remote/oltp_point_select-10     180µs ± 6%     181µs ±14%    ~     (p=0.853 n=10+10)
Sysbench/KV/1node_remote/oltp_read_only-10         736µs ± 3%     729µs ± 3%    ~     (p=0.190 n=10+10)
Sysbench/KV/1node_remote/oltp_write_only-10        594µs ± 2%     587µs ± 4%    ~     (p=0.211 n=9+10)
Sysbench/KV/1node_remote/oltp_read_write-10       1.44ms ± 6%    1.42ms ± 3%    ~     (p=0.156 n=10+9)
Sysbench/KV/1node_remote/oltp_point_select-10     33.7µs ±11%    34.9µs ± 9%    ~     (p=0.247 n=10+10)

name                                            old alloc/op   new alloc/op   delta
Sysbench/KV/1node_remote/oltp_point_select-10     6.69kB ± 1%    6.61kB ± 1%  -1.18%  (p=0.001 n=10+9)
Sysbench/SQL/1node_remote/oltp_point_select-10    30.0kB ± 0%    29.7kB ± 1%  -0.87%  (p=0.000 n=8+10)
Sysbench/SQL/1node_remote/oltp_write_only-10       476kB ± 0%     474kB ± 0%  -0.61%  (p=0.000 n=10+9)
Sysbench/KV/1node_remote/oltp_write_only-10        230kB ± 0%     229kB ± 0%  -0.53%  (p=0.000 n=10+10)
Sysbench/SQL/1node_remote/oltp_read_write-10      1.62MB ± 0%    1.62MB ± 0%  -0.34%  (p=0.000 n=10+10)
Sysbench/KV/1node_remote/oltp_read_only-10         647kB ± 0%     645kB ± 0%  -0.25%  (p=0.000 n=10+10)
Sysbench/KV/1node_remote/oltp_read_write-10        872kB ± 0%     870kB ± 0%  -0.25%  (p=0.000 n=10+9)
Sysbench/SQL/1node_remote/oltp_read_only-10       1.15MB ± 0%    1.15MB ± 0%  -0.23%  (p=0.000 n=10+10)

name                                            old allocs/op  new allocs/op  delta
Sysbench/KV/1node_remote/oltp_point_select-10       57.0 ± 0%      55.0 ± 0%  -3.51%  (p=0.000 n=10+9)
Sysbench/KV/1node_remote/oltp_read_only-10         1.83k ± 0%     1.80k ± 0%  -1.54%  (p=0.000 n=10+10)
Sysbench/KV/1node_remote/oltp_read_write-10        3.33k ± 0%     3.29k ± 0%  -1.43%  (p=0.000 n=10+9)
Sysbench/SQL/1node_remote/oltp_write_only-10       3.79k ± 0%     3.74k ± 0%  -1.38%  (p=0.000 n=10+9)
Sysbench/KV/1node_remote/oltp_write_only-10        1.51k ± 0%     1.49k ± 0%  -1.37%  (p=0.000 n=10+10)
Sysbench/SQL/1node_remote/oltp_read_write-10       8.40k ± 0%     8.31k ± 0%  -1.13%  (p=0.000 n=10+10)
Sysbench/SQL/1node_remote/oltp_point_select-10       253 ± 0%       251 ± 1%  -1.03%  (p=0.000 n=10+10)
Sysbench/SQL/1node_remote/oltp_read_only-10        4.60k ± 0%     4.56k ± 0%  -0.89%  (p=0.000 n=10+10)
```

Epic: None
Release note: None
@nvanbenschoten nvanbenschoten force-pushed the nvanbenschoten/duration-alloc branch from c647cdd to cabfad2 Compare December 16, 2024 16:59
@nvanbenschoten
Copy link
Member Author

TFTRs!

bors r+

@craig craig bot merged commit c524fdc into cockroachdb:master Dec 16, 2024
22 checks passed
@nvanbenschoten nvanbenschoten deleted the nvanbenschoten/duration-alloc branch December 18, 2024 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
o-perf-efficiency Related to performance efficiency
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants