Skip to content

Commit

Permalink
db: remove newRangeKeyIter closure
Browse files Browse the repository at this point in the history
Avoid unnecessarily creating a closure over the *DB, resulting in an
allocation during iterator construction. This closure would have been
eliminated once range key state is fully ecapsulated by the `readState`. This
commit replaces the closure with a direct `*DB` reference for now to avoid the
allocation which may impact benchmarks, including with iterators that only
iterate over point keys.

```
name                                                             old time/op    new time/op    delta
IteratorScan/keys=100,r-amp=1,key-types=points-only-10             5.85µs ± 1%    5.78µs ± 1%   -1.20%  (p=0.016 n=5+5)
IteratorScan/keys=100,r-amp=1,key-types=points-and-ranges-10       9.20µs ± 1%    9.26µs ± 1%     ~     (p=0.421 n=5+5)
IteratorScan/keys=100,r-amp=3,key-types=points-only-10             10.4µs ± 0%    10.4µs ± 1%     ~     (p=0.690 n=5+5)
IteratorScan/keys=100,r-amp=3,key-types=points-and-ranges-10       14.2µs ± 1%    13.9µs ± 0%   -2.10%  (p=0.008 n=5+5)
IteratorScan/keys=100,r-amp=7,key-types=points-only-10             15.7µs ± 2%    15.5µs ± 0%     ~     (p=0.095 n=5+5)
IteratorScan/keys=100,r-amp=7,key-types=points-and-ranges-10       19.2µs ± 1%    19.1µs ± 0%     ~     (p=0.056 n=5+5)
IteratorScan/keys=100,r-amp=10,key-types=points-only-10            19.2µs ± 1%    19.0µs ± 1%     ~     (p=0.095 n=5+5)
IteratorScan/keys=100,r-amp=10,key-types=points-and-ranges-10      23.2µs ± 1%    23.1µs ± 0%     ~     (p=0.056 n=5+5)
IteratorScan/keys=1000,r-amp=1,key-types=points-only-10            44.8µs ± 0%    44.4µs ± 1%   -0.91%  (p=0.008 n=5+5)
IteratorScan/keys=1000,r-amp=1,key-types=points-and-ranges-10      71.8µs ± 0%    71.4µs ± 0%   -0.48%  (p=0.016 n=5+5)
IteratorScan/keys=1000,r-amp=3,key-types=points-only-10            77.6µs ± 0%    76.9µs ± 0%   -0.98%  (p=0.008 n=5+5)
IteratorScan/keys=1000,r-amp=3,key-types=points-and-ranges-10       105µs ± 0%     104µs ± 0%   -1.30%  (p=0.008 n=5+5)
IteratorScan/keys=1000,r-amp=7,key-types=points-only-10             105µs ± 0%     105µs ± 0%   -0.74%  (p=0.016 n=5+5)
IteratorScan/keys=1000,r-amp=7,key-types=points-and-ranges-10       133µs ± 0%     133µs ± 0%   -0.54%  (p=0.032 n=5+5)
IteratorScan/keys=1000,r-amp=10,key-types=points-only-10            119µs ± 0%     119µs ± 0%   -0.52%  (p=0.008 n=5+5)
IteratorScan/keys=1000,r-amp=10,key-types=points-and-ranges-10      147µs ± 1%     146µs ± 0%   -0.58%  (p=0.008 n=5+5)
IteratorScan/keys=10000,r-amp=1,key-types=points-only-10            422µs ± 0%     420µs ± 1%     ~     (p=0.095 n=5+5)
IteratorScan/keys=10000,r-amp=1,key-types=points-and-ranges-10      663µs ± 2%     665µs ± 0%     ~     (p=0.151 n=5+5)
IteratorScan/keys=10000,r-amp=3,key-types=points-only-10            711µs ± 1%     711µs ± 1%     ~     (p=0.841 n=5+5)
IteratorScan/keys=10000,r-amp=3,key-types=points-and-ranges-10      966µs ± 1%     965µs ± 1%     ~     (p=0.841 n=5+5)
IteratorScan/keys=10000,r-amp=7,key-types=points-only-10            951µs ± 0%     951µs ± 1%     ~     (p=1.000 n=5+5)
IteratorScan/keys=10000,r-amp=7,key-types=points-and-ranges-10     1.20ms ± 0%    1.20ms ± 1%     ~     (p=0.841 n=5+5)
IteratorScan/keys=10000,r-amp=10,key-types=points-only-10          1.06ms ± 1%    1.06ms ± 0%     ~     (p=0.222 n=5+5)
IteratorScan/keys=10000,r-amp=10,key-types=points-and-ranges-10    1.30ms ± 0%    1.31ms ± 1%   +0.58%  (p=0.032 n=5+5)

name                                                             old alloc/op   new alloc/op   delta
IteratorScan/keys=100,r-amp=1,key-types=points-only-10              64.0B ± 0%     16.0B ± 0%  -75.00%  (p=0.008 n=5+5)
IteratorScan/keys=100,r-amp=1,key-types=points-and-ranges-10         200B ± 0%      152B ± 0%  -24.00%  (p=0.008 n=5+5)
IteratorScan/keys=100,r-amp=3,key-types=points-only-10              96.0B ± 0%     48.0B ± 0%  -50.00%  (p=0.008 n=5+5)
IteratorScan/keys=100,r-amp=3,key-types=points-and-ranges-10         232B ± 0%      184B ± 0%  -20.69%  (p=0.008 n=5+5)
IteratorScan/keys=100,r-amp=7,key-types=points-only-10               160B ± 0%      112B ± 0%  -30.00%  (p=0.008 n=5+5)
IteratorScan/keys=100,r-amp=7,key-types=points-and-ranges-10         296B ± 0%      248B ± 0%  -16.22%  (p=0.008 n=5+5)
IteratorScan/keys=100,r-amp=10,key-types=points-only-10              208B ± 0%      160B ± 0%  -23.08%  (p=0.008 n=5+5)
IteratorScan/keys=100,r-amp=10,key-types=points-and-ranges-10        344B ± 0%      296B ± 0%  -13.95%  (p=0.008 n=5+5)
IteratorScan/keys=1000,r-amp=1,key-types=points-only-10             64.0B ± 0%     16.0B ± 0%  -75.00%  (p=0.008 n=5+5)
IteratorScan/keys=1000,r-amp=1,key-types=points-and-ranges-10        200B ± 0%      152B ± 0%  -24.15%  (p=0.008 n=5+5)
IteratorScan/keys=1000,r-amp=3,key-types=points-only-10             96.0B ± 0%     48.0B ± 0%     ~     (p=0.079 n=4+5)
IteratorScan/keys=1000,r-amp=3,key-types=points-and-ranges-10        233B ± 0%      185B ± 0%  -20.60%  (p=0.008 n=5+5)
IteratorScan/keys=1000,r-amp=7,key-types=points-only-10              160B ± 0%      113B ± 0%  -29.55%  (p=0.000 n=5+4)
IteratorScan/keys=1000,r-amp=7,key-types=points-and-ranges-10        297B ± 0%      249B ± 0%  -16.16%  (p=0.008 n=5+5)
IteratorScan/keys=1000,r-amp=10,key-types=points-only-10             209B ± 0%      161B ± 0%  -22.97%  (p=0.029 n=4+4)
IteratorScan/keys=1000,r-amp=10,key-types=points-and-ranges-10       346B ± 0%      298B ± 0%  -13.87%  (p=0.029 n=4+4)
IteratorScan/keys=10000,r-amp=1,key-types=points-only-10            66.4B ± 4%     20.0B ± 0%  -69.88%  (p=0.000 n=5+4)
IteratorScan/keys=10000,r-amp=1,key-types=points-and-ranges-10       208B ± 0%      160B ± 0%     ~     (p=0.079 n=4+5)
IteratorScan/keys=10000,r-amp=3,key-types=points-only-10             103B ± 1%       53B ± 7%  -48.86%  (p=0.016 n=4+5)
IteratorScan/keys=10000,r-amp=3,key-types=points-and-ranges-10       240B ± 2%      196B ± 0%  -18.47%  (p=0.008 n=5+5)
IteratorScan/keys=10000,r-amp=7,key-types=points-only-10             170B ± 0%      117B ± 4%  -31.06%  (p=0.000 n=4+5)
IteratorScan/keys=10000,r-amp=7,key-types=points-and-ranges-10       312B ± 0%      259B ± 3%  -16.98%  (p=0.016 n=4+5)
IteratorScan/keys=10000,r-amp=10,key-types=points-only-10            220B ± 0%      172B ± 0%  -21.82%  (p=0.029 n=4+4)
IteratorScan/keys=10000,r-amp=10,key-types=points-and-ranges-10      362B ± 0%      314B ± 0%  -13.14%  (p=0.000 n=4+5)

name                                                             old allocs/op  new allocs/op  delta
IteratorScan/keys=100,r-amp=1,key-types=points-only-10               2.00 ± 0%      1.00 ± 0%  -50.00%  (p=0.008 n=5+5)
IteratorScan/keys=100,r-amp=1,key-types=points-and-ranges-10         8.00 ± 0%      7.00 ± 0%  -12.50%  (p=0.008 n=5+5)
IteratorScan/keys=100,r-amp=3,key-types=points-only-10               4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.008 n=5+5)
IteratorScan/keys=100,r-amp=3,key-types=points-and-ranges-10         10.0 ± 0%       9.0 ± 0%  -10.00%  (p=0.008 n=5+5)
IteratorScan/keys=100,r-amp=7,key-types=points-only-10               8.00 ± 0%      7.00 ± 0%  -12.50%  (p=0.008 n=5+5)
IteratorScan/keys=100,r-amp=7,key-types=points-and-ranges-10         14.0 ± 0%      13.0 ± 0%   -7.14%  (p=0.008 n=5+5)
IteratorScan/keys=100,r-amp=10,key-types=points-only-10              11.0 ± 0%      10.0 ± 0%   -9.09%  (p=0.008 n=5+5)
IteratorScan/keys=100,r-amp=10,key-types=points-and-ranges-10        17.0 ± 0%      16.0 ± 0%   -5.88%  (p=0.008 n=5+5)
IteratorScan/keys=1000,r-amp=1,key-types=points-only-10              2.00 ± 0%      1.00 ± 0%  -50.00%  (p=0.008 n=5+5)
IteratorScan/keys=1000,r-amp=1,key-types=points-and-ranges-10        8.00 ± 0%      7.00 ± 0%  -12.50%  (p=0.008 n=5+5)
IteratorScan/keys=1000,r-amp=3,key-types=points-only-10              4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.008 n=5+5)
IteratorScan/keys=1000,r-amp=3,key-types=points-and-ranges-10        10.0 ± 0%       9.0 ± 0%  -10.00%  (p=0.008 n=5+5)
IteratorScan/keys=1000,r-amp=7,key-types=points-only-10              8.00 ± 0%      7.00 ± 0%  -12.50%  (p=0.008 n=5+5)
IteratorScan/keys=1000,r-amp=7,key-types=points-and-ranges-10        14.0 ± 0%      13.0 ± 0%   -7.14%  (p=0.008 n=5+5)
IteratorScan/keys=1000,r-amp=10,key-types=points-only-10             11.0 ± 0%      10.0 ± 0%   -9.09%  (p=0.008 n=5+5)
IteratorScan/keys=1000,r-amp=10,key-types=points-and-ranges-10       17.0 ± 0%      16.0 ± 0%   -5.88%  (p=0.008 n=5+5)
IteratorScan/keys=10000,r-amp=1,key-types=points-only-10             2.00 ± 0%      1.00 ± 0%  -50.00%  (p=0.008 n=5+5)
IteratorScan/keys=10000,r-amp=1,key-types=points-and-ranges-10       8.00 ± 0%      7.00 ± 0%  -12.50%  (p=0.008 n=5+5)
IteratorScan/keys=10000,r-amp=3,key-types=points-only-10             4.00 ± 0%      3.00 ± 0%  -25.00%  (p=0.008 n=5+5)
IteratorScan/keys=10000,r-amp=3,key-types=points-and-ranges-10       10.0 ± 0%       9.0 ± 0%  -10.00%  (p=0.008 n=5+5)
IteratorScan/keys=10000,r-amp=7,key-types=points-only-10             8.00 ± 0%      7.00 ± 0%  -12.50%  (p=0.008 n=5+5)
IteratorScan/keys=10000,r-amp=7,key-types=points-and-ranges-10       14.0 ± 0%      13.0 ± 0%   -7.14%  (p=0.008 n=5+5)
IteratorScan/keys=10000,r-amp=10,key-types=points-only-10            11.0 ± 0%      10.0 ± 0%   -9.09%  (p=0.008 n=5+5)
IteratorScan/keys=10000,r-amp=10,key-types=points-and-ranges-10      17.0 ± 0%      16.0 ± 0%   -5.88%  (p=0.008 n=5+5)
```
  • Loading branch information
jbowens committed May 5, 2022
1 parent 6d97548 commit 5ae2174
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
6 changes: 2 additions & 4 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,9 +930,7 @@ func (d *DB) newIterInternal(batch *Batch, s *Snapshot, o *IterOptions) *Iterato
batch: batch,
newIters: d.newIters,
seqNum: seqNum,
newRangeKeyIter: func(it *iteratorRangeKeyState) keyspan.FragmentIterator {
return d.newRangeKeyIter(it, seqNum, batch, readState, &dbi.opts)
},
db: d,
}
if o != nil {
dbi.opts = *o
Expand Down Expand Up @@ -974,7 +972,7 @@ func finishInitializingIter(buf *iterAlloc) *Iterator {
if dbi.rangeKey == nil {
dbi.rangeKey = iterRangeKeyStateAllocPool.Get().(*iteratorRangeKeyState)
dbi.rangeKey.keys.cmp = dbi.cmp
dbi.rangeKey.rangeKeyIter = dbi.newRangeKeyIter(dbi.rangeKey)
dbi.rangeKey.rangeKeyIter = dbi.db.newRangeKeyIter(dbi.rangeKey, dbi.seqNum, dbi.batch, dbi.readState, &dbi.opts)
}

// Wrap the point iterator (currently dbi.iter) with an interleaving
Expand Down
7 changes: 4 additions & 3 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,9 @@ type Iterator struct {
batch *Batch
newIters tableNewIters
seqNum uint64
// TODO(jackson): Remove when we no longer require the global arena.
newRangeKeyIter func(*iteratorRangeKeyState) keyspan.FragmentIterator
// TODO(jackson): Remove db when we no longer require the global arena for
// range keys. This reference is only temporary.
db *DB

// Keeping the bools here after all the 8 byte aligned fields shrinks the
// sizeof this struct by 24 bytes.
Expand Down Expand Up @@ -1941,7 +1942,7 @@ func (i *Iterator) Clone() (*Iterator, error) {
batch: i.batch,
newIters: i.newIters,
seqNum: i.seqNum,
newRangeKeyIter: i.newRangeKeyIter,
db: i.db,
}
return finishInitializingIter(buf), nil
}
Expand Down

0 comments on commit 5ae2174

Please sign in to comment.