Skip to content

interval/generic: avoid letting argument to FirstOverlap escape to heap#45423

Merged
craig[bot] merged 2 commits intocockroachdb:masterfrom
nvb:nvanbenschoten/overlapScanFix
Feb 26, 2020
Merged

interval/generic: avoid letting argument to FirstOverlap escape to heap#45423
craig[bot] merged 2 commits intocockroachdb:masterfrom
nvb:nvanbenschoten/overlapScanFix

Conversation

@nvb
Copy link
Copy Markdown
Contributor

@nvb nvb commented Feb 26, 2020

Related to #45276.

Prior to this change, the argument provided to iterator.FirstOverlap was stored in the iterator. This caused the argument to escape to the heap (if the parameterized type was a pointer) and would force an allocation if the argument was not already on the heap. This was not the intention and was causing issues in #45276.

This commit fixes the issue by no longer storing the argument in the iterator's overlapScan object. This allows escape analysis to determine that the argument does not escape, like it was already doing for iterator.SeekGE and iterator.SeekLT.

name                                  old time/op    new time/op    delta
BTreeIterFirstOverlap/count=16-16        400ns ± 1%     325ns ± 5%   -18.75%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=128-16       661ns ± 0%     581ns ± 1%   -12.02%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=1024-16     1.15µs ± 2%    1.07µs ± 2%    -6.76%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=8192-16     1.84µs ± 2%    1.76µs ± 2%    -4.38%  (p=0.016 n=5+5)
BTreeIterFirstOverlap/count=65536-16    2.62µs ± 2%    2.52µs ± 1%    -4.07%  (p=0.008 n=5+5)
BTreeIterNextOverlap-16                 11.1ns ± 2%    10.7ns ± 0%    -3.95%  (p=0.016 n=5+4)
BTreeIterOverlapScan-16                 34.2µs ± 1%    31.2µs ± 1%    -8.78%  (p=0.008 n=5+5)

name                                  old alloc/op   new alloc/op   delta
BTreeIterFirstOverlap/count=16-16        96.0B ± 0%      0.0B       -100.00%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=128-16       96.0B ± 0%      0.0B       -100.00%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=1024-16      96.0B ± 0%      0.0B       -100.00%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=8192-16      96.0B ± 0%      0.0B       -100.00%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=65536-16     96.0B ± 0%      0.0B       -100.00%  (p=0.008 n=5+5)
BTreeIterNextOverlap-16                  0.00B          0.00B           ~     (all equal)
BTreeIterOverlapScan-16                   144B ± 0%       48B ± 0%   -66.67%  (p=0.008 n=5+5)

name                                  old allocs/op  new allocs/op  delta
BTreeIterFirstOverlap/count=16-16         1.00 ± 0%      0.00       -100.00%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=128-16        1.00 ± 0%      0.00       -100.00%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=1024-16       1.00 ± 0%      0.00       -100.00%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=8192-16       1.00 ± 0%      0.00       -100.00%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=65536-16      1.00 ± 0%      0.00       -100.00%  (p=0.008 n=5+5)
BTreeIterNextOverlap-16                   0.00           0.00           ~     (all equal)
BTreeIterOverlapScan-16                   6.40 ± 9%      5.00 ± 0%   -21.88%  (p=0.008 n=5+5)

nvb added 2 commits February 25, 2020 21:16
This was hiding the fact that FirstOverlap caused its argument to escape
to the heap, forcing an allocation for some callers. The benchmarks now
capture this:
```
name                                  time/op
BTreeIterFirstOverlap/count=16-16      400ns ± 1%
BTreeIterFirstOverlap/count=128-16     661ns ± 0%
BTreeIterFirstOverlap/count=1024-16   1.15µs ± 2%
BTreeIterFirstOverlap/count=8192-16   1.84µs ± 2%
BTreeIterFirstOverlap/count=65536-16  2.62µs ± 2%
BTreeIterNextOverlap-16               11.1ns ± 2%
BTreeIterOverlapScan-16               34.2µs ± 1%

name                                  alloc/op
BTreeIterFirstOverlap/count=16-16      96.0B ± 0%
BTreeIterFirstOverlap/count=128-16     96.0B ± 0%
BTreeIterFirstOverlap/count=1024-16    96.0B ± 0%
BTreeIterFirstOverlap/count=8192-16    96.0B ± 0%
BTreeIterFirstOverlap/count=65536-16   96.0B ± 0%
BTreeIterNextOverlap-16                0.00B
BTreeIterOverlapScan-16                 144B ± 0%

name                                  allocs/op
BTreeIterFirstOverlap/count=16-16       1.00 ± 0%
BTreeIterFirstOverlap/count=128-16      1.00 ± 0%
BTreeIterFirstOverlap/count=1024-16     1.00 ± 0%
BTreeIterFirstOverlap/count=8192-16     1.00 ± 0%
BTreeIterFirstOverlap/count=65536-16    1.00 ± 0%
BTreeIterNextOverlap-16                 0.00
BTreeIterOverlapScan-16                 6.40 ± 9%
```
Related to cockroachdb#45276.

Prior to this change, the argument provided to `iterator.FirstOverlap` was
stored in the iterator. This caused the argument to escape to the heap (if
the parameterized type was a pointer) and would force an allocation if the
argument was not already on the heap. This was not the intention and was
causing issues in cockroachdb#45276.

This commit fixes the issue by no longer storing the argument in the iterator's
`overlapScan` object. This allows escape analysis to determine that the argument
does not escape, like it was already doing for `iterator.SeekGE` and `iterator.SeekLT`.

```
name                                  old time/op    new time/op    delta
BTreeIterFirstOverlap/count=16-16        400ns ± 1%     325ns ± 5%   -18.75%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=128-16       661ns ± 0%     581ns ± 1%   -12.02%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=1024-16     1.15µs ± 2%    1.07µs ± 2%    -6.76%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=8192-16     1.84µs ± 2%    1.76µs ± 2%    -4.38%  (p=0.016 n=5+5)
BTreeIterFirstOverlap/count=65536-16    2.62µs ± 2%    2.52µs ± 1%    -4.07%  (p=0.008 n=5+5)
BTreeIterNextOverlap-16                 11.1ns ± 2%    10.7ns ± 0%    -3.95%  (p=0.016 n=5+4)
BTreeIterOverlapScan-16                 34.2µs ± 1%    31.2µs ± 1%    -8.78%  (p=0.008 n=5+5)

name                                  old alloc/op   new alloc/op   delta
BTreeIterFirstOverlap/count=16-16        96.0B ± 0%      0.0B       -100.00%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=128-16       96.0B ± 0%      0.0B       -100.00%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=1024-16      96.0B ± 0%      0.0B       -100.00%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=8192-16      96.0B ± 0%      0.0B       -100.00%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=65536-16     96.0B ± 0%      0.0B       -100.00%  (p=0.008 n=5+5)
BTreeIterNextOverlap-16                  0.00B          0.00B           ~     (all equal)
BTreeIterOverlapScan-16                   144B ± 0%       48B ± 0%   -66.67%  (p=0.008 n=5+5)

name                                  old allocs/op  new allocs/op  delta
BTreeIterFirstOverlap/count=16-16         1.00 ± 0%      0.00       -100.00%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=128-16        1.00 ± 0%      0.00       -100.00%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=1024-16       1.00 ± 0%      0.00       -100.00%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=8192-16       1.00 ± 0%      0.00       -100.00%  (p=0.008 n=5+5)
BTreeIterFirstOverlap/count=65536-16      1.00 ± 0%      0.00       -100.00%  (p=0.008 n=5+5)
BTreeIterNextOverlap-16                   0.00           0.00           ~     (all equal)
BTreeIterOverlapScan-16                   6.40 ± 9%      5.00 ± 0%   -21.88%  (p=0.008 n=5+5)
```
@nvb nvb requested a review from sumeerbhola February 26, 2020 02:17
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

Copy link
Copy Markdown
Collaborator

@sumeerbhola sumeerbhola left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 7 of 7 files at r2.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained

@nvb
Copy link
Copy Markdown
Contributor Author

nvb commented Feb 26, 2020

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Feb 26, 2020

Build succeeded

@craig craig bot merged commit 68d7ae1 into cockroachdb:master Feb 26, 2020
@nvb nvb deleted the nvanbenschoten/overlapScanFix branch February 26, 2020 22:31
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.

3 participants