Repository navigation
Prefetching in aggregation - #39304
Conversation
|
Prefetch usually helps with single-thread scenarios but does not help with multiple threads, because the CPU is already doing as many loads from memory in parallel as possible. |
|
If the implementation will help at least in some scenarios and does not lead to degradations, it's worth merging. |
d01070f to
4def995
Compare
4def995 to
08b80cc
Compare
8aeeb85 to
ad68686
Compare
0200b93 to
2526c74
Compare
| @@ -1,4 +1,5 @@ | |||
| <test> | |||
| <!-- --> | |||
There was a problem hiding this comment.
these comments are added to run all queries in perf tests. will delete.
| static constexpr auto assumed_load_latency_ns = 100; | ||
| static constexpr auto just_coefficient = 4; | ||
| const auto single_iteration_latency = std::max<double>(1.0L * watch.elapsedNanoseconds() / iterations_to_measure, 1); | ||
| return std::clamp<size_t>( |
There was a problem hiding this comment.
I haven't tested this formula very thoroughly, but since the function should be linear two points should be enough. these two points were big linux vm with intel processor and MacBook with m1.
| M(UInt64, max_size_to_preallocate_for_aggregation, 10'000'000, "For how many elements it is allowed to preallocate space in all hash tables in total before aggregation", 0) \ | ||
| \ | ||
| M(Bool, kafka_disable_num_consumers_limit, false, "Disable limit on kafka_num_consumers that depends on the number of available CPU cores", 0) \ | ||
| M(Bool, enable_sortware_prefetch_in_aggregation, true, "Enable use of software prefetch in aggregation", 0) \ |
| #endif | ||
|
|
||
| /// 1MB looks like a reasonable estimate of L2 size. 4 is empirical constant. | ||
| return 4 * std::max<size_t>(l2_size, 1024 * 1024); |
There was a problem hiding this comment.
Why std::max?
PS. L2 cache is typically 256 KiB per core on Intel CPU.
Btw, what sysconf returns? The per-core size or total size?
There was a problem hiding this comment.
On my CPU it is 512 KiB/core, 32 MB in total.
There was a problem hiding this comment.
Let's replace 1024 * 1024 to 256 * 1024.
There was a problem hiding this comment.
Why std::max?
if HT fits in l2, prefetching will likely harm perf, so the threshold should be greater than l2 size.
Btw, what sysconf returns? The per-core size or total size?
per-core
on my vm it is 1MB. my point was that it is safer to choose greater value than smaller. but we could use 256KB, arrow use it as default for l2.
|
Please add a changelog and edit the category to "Performance Improvement". |
| if (i + prefetch_look_ahead < row_end) | ||
| { | ||
| auto && key_holder = state.getKeyHolder(i + prefetch_look_ahead, *aggregates_pool); | ||
| method.data.prefetch(std::move(key_holder)); |
There was a problem hiding this comment.
Hi @nickitat , it seems that the prefetching operations add some overhead due to double serialization and hashing.
Here, getKeyHolder would serialize the columns into a key; method.data.prefetch() would compute the hash value of the key. In the next line, state.emplaceKey does these two steps again.
Would it be better to cache the serialized key and hash value? Thanks!
There was a problem hiding this comment.
for this reason we have special trait in HashMethod-s that tells, if these operations are cheap:
const bool prefetch = Method::State::has_cheap_key_calculation && params.enable_prefetch
&& (method.data.getBufferSizeInBytes() > min_bytes_for_prefetch);another consideration is that prefetching gives equal speedup regardless of the key complexity and the more costly to compute keys the less noticeable will be perf gain from prefetching, so we not really loose much if we just disable the optimisation for group by with complex keys.
that being said cache keys makes perfect sense. it wasn't implemented mostly because I didn't want to introduce some amount of fairly ugly code. it would be required because for example some key holders are not copiable (e.g. ArenaKeyHolder)
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Software prefetching is used in aggregation to speed up operations with hash tables. Controlled by the setting
enable_software_prefetch_in_aggregation, enabled by default.