Skip to content

opt: speed up new init pattern#62767

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
mgartner:init-optimizations
Mar 30, 2021
Merged

opt: speed up new init pattern#62767
craig[bot] merged 1 commit intocockroachdb:masterfrom
mgartner:init-optimizations

Conversation

@mgartner
Copy link
Copy Markdown
Contributor

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

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
@mgartner mgartner requested a review from RaduBerinde March 29, 2021 23:35
@mgartner mgartner requested a review from a team as a code owner March 29, 2021 23:35
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

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: This should be backported, right?

Reviewed 3 of 3 files at r1.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @RaduBerinde)

@mgartner
Copy link
Copy Markdown
Contributor Author

mgartner commented Mar 30, 2021

This should be backported, right?

Yes! Label added.

Copy link
Copy Markdown
Member

@RaduBerinde RaduBerinde left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @mgartner)

@mgartner
Copy link
Copy Markdown
Contributor Author

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Mar 30, 2021

Build succeeded:

@craig craig bot merged commit 194c7b9 into cockroachdb:master Mar 30, 2021
@mgartner mgartner deleted the init-optimizations branch March 30, 2021 20:06
Copy link
Copy Markdown
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained


pkg/sql/opt/metadata.go, line 154 at r1 (raw file):

	// Clear the metadata objects to release memory (this clearing pattern is
	// optimized by Go).
	schemas := md.schemas

IIUC (I didn't look at the assembly or gcflags) the optimization in this PR is bounds-checks elimination for slice accesses. If that's the case, we have been using //gcassert:bce annotations to enforce during linting that BCE occur.

@RaduBerinde
Copy link
Copy Markdown
Member


pkg/sql/opt/metadata.go, line 154 at r1 (raw file):

Previously, yuzefovich (Yahor Yuzefovich) wrote…

IIUC (I didn't look at the assembly or gcflags) the optimization in this PR is bounds-checks elimination for slice accesses. If that's the case, we have been using //gcassert:bce annotations to enforce during linting that BCE occur.

The issue was that we were using md. inside *md = {...}, and Go was making a copy of the entire struct onto the stack. This way we store only the data we need (hopefully some of it in registers).

@rafiss rafiss added this to the 21.1 milestone Apr 22, 2021
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.

6 participants