Optimize RowNumberReader to be 8x faster - #9680
Merged
Merged
Conversation
- Instead of storing an iterator over individual row numbers, we now store a vec of ranges.
- These ranges are not materialized into a fully array until needed.
- `read_records` was previously linear in terms of number of rows read.
- Now it's close to constant since one batch (8192 rows) usually is satisfied by one row range (which comes from a row group).
- Same for `skip_records`
- `consume_batch` is still linear in terms of rows, but it is faster since it can pre-allocate the output vec.
- Previously, the `Flatten` iter would have prevented it pre-allocating (it's not an `ExactSizeIterator`).
I do not have micro-benchmark numbers for this change, but I have noticed a 3x improvement in execution time for an internal query.
Contributor
|
Thank you @Samyak2 -- this looks good to me. |
alamb
approved these changes
Apr 13, 2026
Rich-T-kid
pushed a commit
to Rich-T-kid/arrow-rs
that referenced
this pull request
Jun 2, 2026
# Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax.
-->
- Closes None
# Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->
We internally found `RowNumberReader` to be a hot path in some of our
queries. Flamegraphs showed ~70% of the cpu time taken by methods in
`RowNumberReader`.
These can be made an order of magnitude faster (benchmarks below).
# What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->
- Instead of storing an iterator over individual row numbers, we now
store a vec of ranges.
- These ranges are not materialized into a fully array until needed.
- `read_records` was previously linear in terms of number of rows read.
- Now it's close to constant since one batch (8192 rows) usually is
satisfied by one row range (which comes from a row group).
- Same for `skip_records`
- `consume_batch` is still linear in terms of rows, but it is faster
since it can pre-allocate the output vec.
- Previously, the `Flatten` iter would have prevented it pre-allocating
(it's not an `ExactSizeIterator`).
# Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->
- Yes, added more unit tests
- I have some benchmarks at Samyak2#1,
but they need `RowNumberReader` to be pub, so I've not included them
here
Before:
```
Benchmarking row_number_read_consume: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 7.0s, enable flat sampling, or reduce sample count to 50.
row_number_read_consume time: [1.3915 ms 1.3967 ms 1.4035 ms]
Found 11 outliers among 100 measurements (11.00%)
1 (1.00%) low severe
1 (1.00%) low mild
5 (5.00%) high mild
4 (4.00%) high severe
row_number_skip_and_read
time: [716.61 µs 718.14 µs 719.91 µs]
Found 6 outliers among 100 measurements (6.00%)
1 (1.00%) low severe
1 (1.00%) low mild
3 (3.00%) high mild
1 (1.00%) high severe
```
After:
```
row_number_read_consume time: [159.00 µs 160.81 µs 162.68 µs]
change: [−88.900% −88.721% −88.505%] (p = 0.00 < 0.05)
Performance has improved.
Found 3 outliers among 100 measurements (3.00%)
1 (1.00%) low mild
2 (2.00%) high mild
row_number_skip_and_read
time: [79.057 µs 79.924 µs 80.846 µs]
change: [−89.025% −88.865% −88.712%] (p = 0.00 < 0.05)
Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
2 (2.00%) high mild
```
Ranging from **8.6x to 8.9x faster**!
# Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
If there are any breaking changes to public APIs, please call them out.
-->
No
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
We internally found
RowNumberReaderto be a hot path in some of our queries. Flamegraphs showed ~70% of the cpu time taken by methods inRowNumberReader.These can be made an order of magnitude faster (benchmarks below).
What changes are included in this PR?
read_recordswas previously linear in terms of number of rows read.skip_recordsconsume_batchis still linear in terms of rows, but it is faster since it can pre-allocate the output vec.Flatteniter would have prevented it pre-allocating (it's not anExactSizeIterator).Are these changes tested?
RowNumberReaderto be pub, so I've not included them hereBefore:
After:
Ranging from 8.6x to 8.9x faster!
Are there any user-facing changes?
No