Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

colfetcher: use the limit hint to size the batch #63020

Merged
merged 1 commit into from
Apr 3, 2021

Conversation

yuzefovich
Copy link
Member

@yuzefovich yuzefovich commented Apr 2, 2021

We recently merged a couple of commits to take advantage of the
optimizer-driven estimated row count that will be output by the cfetcher
and of the limit hint when available. However, one scenario was
overlooked - if there is no estimate but the limit hint is available, we
don't use the latter to size the original batch, and now we do.

As an additional optimization this commit refactors the initial state of
the state machine so that we didn't allocate the output batch in the
constructor of the ColBatchScan but do it only on the very first call
to NextBatch. This was prompted by the fact that the limit hint is
currently only set in StartScan which is called in
ColBatchScan.Init, so without some changes, in the case when the limit
hint can be used to size the batch we would allocate a redundant batch
of size one in NewColBatchScan. I also think that the new initial
state of the state machine makes a bit more sense to me.

Additionally, this commit generalizes the logic to keep track of the
limit hint as "remaining rows to be fetched."

Addresses: #62212.

Release note: None (this is a follow-up to a recently merged commit that
had a release note)

@yuzefovich yuzefovich requested review from jordanlewis and a team April 2, 2021 06:54
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Member

@jordanlewis jordanlewis left a comment

Choose a reason for hiding this comment

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

I don't understand, when would a limit hint be available and no estimate? Is this expected, and should we fix in the optimizer too?

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


pkg/sql/colfetcher/cfetcher.go, line 1073 at r1 (raw file):

			var emitBatch bool
			if rf.machine.rowIdx == rf.machine.batch.Capacity() ||
				(rf.machine.limitHint > 0 && rf.machine.rowIdx == rf.machine.limitHint) {

nit: I see you changed the "inaccurate" >= to == in both places. I was taught as a young programmer to prefer using >= for conditions like this because it limits the effects of other broken code in the system. I agree that I can see no reason why the index would exceed the capacity, and indeed many things worse could go wrong, but I think it's a good habit to keep a >= to "limit the blast radius" in case other bugs creep in.

@yuzefovich
Copy link
Member Author

I think such scenario can arise when there are no stats on the table, but the query has a LIMIT.

@yuzefovich
Copy link
Member Author

The benchmarks of this change:

Switching to use-limit-hint^
Switching to use-limit-hint
name                                                            old time/op    new time/op    delta
Scan/Cockroach/count=1/limit=0-24                                  406µs ± 6%     395µs ± 3%     ~     (p=0.063 n=10+10)
Scan/Cockroach/count=1/limit=1-24                                  416µs ± 4%     405µs ± 6%     ~     (p=0.123 n=10+10)
Scan/Cockroach/count=1/limit=10-24                                 417µs ± 1%     412µs ± 2%   -1.38%  (p=0.012 n=8+10)
Scan/Cockroach/count=1/limit=100-24                                414µs ± 3%     413µs ± 1%     ~     (p=0.481 n=10+10)
Scan/Cockroach/count=10/limit=0-24                                 429µs ± 3%     430µs ± 4%     ~     (p=0.631 n=10+10)
Scan/Cockroach/count=10/limit=1-24                                 425µs ± 4%     438µs ± 6%   +3.11%  (p=0.009 n=10+10)
Scan/Cockroach/count=10/limit=10-24                                432µs ± 4%     452µs ± 4%   +4.73%  (p=0.003 n=10+10)
Scan/Cockroach/count=10/limit=100-24                               452µs ± 4%     452µs ± 3%     ~     (p=0.912 n=10+10)
Scan/Cockroach/count=100/limit=0-24                                535µs ± 3%     529µs ± 3%     ~     (p=0.165 n=10+10)
Scan/Cockroach/count=100/limit=1-24                                445µs ± 5%     422µs ± 5%   -5.30%  (p=0.000 n=10+10)
Scan/Cockroach/count=100/limit=10-24                               548µs ± 2%     443µs ± 3%  -19.12%  (p=0.000 n=10+10)
Scan/Cockroach/count=100/limit=100-24                              533µs ± 2%     520µs ± 3%   -2.40%  (p=0.007 n=10+10)
Scan/Cockroach/count=1000/limit=0-24                              1.32ms ± 3%    1.34ms ± 1%   +1.74%  (p=0.004 n=10+9)
Scan/Cockroach/count=1000/limit=1-24                               447µs ± 4%     463µs ± 5%   +3.53%  (p=0.023 n=10+10)
Scan/Cockroach/count=1000/limit=10-24                              817µs ± 1%     470µs ± 3%  -42.48%  (p=0.000 n=10+10)
Scan/Cockroach/count=1000/limit=100-24                             872µs ± 2%     547µs ± 2%  -37.32%  (p=0.000 n=10+10)
Scan/Cockroach/count=10000/limit=0-24                             7.25ms ± 2%    7.29ms ± 2%     ~     (p=0.393 n=10+10)
Scan/Cockroach/count=10000/limit=1-24                              469µs ± 5%     477µs ± 3%     ~     (p=0.247 n=10+10)
Scan/Cockroach/count=10000/limit=10-24                             884µs ± 6%     493µs ± 6%  -44.25%  (p=0.000 n=10+10)
Scan/Cockroach/count=10000/limit=100-24                           1.00ms ± 1%    0.57ms ± 3%  -42.40%  (p=0.000 n=9+10)
Scan/MultinodeCockroach/count=1/limit=0-24                         888µs ± 3%     895µs ± 4%     ~     (p=0.243 n=9+10)
Scan/MultinodeCockroach/count=1/limit=1-24                         879µs ± 4%     867µs ± 3%     ~     (p=0.165 n=10+10)
Scan/MultinodeCockroach/count=1/limit=10-24                        876µs ± 4%     881µs ± 4%     ~     (p=0.356 n=10+9)
Scan/MultinodeCockroach/count=1/limit=100-24                       895µs ± 2%     910µs ± 4%     ~     (p=0.143 n=10+10)
Scan/MultinodeCockroach/count=10/limit=0-24                        958µs ± 2%     964µs ± 3%     ~     (p=0.481 n=10+10)
Scan/MultinodeCockroach/count=10/limit=1-24                        892µs ± 5%     884µs ± 3%     ~     (p=0.436 n=10+10)
Scan/MultinodeCockroach/count=10/limit=10-24                       937µs ± 2%     896µs ± 4%   -4.41%  (p=0.000 n=9+10)
Scan/MultinodeCockroach/count=10/limit=100-24                      968µs ± 7%     974µs ± 7%     ~     (p=0.739 n=10+10)
Scan/MultinodeCockroach/count=100/limit=0-24                      1.14ms ± 9%    1.12ms ± 3%     ~     (p=0.579 n=10+10)
Scan/MultinodeCockroach/count=100/limit=1-24                       905µs ± 3%     907µs ± 2%     ~     (p=0.971 n=10+10)
Scan/MultinodeCockroach/count=100/limit=10-24                     1.05ms ± 4%    0.92ms ± 2%  -12.76%  (p=0.000 n=10+9)
Scan/MultinodeCockroach/count=100/limit=100-24                    1.12ms ±10%    1.03ms ± 5%   -7.27%  (p=0.000 n=9+10)
Scan/MultinodeCockroach/count=1000/limit=0-24                     2.03ms ± 4%    1.98ms ± 4%   -2.75%  (p=0.043 n=10+10)
Scan/MultinodeCockroach/count=1000/limit=1-24                      959µs ± 2%     931µs ± 4%   -2.99%  (p=0.001 n=8+10)
Scan/MultinodeCockroach/count=1000/limit=10-24                    1.45ms ± 3%    0.97ms ± 3%  -33.10%  (p=0.000 n=10+10)
Scan/MultinodeCockroach/count=1000/limit=100-24                   1.55ms ± 3%    1.07ms ± 4%  -31.14%  (p=0.000 n=10+10)
Scan/MultinodeCockroach/count=10000/limit=0-24                    7.77ms ± 5%    7.74ms ± 3%     ~     (p=0.796 n=10+10)
Scan/MultinodeCockroach/count=10000/limit=1-24                     954µs ± 4%     952µs ± 4%     ~     (p=0.853 n=10+10)
Scan/MultinodeCockroach/count=10000/limit=10-24                   1.42ms ± 3%    0.98ms ± 4%  -31.07%  (p=0.000 n=10+10)
Scan/MultinodeCockroach/count=10000/limit=100-24                  1.58ms ± 5%    1.12ms ± 4%  -29.26%  (p=0.000 n=10+10)
ScanFilter/Cockroach/count1=25/count2=400/limit=1-24               568µs ± 2%     559µs ± 1%   -1.66%  (p=0.010 n=10+9)
ScanFilter/Cockroach/count1=25/count2=400/limit=10-24              595µs ± 2%     616µs ± 6%   +3.44%  (p=0.002 n=10+10)
ScanFilter/Cockroach/count1=25/count2=400/limit=50-24             2.33ms ± 3%    2.38ms ± 2%   +2.39%  (p=0.003 n=10+9)
ScanFilter/MultinodeCockroach/count1=25/count2=400/limit=1-24      905µs ± 2%     915µs ± 3%     ~     (p=0.315 n=8+10)
ScanFilter/MultinodeCockroach/count1=25/count2=400/limit=10-24     960µs ± 6%     980µs ± 4%     ~     (p=0.123 n=10+10)
ScanFilter/MultinodeCockroach/count1=25/count2=400/limit=50-24    3.47ms ± 6%    3.31ms ± 4%   -4.56%  (p=0.001 n=10+9)
VecSkipScan/Bench_scan_with_skip-24                               1.43ms ± 1%    1.45ms ± 1%   +0.88%  (p=0.011 n=10+10)

name                                                            old alloc/op   new alloc/op   delta
Scan/Cockroach/count=1/limit=0-24                                 24.9kB ± 2%    24.9kB ± 2%     ~     (p=0.863 n=9+9)
Scan/Cockroach/count=1/limit=1-24                                 24.6kB ± 1%    24.6kB ± 1%     ~     (p=0.798 n=8+8)
Scan/Cockroach/count=1/limit=10-24                                24.6kB ± 1%    25.0kB ± 4%   +1.47%  (p=0.000 n=9+8)
Scan/Cockroach/count=1/limit=100-24                               24.7kB ± 1%    26.4kB ± 1%   +7.04%  (p=0.000 n=9+9)
Scan/Cockroach/count=10/limit=0-24                                25.8kB ± 1%    25.8kB ± 1%     ~     (p=0.399 n=9+9)
Scan/Cockroach/count=10/limit=1-24                                24.6kB ± 1%    24.7kB ± 1%     ~     (p=0.140 n=10+8)
Scan/Cockroach/count=10/limit=10-24                               26.0kB ± 1%    25.5kB ± 1%   -2.06%  (p=0.000 n=8+9)
Scan/Cockroach/count=10/limit=100-24                              25.8kB ± 2%    27.1kB ± 1%   +4.98%  (p=0.000 n=9+8)
Scan/Cockroach/count=100/limit=0-24                               33.0kB ± 1%    33.0kB ± 1%     ~     (p=0.673 n=8+9)
Scan/Cockroach/count=100/limit=1-24                               24.5kB ± 1%    24.5kB ± 1%     ~     (p=0.574 n=8+8)
Scan/Cockroach/count=100/limit=10-24                              36.9kB ± 2%    25.5kB ± 1%  -30.88%  (p=0.000 n=9+8)
Scan/Cockroach/count=100/limit=100-24                             33.1kB ± 1%    31.9kB ± 1%   -3.62%  (p=0.000 n=8+8)
Scan/Cockroach/count=1000/limit=0-24                              97.7kB ± 0%    97.6kB ± 1%     ~     (p=0.724 n=9+8)
Scan/Cockroach/count=1000/limit=1-24                              24.6kB ± 2%    24.6kB ± 1%     ~     (p=0.505 n=8+8)
Scan/Cockroach/count=1000/limit=10-24                             65.9kB ± 1%    25.6kB ± 3%  -61.12%  (p=0.000 n=9+9)
Scan/Cockroach/count=1000/limit=100-24                            73.1kB ± 1%    32.0kB ± 1%  -56.20%  (p=0.000 n=9+9)
Scan/Cockroach/count=10000/limit=0-24                              830kB ± 0%     835kB ± 1%   +0.59%  (p=0.001 n=9+9)
Scan/Cockroach/count=10000/limit=1-24                             24.6kB ± 0%    24.6kB ± 1%     ~     (p=0.908 n=8+9)
Scan/Cockroach/count=10000/limit=10-24                            65.9kB ± 1%    26.0kB ± 8%  -60.57%  (p=0.000 n=8+9)
Scan/Cockroach/count=10000/limit=100-24                           73.4kB ± 1%    32.1kB ± 1%  -56.25%  (p=0.000 n=8+9)
Scan/MultinodeCockroach/count=1/limit=0-24                        78.9kB ± 8%    73.5kB ± 2%   -6.85%  (p=0.000 n=9+10)
Scan/MultinodeCockroach/count=1/limit=1-24                        76.5kB ± 4%    73.1kB ± 2%   -4.47%  (p=0.000 n=9+10)
Scan/MultinodeCockroach/count=1/limit=10-24                       76.4kB ± 2%    73.9kB ± 4%   -3.27%  (p=0.006 n=8+9)
Scan/MultinodeCockroach/count=1/limit=100-24                      74.2kB ± 4%    75.6kB ± 3%     ~     (p=0.122 n=8+10)
Scan/MultinodeCockroach/count=10/limit=0-24                       84.6kB ± 1%    85.0kB ± 4%     ~     (p=0.959 n=8+8)
Scan/MultinodeCockroach/count=10/limit=1-24                       73.7kB ± 4%    73.1kB ± 1%     ~     (p=0.236 n=9+8)
Scan/MultinodeCockroach/count=10/limit=10-24                      84.8kB ± 1%    77.7kB ± 1%   -8.33%  (p=0.000 n=8+9)
Scan/MultinodeCockroach/count=10/limit=100-24                     96.3kB ±32%    79.8kB ± 2%  -17.17%  (p=0.000 n=10+8)
Scan/MultinodeCockroach/count=100/limit=0-24                       108kB ± 1%     107kB ± 1%     ~     (p=0.546 n=9+9)
Scan/MultinodeCockroach/count=100/limit=1-24                      73.1kB ± 2%    73.4kB ± 1%     ~     (p=0.328 n=8+8)
Scan/MultinodeCockroach/count=100/limit=10-24                      105kB ±23%      78kB ± 1%  -25.88%  (p=0.000 n=10+8)
Scan/MultinodeCockroach/count=100/limit=100-24                     108kB ± 3%      88kB ± 1%  -18.28%  (p=0.000 n=9+8)
Scan/MultinodeCockroach/count=1000/limit=0-24                      251kB ± 2%     252kB ± 1%     ~     (p=0.382 n=8+8)
Scan/MultinodeCockroach/count=1000/limit=1-24                     74.6kB ± 3%    73.5kB ± 1%   -1.50%  (p=0.021 n=8+9)
Scan/MultinodeCockroach/count=1000/limit=10-24                     128kB ± 2%      79kB ± 4%  -38.24%  (p=0.000 n=9+9)
Scan/MultinodeCockroach/count=1000/limit=100-24                    151kB ± 1%      89kB ± 2%  -41.05%  (p=0.000 n=8+8)
Scan/MultinodeCockroach/count=10000/limit=0-24                    1.36MB ± 1%    1.53MB ±23%     ~     (p=0.055 n=8+10)
Scan/MultinodeCockroach/count=10000/limit=1-24                    88.9kB ±45%    77.3kB ±13%     ~     (p=0.079 n=9+10)
Scan/MultinodeCockroach/count=10000/limit=10-24                    128kB ± 1%      80kB ± 4%  -37.40%  (p=0.000 n=8+8)
Scan/MultinodeCockroach/count=10000/limit=100-24                   154kB ± 9%      90kB ± 1%  -41.82%  (p=0.000 n=9+8)
ScanFilter/Cockroach/count1=25/count2=400/limit=1-24              48.2kB ± 6%    48.0kB ± 6%     ~     (p=0.730 n=9+9)
ScanFilter/Cockroach/count1=25/count2=400/limit=10-24             50.6kB ± 1%    50.5kB ± 1%     ~     (p=0.505 n=8+8)
ScanFilter/Cockroach/count1=25/count2=400/limit=50-24              325kB ± 0%     325kB ± 0%     ~     (p=0.297 n=9+9)
ScanFilter/MultinodeCockroach/count1=25/count2=400/limit=1-24     68.8kB ± 3%    71.8kB ± 8%     ~     (p=0.114 n=8+9)
ScanFilter/MultinodeCockroach/count1=25/count2=400/limit=10-24    84.4kB ±26%    85.4kB ±34%     ~     (p=0.863 n=9+9)
ScanFilter/MultinodeCockroach/count1=25/count2=400/limit=50-24    1.06MB ± 5%    1.04MB ± 1%     ~     (p=0.093 n=9+8)
VecSkipScan/Bench_scan_with_skip-24                                186kB ± 1%     186kB ± 1%     ~     (p=1.000 n=9+9)

name                                                            old allocs/op  new allocs/op  delta
Scan/Cockroach/count=1/limit=0-24                                    253 ± 1%       253 ± 1%     ~     (p=0.844 n=9+8)
Scan/Cockroach/count=1/limit=1-24                                    247 ± 1%       247 ± 1%     ~     (p=1.000 n=8+8)
Scan/Cockroach/count=1/limit=10-24                                   251 ± 0%       254 ± 3%   +1.24%  (p=0.011 n=9+10)
Scan/Cockroach/count=1/limit=100-24                                  251 ± 0%       252 ± 1%     ~     (p=1.000 n=9+9)
Scan/Cockroach/count=10/limit=0-24                                   273 ± 0%       272 ± 0%     ~     (p=0.444 n=9+9)
Scan/Cockroach/count=10/limit=1-24                                   248 ± 1%       249 ± 0%   +0.24%  (p=0.034 n=10+7)
Scan/Cockroach/count=10/limit=10-24                                  274 ± 1%       258 ± 1%   -5.91%  (p=0.000 n=10+9)
Scan/Cockroach/count=10/limit=100-24                                 274 ± 0%       259 ± 0%   -5.70%  (p=0.000 n=9+8)
Scan/Cockroach/count=100/limit=0-24                                  391 ± 0%       392 ± 0%     ~     (p=0.138 n=9+7)
Scan/Cockroach/count=100/limit=1-24                                  249 ± 0%       250 ± 2%     ~     (p=0.850 n=8+9)
Scan/Cockroach/count=100/limit=10-24                                 339 ± 1%       260 ± 0%  -23.21%  (p=0.000 n=8+9)
Scan/Cockroach/count=100/limit=100-24                                392 ± 0%       352 ± 0%  -10.21%  (p=0.000 n=9+8)
Scan/Cockroach/count=1000/limit=0-24                               2.07k ± 0%     2.07k ± 0%     ~     (p=0.900 n=9+9)
Scan/Cockroach/count=1000/limit=1-24                                 249 ± 0%       250 ± 1%     ~     (p=0.309 n=8+8)
Scan/Cockroach/count=1000/limit=10-24                                342 ± 1%       261 ± 1%  -23.86%  (p=0.000 n=9+9)
Scan/Cockroach/count=1000/limit=100-24                               461 ± 0%       354 ± 0%  -23.13%  (p=0.000 n=9+8)
Scan/Cockroach/count=10000/limit=0-24                              20.1k ± 0%     20.1k ± 0%     ~     (p=0.069 n=9+8)
Scan/Cockroach/count=10000/limit=1-24                                249 ± 0%       250 ± 0%     ~     (p=0.444 n=9+9)
Scan/Cockroach/count=10000/limit=10-24                               344 ± 0%       262 ± 3%  -23.92%  (p=0.000 n=8+9)
Scan/Cockroach/count=10000/limit=100-24                              464 ± 1%       355 ± 0%  -23.42%  (p=0.000 n=8+9)
Scan/MultinodeCockroach/count=1/limit=0-24                           800 ± 1%       795 ± 1%     ~     (p=0.105 n=8+10)
Scan/MultinodeCockroach/count=1/limit=1-24                           792 ± 3%       787 ± 1%     ~     (p=0.250 n=9+10)
Scan/MultinodeCockroach/count=1/limit=10-24                          793 ± 1%       795 ± 3%     ~     (p=0.761 n=8+9)
Scan/MultinodeCockroach/count=1/limit=100-24                         792 ± 2%       795 ± 1%     ~     (p=0.131 n=8+9)
Scan/MultinodeCockroach/count=10/limit=0-24                          906 ± 1%       906 ± 1%     ~     (p=0.937 n=8+8)
Scan/MultinodeCockroach/count=10/limit=1-24                          795 ± 3%       792 ± 3%     ~     (p=0.325 n=9+9)
Scan/MultinodeCockroach/count=10/limit=10-24                         910 ± 1%       832 ± 1%   -8.48%  (p=0.000 n=8+8)
Scan/MultinodeCockroach/count=10/limit=100-24                        931 ± 7%       836 ± 1%  -10.23%  (p=0.000 n=9+8)
Scan/MultinodeCockroach/count=100/limit=0-24                       1.12k ± 1%     1.12k ± 1%     ~     (p=0.620 n=9+9)
Scan/MultinodeCockroach/count=100/limit=1-24                         790 ± 1%       792 ± 1%     ~     (p=0.148 n=9+8)
Scan/MultinodeCockroach/count=100/limit=10-24                        983 ± 1%       836 ± 2%  -14.95%  (p=0.000 n=8+9)
Scan/MultinodeCockroach/count=100/limit=100-24                     1.13k ± 2%     0.93k ± 1%  -17.62%  (p=0.000 n=9+8)
Scan/MultinodeCockroach/count=1000/limit=0-24                      2.92k ± 2%     2.92k ± 1%     ~     (p=0.527 n=8+8)
Scan/MultinodeCockroach/count=1000/limit=1-24                        799 ± 2%       793 ± 1%     ~     (p=0.054 n=8+9)
Scan/MultinodeCockroach/count=1000/limit=10-24                     1.00k ± 2%     0.84k ± 1%  -16.27%  (p=0.000 n=9+8)
Scan/MultinodeCockroach/count=1000/limit=100-24                    1.23k ± 2%     0.93k ± 1%  -23.98%  (p=0.000 n=9+9)
Scan/MultinodeCockroach/count=10000/limit=0-24                     21.3k ± 0%     21.5k ± 4%     ~     (p=0.114 n=8+9)
Scan/MultinodeCockroach/count=10000/limit=1-24                       822 ±10%       797 ± 4%   -3.08%  (p=0.018 n=8+9)
Scan/MultinodeCockroach/count=10000/limit=10-24                    1.00k ± 1%     0.84k ± 2%  -15.71%  (p=0.000 n=8+8)
Scan/MultinodeCockroach/count=10000/limit=100-24                   1.22k ± 2%     0.94k ± 1%  -23.32%  (p=0.000 n=9+8)
ScanFilter/Cockroach/count1=25/count2=400/limit=1-24                 456 ± 0%       456 ± 0%     ~     (p=0.699 n=8+8)
ScanFilter/Cockroach/count1=25/count2=400/limit=10-24                520 ± 0%       478 ± 0%   -8.13%  (p=0.000 n=9+8)
ScanFilter/Cockroach/count1=25/count2=400/limit=50-24                788 ± 0%       788 ± 1%     ~     (p=0.606 n=10+9)
ScanFilter/MultinodeCockroach/count1=25/count2=400/limit=1-24        717 ± 1%       720 ± 1%     ~     (p=0.398 n=8+8)
ScanFilter/MultinodeCockroach/count1=25/count2=400/limit=10-24       803 ± 9%       762 ± 9%   -5.02%  (p=0.018 n=9+9)
ScanFilter/MultinodeCockroach/count1=25/count2=400/limit=50-24     1.21k ± 2%     1.21k ± 2%     ~     (p=0.870 n=8+9)
VecSkipScan/Bench_scan_with_skip-24                                  687 ± 0%       686 ± 0%     ~     (p=0.267 n=8+8)

Copy link
Member Author

@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! 0 of 0 LGTMs obtained (waiting on @jordanlewis)


pkg/sql/colfetcher/cfetcher.go, line 1073 at r1 (raw file):

Previously, jordanlewis (Jordan Lewis) wrote…

nit: I see you changed the "inaccurate" >= to == in both places. I was taught as a young programmer to prefer using >= for conditions like this because it limits the effects of other broken code in the system. I agree that I can see no reason why the index would exceed the capacity, and indeed many things worse could go wrong, but I think it's a good habit to keep a >= to "limit the blast radius" in case other bugs creep in.

Fair point, restored.

Copy link
Member

@jordanlewis jordanlewis 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! 1 of 0 LGTMs obtained (waiting on @yuzefovich)


pkg/sql/colfetcher/cfetcher.go, line 682 at r2 (raw file):

	rf.machine.limitHint = int(limitHint)
	rf.machine.state[0] = stateResetBatch
	rf.machine.state[1] = stateInitFetch

Why the change to have the state machine do the reset? I'm okay with it but just curious.


pkg/sql/colfetcher/cfetcher.go, line 1087 at r2 (raw file):

				// Note that limitHint might become negative at which point we
				// will start ignoring it.
				rf.machine.limitHint -= rf.machine.rowIdx

This could conceivably overflow if we did a int63-sized scan, which I guess is not really possible, but why bother continuing to track this?

Copy link
Member Author

@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! 1 of 0 LGTMs obtained (waiting on @jordanlewis)


pkg/sql/colfetcher/cfetcher.go, line 682 at r2 (raw file):

Previously, jordanlewis (Jordan Lewis) wrote…

Why the change to have the state machine do the reset? I'm okay with it but just curious.

I included the reasoning in the commit message. Without this (or some other) change, we call resetBatch in cFetcher.Init which is called in initCRowFetcher which is called in NewColBatchScan; but at that time the limit hint is not yet set - that is done in cFetcher.StartScan which is called in ColBatchScan.Init; thus, we could allocate a batch of capacity 1 first, and only then we would use the limit hint to size the batch.


pkg/sql/colfetcher/cfetcher.go, line 1087 at r2 (raw file):

Previously, jordanlewis (Jordan Lewis) wrote…

This could conceivably overflow if we did a int63-sized scan, which I guess is not really possible, but why bother continuing to track this?

My thinking was that unconditional subtraction is faster than having a conditional in place, and the code ends being cleaner overall. Yeah, I'm not worried about the underflowing, but happy to change this code if you see a clean way to implement it. I came up with something like

if rf.machine.limitHint > 0 {
  // Update the limit hint to track the expected remaining rows to
  // be fetched.
  if rf.machine.limitHint >= rf.machine.rowIdx {
    rf.machine.limitHint -= rf.machine.rowIdx
  } else {
    rf.machine.limitHint = 0
  }
}

We recently merged a couple of commits to take advantage of the
optimizer-driven estimated row count that will be output by the cfetcher
and of the limit hint when available. However, one scenario was
overlooked - if there is no estimate but the limit hint is available, we
don't use the latter to size the original batch, and now we do.

As an additional optimization this commit refactors the initial state of
the state machine so that we didn't allocate the output batch in the
constructor of the ColBatchScan but do it only on the very first call
to `NextBatch`. This was prompted by the fact that the limit hint is
currently only set in `StartScan` which is called in
`ColBatchScan.Init`, so without some changes, in the case when the limit
hint can be used to size the batch we would allocate a redundant batch
of size one in `NewColBatchScan`. I also think that the new initial
state of the state machine makes a bit more sense to me.

Additionally, this commit generalizes the logic to keep track of the
limit hint as "remaining rows to be fetched."

Release note: None (this is a follow-up to a recently merged commit that
had a release note)
@yuzefovich
Copy link
Member Author

I think we'll want to backport this to 21.1.0, so I'll go ahead and merge.

TFTR!

bors r+

@craig
Copy link
Contributor

craig bot commented Apr 3, 2021

Build succeeded:

@craig craig bot merged commit 0e30063 into cockroachdb:master Apr 3, 2021
@yuzefovich yuzefovich deleted the use-limit-hint branch April 3, 2021 01:06
@rytaft
Copy link
Collaborator

rytaft commented Apr 5, 2021


pkg/sql/colfetcher/cfetcher.go, line 328 at r2 (raw file):

		// ResetMaybeReallocate will chop it down.
		minCapacity = rf.machine.limitHint
	} else {

Sorry for the late drive-by, but in a future commit you could consider adding another case for soft limit as follows:

	 if rf.estimatedRowCount == 0 && rf.machine.limitHint > 0 {
		// If we don't have an estimate but we have a limit hint, use the hint
		// to size the batch. Note that if it exceeds coldata.BatchSize,
		// ResetMaybeReallocate will chop it down.
		minCapacity = rf.machine.limitHint
	} else rf.machine.limitHint > 0 && rf.machine.limitHint < rf.estimatedRowCount {
		// This is a case where we have a soft limit.
		minCapacity = rf.machine.limitHint
	} else {
		...
	}		

Not sure if this is always desirable, but could improve performance in some cases.

@rytaft
Copy link
Collaborator

rytaft commented Apr 5, 2021


pkg/sql/colfetcher/cfetcher.go, line 328 at r2 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

Sorry for the late drive-by, but in a future commit you could consider adding another case for soft limit as follows:

	 if rf.estimatedRowCount == 0 && rf.machine.limitHint > 0 {
		// If we don't have an estimate but we have a limit hint, use the hint
		// to size the batch. Note that if it exceeds coldata.BatchSize,
		// ResetMaybeReallocate will chop it down.
		minCapacity = rf.machine.limitHint
	} else rf.machine.limitHint > 0 && rf.machine.limitHint < rf.estimatedRowCount {
		// This is a case where we have a soft limit.
		minCapacity = rf.machine.limitHint
	} else {
		...
	}		

Not sure if this is always desirable, but could improve performance in some cases.

(also, not sure if soft limit is actually used to set rf.machine.limitHint, that would be a prerequisite for this change...)

Copy link
Member Author

@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! 1 of 0 LGTMs obtained


pkg/sql/colfetcher/cfetcher.go, line 328 at r2 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

(also, not sure if soft limit is actually used to set rf.machine.limitHint, that would be a prerequisite for this change...)

Thanks! I'll open up a PR to address this.

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