Skip to content

Commit

Permalink
interval/generic: avoid letting argument to FirstOverlap escape to heap
Browse files Browse the repository at this point in the history
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)
```
  • Loading branch information
nvanbenschoten committed Feb 26, 2020
1 parent 18a5a5e commit d25fdb5
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 133 deletions.
37 changes: 15 additions & 22 deletions pkg/storage/spanlatch/latch_interval_btree.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 21 additions & 22 deletions pkg/storage/spanlatch/latch_interval_btree_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/storage/spanlatch/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func (m *Manager) wait(ctx context.Context, lg *Guard, snap snapshot) error {
func (m *Manager) iterAndWait(
ctx context.Context, t *timeutil.Timer, it *iterator, wait *latch, ignore ignoreFn,
) error {
for it.FirstOverlap(wait); it.Valid(); it.NextOverlap() {
for it.FirstOverlap(wait); it.Valid(); it.NextOverlap(wait) {
held := it.Cur()
if held.done.signaled() {
continue
Expand Down
37 changes: 15 additions & 22 deletions pkg/util/interval/generic/example_interval_btree.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d25fdb5

Please sign in to comment.