Skip to content

opt: use safer struct initialization pattern#58386

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
mgartner:safer-init-pattern
Jan 4, 2021
Merged

opt: use safer struct initialization pattern#58386
craig[bot] merged 1 commit intocockroachdb:masterfrom
mgartner:safer-init-pattern

Conversation

@mgartner
Copy link
Copy Markdown
Contributor

We commonly use a pattern of declaring structs and then initializing
them via an Init method. This allows internal data structures to be
reused, reducing allocations. However, the common initialization
implementation is dangerous because fields are implicitly reused. This
can lead to inconspicuous bugs (see #58306).

This commit introduces a new pattern for initialization functions that
requires reused fields to be explicitly listed. This should reduce the
risk of these types of bugs.

Release note: None

@mgartner mgartner requested a review from a team as a code owner December 30, 2020 23:53
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

We commonly use a pattern of declaring structs and then initializing
them via an `Init` method. This allows internal data structures to be
reused, reducing allocations. However, the common initialization
implementation is dangerous because fields are implicitly reused. This
can lead to inconspicuous bugs (see cockroachdb#58306).

This commit introduces a new pattern for initialization functions that
requires reused fields to be explicitly listed. This should reduce the
risk of these types of bugs.

Release note: None
Copy link
Copy Markdown
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

:lgtm: Looks like this was tedious -- thanks for being so thorough!

Reviewed 34 of 34 files at r1.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @andy-kimball and @RaduBerinde)

@mgartner
Copy link
Copy Markdown
Contributor Author

mgartner commented Jan 4, 2021

TFTR!

bors r=rytaft

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Jan 4, 2021

Build succeeded:

@craig craig bot merged commit 2ee71b3 into cockroachdb:master Jan 4, 2021
@mgartner mgartner deleted the safer-init-pattern branch January 4, 2021 17:55
@RaduBerinde
Copy link
Copy Markdown
Member

💯

mgartner added a commit to mgartner/cockroach that referenced this pull request Mar 29, 2021
A new initialization pattern was introduced in cockroachdb#58386 which makes
inadvertently reusing struct fields less likely. This pattern incurs a
slight overhead over the previous pattern which shows up in optimizer
micro-benchmarks. This commit makes a few changes to reduce the
overhead, but not completely eliminate it. The overhead is on the order
of tens of nanoseconds, so it should not be a huge concern.

The speedup of this change on the kv-read benchmark is shown below:

    name                              old time/op  new time/op  delta
    kv-read/Parse              2.82ns ± 5%  2.60ns ± 0%   -7.59%  (p=0.008 n=5+5)
    kv-read/OptBuild           8.81µs ± 1%  7.97µs ± 4%   -9.57%  (p=0.008 n=5+5)
    kv-read/Normalize          8.87µs ± 7%  7.80µs ± 2%  -12.08%  (p=0.008 n=5+5)
    kv-read/Explore            14.9µs ± 1%  14.6µs ± 1%   -1.96%  (p=0.008 n=5+5)
    kv-read/ExecBuild          16.2µs ± 1%  16.2µs ± 1%     ~     (p=0.222 n=5+5)
    kv-read-no-prep/Parse      14.6µs ± 2%  14.4µs ± 1%     ~     (p=0.222 n=5+5)
    kv-read-no-prep/OptBuild   40.3µs ± 4%  38.9µs ± 1%     ~     (p=0.095 n=5+5)
    kv-read-no-prep/Normalize  42.2µs ± 1%  40.7µs ± 1%   -3.44%  (p=0.008 n=5+5)
    kv-read-no-prep/Explore    52.0µs ± 4%  50.2µs ± 1%   -3.37%  (p=0.008 n=5+5)
    kv-read-no-prep/ExecBuild  53.7µs ± 2%  51.7µs ± 1%   -3.79%  (p=0.008 n=5+5)
    kv-read-const/Parse        2.64ns ± 2%  2.62ns ± 1%     ~     (p=0.135 n=5+5)
    kv-read-const/OptBuild      208ns ± 2%   206ns ± 2%     ~     (p=0.659 n=5+5)
    kv-read-const/Normalize     204ns ± 1%   201ns ± 1%   -1.86%  (p=0.016 n=5+5)
    kv-read-const/Explore       225ns ± 3%   229ns ± 6%     ~     (p=0.579 n=5+5)
    kv-read-const/ExecBuild     829ns ± 8%   810ns ± 3%     ~     (p=0.841 n=5+5)

Release note: None
craig bot pushed a commit that referenced this pull request Mar 30, 2021
62767: opt: speed up new init pattern r=mgartner a=mgartner

A new initialization pattern was introduced in #58386 which makes
inadvertently reusing struct fields less likely. This pattern incurs a
slight overhead over the previous pattern which shows up in optimizer
micro-benchmarks. This commit makes a few changes to reduce the
overhead, but not completely eliminate it. The overhead is on the order
of tens of nanoseconds, so it should not be a huge concern.

The speedup of this change on the kv-read benchmark is shown below:

    name                              old time/op  new time/op  delta
    kv-read/Parse              2.82ns ± 5%  2.60ns ± 0%   -7.59%  (p=0.008 n=5+5)
    kv-read/OptBuild           8.81µs ± 1%  7.97µs ± 4%   -9.57%  (p=0.008 n=5+5)
    kv-read/Normalize          8.87µs ± 7%  7.80µs ± 2%  -12.08%  (p=0.008 n=5+5)
    kv-read/Explore            14.9µs ± 1%  14.6µs ± 1%   -1.96%  (p=0.008 n=5+5)
    kv-read/ExecBuild          16.2µs ± 1%  16.2µs ± 1%     ~     (p=0.222 n=5+5)
    kv-read-no-prep/Parse      14.6µs ± 2%  14.4µs ± 1%     ~     (p=0.222 n=5+5)
    kv-read-no-prep/OptBuild   40.3µs ± 4%  38.9µs ± 1%     ~     (p=0.095 n=5+5)
    kv-read-no-prep/Normalize  42.2µs ± 1%  40.7µs ± 1%   -3.44%  (p=0.008 n=5+5)
    kv-read-no-prep/Explore    52.0µs ± 4%  50.2µs ± 1%   -3.37%  (p=0.008 n=5+5)
    kv-read-no-prep/ExecBuild  53.7µs ± 2%  51.7µs ± 1%   -3.79%  (p=0.008 n=5+5)
    kv-read-const/Parse        2.64ns ± 2%  2.62ns ± 1%     ~     (p=0.135 n=5+5)
    kv-read-const/OptBuild      208ns ± 2%   206ns ± 2%     ~     (p=0.659 n=5+5)
    kv-read-const/Normalize     204ns ± 1%   201ns ± 1%   -1.86%  (p=0.016 n=5+5)
    kv-read-const/Explore       225ns ± 3%   229ns ± 6%     ~     (p=0.579 n=5+5)
    kv-read-const/ExecBuild     829ns ± 8%   810ns ± 3%     ~     (p=0.841 n=5+5)

Release note: None

Co-authored-by: Marcus Gartner <marcus@cockroachlabs.com>
mgartner added a commit to mgartner/cockroach that referenced this pull request Mar 30, 2021
A new initialization pattern was introduced in cockroachdb#58386 which makes
inadvertently reusing struct fields less likely. This pattern incurs a
slight overhead over the previous pattern which shows up in optimizer
micro-benchmarks. This commit makes a few changes to reduce the
overhead, but not completely eliminate it. The overhead is on the order
of tens of nanoseconds, so it should not be a huge concern.

The speedup of this change on the kv-read benchmark is shown below:

    name                              old time/op  new time/op  delta
    kv-read/Parse              2.82ns ± 5%  2.60ns ± 0%   -7.59%  (p=0.008 n=5+5)
    kv-read/OptBuild           8.81µs ± 1%  7.97µs ± 4%   -9.57%  (p=0.008 n=5+5)
    kv-read/Normalize          8.87µs ± 7%  7.80µs ± 2%  -12.08%  (p=0.008 n=5+5)
    kv-read/Explore            14.9µs ± 1%  14.6µs ± 1%   -1.96%  (p=0.008 n=5+5)
    kv-read/ExecBuild          16.2µs ± 1%  16.2µs ± 1%     ~     (p=0.222 n=5+5)
    kv-read-no-prep/Parse      14.6µs ± 2%  14.4µs ± 1%     ~     (p=0.222 n=5+5)
    kv-read-no-prep/OptBuild   40.3µs ± 4%  38.9µs ± 1%     ~     (p=0.095 n=5+5)
    kv-read-no-prep/Normalize  42.2µs ± 1%  40.7µs ± 1%   -3.44%  (p=0.008 n=5+5)
    kv-read-no-prep/Explore    52.0µs ± 4%  50.2µs ± 1%   -3.37%  (p=0.008 n=5+5)
    kv-read-no-prep/ExecBuild  53.7µs ± 2%  51.7µs ± 1%   -3.79%  (p=0.008 n=5+5)
    kv-read-const/Parse        2.64ns ± 2%  2.62ns ± 1%     ~     (p=0.135 n=5+5)
    kv-read-const/OptBuild      208ns ± 2%   206ns ± 2%     ~     (p=0.659 n=5+5)
    kv-read-const/Normalize     204ns ± 1%   201ns ± 1%   -1.86%  (p=0.016 n=5+5)
    kv-read-const/Explore       225ns ± 3%   229ns ± 6%     ~     (p=0.579 n=5+5)
    kv-read-const/ExecBuild     829ns ± 8%   810ns ± 3%     ~     (p=0.841 n=5+5)

Release note: None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants