Skip to content

Prefetching in aggregation - #39304

Merged
nickitat merged 24 commits into
ClickHouse:masterfrom
nickitat:try_prefetch
Sep 21, 2022
Merged

nickitat merged 24 commits into
ClickHouse:masterfrom
nickitat:try_prefetch

Conversation

@nickitat

@nickitat nickitat commented Jul 17, 2022 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

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.

@robot-ch-test-poll robot-ch-test-poll added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jul 17, 2022
@alexey-milovidov

Copy link
Copy Markdown
Member

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.

@alexey-milovidov

Copy link
Copy Markdown
Member

If the implementation will help at least in some scenarios and does not lead to degradations, it's worth merging.

@nickitat
nickitat force-pushed the try_prefetch branch 3 times, most recently from d01070f to 4def995 Compare August 28, 2022 15:32
@nickitat
nickitat force-pushed the try_prefetch branch 5 times, most recently from 8aeeb85 to ad68686 Compare September 15, 2022 14:50
@@ -1,4 +1,5 @@
<test>
<!-- -->

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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>(

@nickitat nickitat Sep 20, 2022 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@nickitat nickitat changed the title Try prefetching in aggregation Prefetching in aggregation Sep 20, 2022
@nickitat
nickitat marked this pull request as ready for review September 20, 2022 18:29
Comment thread src/Core/Settings.h Outdated
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) \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

sortware -> software

Comment thread src/Interpreters/Aggregator.cpp Outdated
#endif

/// 1MB looks like a reasonable estimate of L2 size. 4 is empirical constant.
return 4 * std::max<size_t>(l2_size, 1024 * 1024);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

On my CPU it is 512 KiB/core, 32 MB in total.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's replace 1024 * 1024 to 256 * 1024.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@alexey-milovidov alexey-milovidov self-assigned this Sep 20, 2022

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@alexey-milovidov

Copy link
Copy Markdown
Member

Please add a changelog and edit the category to "Performance Improvement".

@nickitat nickitat added pr-performance Pull request with some performance improvements and removed pr-not-for-changelog This PR should not be mentioned in the changelog force tests labels Sep 21, 2022
@nickitat
nickitat merged commit 100c055 into ClickHouse:master Sep 21, 2022
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));

@nudles nudles Feb 13, 2023 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants