Skip to content

Commit f26ece3

Browse files
groeneaiclaude
andcommitted
Keep one-column sorted-run searches as fast as before SortedKeyRuns
parallel_full_sorting_merge_join #1 (full_sorting_merge on one UInt64 key) and window_function_with_order #3 got 10-20% slower: per row, the join called an out-of-line findKeyRangeEndAssumeSorted, and the window code called out-of-line copies of the SortedKeyRuns overloads and WindowTransform::findPeerRunEnd, where master inlined the stateless search. - force-inline findKeyRangeEndAssumeSorted, the SortedKeyRuns overloads in SortCursor.h and findPeerRunEnd; - the SortedKeyRuns overloads take the stateless search for one-column keys; - the peer-group marking of SlidingIndexes (new in master) uses one SortedKeyRuns per block instead of resetting it for every partition, which called vector::assign per partition. The partitions are searched in order and none starts inside an earlier one, so no stale run can be reused; the reset contract is stated precisely. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e5aef2b commit f26ece3

4 files changed

Lines changed: 19 additions & 12 deletions

File tree

‎src/Columns/findEqualRangeEndAssumeSorted.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ ALWAYS_INLINE Int64 compareTrackAtImpl(
153153
* column `i` is sorted there because `bound` is the end of the run of columns `0..i-1`.
154154
*/
155155
template <typename Search>
156-
size_t findKeyRangeEndAssumeSorted(size_t key_size, size_t begin, size_t end, Search && search)
156+
ALWAYS_INLINE size_t findKeyRangeEndAssumeSorted(size_t key_size, size_t begin, size_t end, Search && search)
157157
{
158158
size_t run_end = end;
159159
for (size_t i = 0; i < key_size; ++i)
@@ -165,9 +165,10 @@ size_t findKeyRangeEndAssumeSorted(size_t key_size, size_t begin, size_t end, Se
165165
return run_end;
166166
}
167167

168-
/** Same result as findKeyRangeEndAssumeSorted, for callers that search the runs of one range one after another.
168+
/** Same result as `findKeyRangeEndAssumeSorted`, for callers that search the runs of one range one after another.
169169
* Remembers the last run found for each key prefix: a search from a row inside it ends at the same row, so a column
170-
* is searched again only when the search leaves its prefix's run. Call reset() when the columns or `end` change.
170+
* is searched again only when the search leaves its prefix's run. Call `reset` when the columns change, or when `end`
171+
* changes and the next search may start before the previous `end`.
171172
*/
172173
class SortedKeyRuns
173174
{

‎src/Core/SortCursor.h‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,21 +1027,25 @@ size_t getEqualRangeEndAssumeSorted(const TColumns & columns, const TSortDescrip
10271027
descr.size(), begin, end, [&](size_t i) -> decltype(auto) { return columns[i]; }, [&](size_t i) { return descr[i].nulls_direction; });
10281028
}
10291029

1030-
/** Same as the overloads above, for a caller that finds the runs of one range one after another (see SortedKeyRuns).
1031-
* `runs` must be sized to the number of key columns and reset when the columns or `end` change.
1030+
/** Same as the overloads above, for a caller that finds the runs of one range one after another (see `SortedKeyRuns`).
1031+
* `runs` must be sized to the number of key columns and reset as `SortedKeyRuns` requires.
10321032
*/
10331033
template <typename TColumns>
1034-
size_t getEqualRangeEndAssumeSorted(SortedKeyRuns & runs, const TColumns & columns, size_t begin, size_t end, int nan_direction_hint)
1034+
ALWAYS_INLINE size_t getEqualRangeEndAssumeSorted(SortedKeyRuns & runs, const TColumns & columns, size_t begin, size_t end, int nan_direction_hint)
10351035
{
1036+
if (columns.size() < 2)
1037+
return getEqualRangeEndAssumeSorted(columns, begin, end, nan_direction_hint);
10361038
chassert(runs.keySize() == columns.size());
10371039
return runs.findRunEnd(
10381040
begin, end, [&](size_t i, size_t from, size_t bound) { return columns[i]->getEqualRangeEndAssumeSorted(from, bound, nan_direction_hint); });
10391041
}
10401042

10411043
template <typename TColumns>
1042-
size_t getEqualRangeEndAssumeSorted(
1044+
ALWAYS_INLINE size_t getEqualRangeEndAssumeSorted(
10431045
SortedKeyRuns & runs, const TColumns & columns, const std::vector<size_t> & positions, size_t begin, size_t end, int nan_direction_hint)
10441046
{
1047+
if (positions.size() < 2)
1048+
return getEqualRangeEndAssumeSorted(columns, positions, begin, end, nan_direction_hint);
10451049
chassert(runs.keySize() == positions.size());
10461050
return runs.findRunEnd(
10471051
begin,
@@ -1051,8 +1055,10 @@ size_t getEqualRangeEndAssumeSorted(
10511055

10521056
template <typename TColumns, typename TSortDescription>
10531057
requires requires (const TSortDescription & d) { d.size(); d[0].nulls_direction; }
1054-
size_t getEqualRangeEndAssumeSorted(SortedKeyRuns & runs, const TColumns & columns, const TSortDescription & descr, size_t begin, size_t end)
1058+
ALWAYS_INLINE size_t getEqualRangeEndAssumeSorted(SortedKeyRuns & runs, const TColumns & columns, const TSortDescription & descr, size_t begin, size_t end)
10551059
{
1060+
if (descr.size() < 2)
1061+
return getEqualRangeEndAssumeSorted(columns, descr, begin, end);
10561062
chassert(runs.keySize() == descr.size());
10571063
return runs.findRunEnd(
10581064
begin,

‎src/Processors/Transforms/Window/SlidingIndexes.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ bool haveSameKeys(const Columns & lhs, size_t lhs_row, const Columns & rhs, size
2323
void markKeyChangesInRange(
2424
const Columns & columns, const std::vector<size_t> & key_indices, size_t begin, size_t end, SortedKeyRuns & key_runs, std::vector<bool> & changes)
2525
{
26-
key_runs.reset(key_indices.size());
2726
size_t next_change = getEqualRangeEndAssumeSorted(key_runs, columns, key_indices, begin, end, /*nan_direction_hint=*/1);
2827
while (next_change < end)
2928
{
@@ -36,7 +35,7 @@ std::vector<bool> markPartitionStarts(const Columns & columns, size_t rows_count
3635
{
3736
std::vector<bool> starts(rows_count, false);
3837
starts[0] = !previous_partition_key || !haveSameKeys(*previous_partition_key, 0, columns, 0, partition_by_indices);
39-
SortedKeyRuns key_runs;
38+
SortedKeyRuns key_runs(partition_by_indices.size());
4039
markKeyChangesInRange(columns, partition_by_indices, 0, rows_count, key_runs, starts);
4140
return starts;
4241
}
@@ -58,7 +57,8 @@ std::vector<bool> markPeerGroupStarts(
5857
std::vector<bool> starts = partition_starts;
5958
starts[0] = starts[0] || !previous_order_key || !haveSameKeys(*previous_order_key, 0, columns, 0, order_by_indices);
6059

61-
SortedKeyRuns key_runs;
60+
/// The partitions are searched in order and none starts inside an earlier one, so their runs need no reset.
61+
SortedKeyRuns key_runs(order_by_indices.size());
6262
size_t partition_begin = 0;
6363
while (partition_begin < rows_count)
6464
{

‎src/Processors/Transforms/WindowTransform.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ void WindowTransform::advanceFrameEndRangeOffset()
452452
frame_ended = partition.bounds().fully_visible;
453453
}
454454

455-
Int64 WindowTransform::findPeerRunEnd(Int64 block, Int64 begin, Int64 end)
455+
ALWAYS_INLINE Int64 WindowTransform::findPeerRunEnd(Int64 block, Int64 begin, Int64 end)
456456
{
457457
const auto & columns = blocks.blockAt(block).materialized_columns;
458458
if (params.order_by_indices.size() < 2)

0 commit comments

Comments
 (0)