Skip to content

Improve the performance of serialized aggregation method when involving multiple [nullable] columns#55809

Merged
nickitat merged 4 commits intoClickHouse:masterfrom
amosbird:alt-51399
Mar 1, 2024
Merged

Improve the performance of serialized aggregation method when involving multiple [nullable] columns#55809
nickitat merged 4 commits intoClickHouse:masterfrom
amosbird:alt-51399

Conversation

@amosbird
Copy link
Copy Markdown
Collaborator

@amosbird amosbird commented Oct 19, 2023

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Improve the performance of serialized aggregation method when involving multiple [nullable] columns. This is a general version of #51399 that doesn't compromise on abstraction integrity.

This pull request refactors the IColumn in a manner similar to the IAggregateFunction. It introduces IColumnHelper, which implements methods to devirtualize some calls of IColumn in its final descendants. It is also used to implement common methods, such as serializeValueIntoArena, and place them in the same translation unit to ensure they are properly inlined.

Three variants of serialized aggregation methods have been added: nullable_serialized, prealloc_serialized, and nullable_prealloc_serialized. With the nullable variant, we have devirtualized calls to write nested column values when they are not null. In the prealloc variant, we first calculate the serialized byte size of each row (involving more than one column) in a vectorized way. Then, we allocate continuous memory as a whole and use serializeValueIntoMemory to serialize column values.

The performance numbers are impressive, showing ~2x improvement in speed.

@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-improvement Pull request with some product improvements label Oct 19, 2023
@robot-ch-test-poll1
Copy link
Copy Markdown
Contributor

robot-ch-test-poll1 commented Oct 19, 2023

This is an automated comment for commit a2027db with description of existing statuses. It's updated for the latest CI running

❌ Click here to open a full report in a separate page

Successful checks
Check nameDescriptionStatus
A SyncThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
ClickBenchRuns [ClickBench](https://github.com/ClickHouse/ClickBench/) with instant-attach table✅ success
ClickHouse build checkBuilds ClickHouse in various configurations for use in further steps. You have to fix the builds that fail. Build logs often has enough information to fix the error, but you might have to reproduce the failure locally. The cmake options can be found in the build log, grepping for cmake. Use these options and follow the general build process✅ success
Compatibility checkChecks that clickhouse binary runs on distributions with old libc versions. If it fails, ask a maintainer for help✅ success
Docker keeper imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docker server imageThe check to build and optionally push the mentioned image to docker hub✅ success
Docs checkBuilds and tests the documentation✅ success
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here✅ success
Flaky testsChecks if new added or modified tests are flaky by running them repeatedly, in parallel, with more randomization. Functional tests are run 100 times with address sanitizer, and additional randomization of thread scheduling. Integrational tests are run up to 10 times. If at least once a new test has failed, or was too long, this check will be red. We don't allow flaky tests, read the doc✅ success
Install packagesChecks that the built packages are installable in a clear environment✅ success
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests✅ success
Mergeable CheckChecks if all other necessary checks are successful✅ success
PR CheckThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
Performance ComparisonMeasure changes in query performance. The performance test report is described in detail here. In square brackets are the optional part/total tests✅ success
SQLTestThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
SQLancerFuzzing tests that detect logical bugs with SQLancer tool✅ success
SqllogicRun clickhouse on the sqllogic test set against sqlite and checks that all statements are passed✅ success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors✅ success
Style checkRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success
Unit testsRuns the unit tests for different release types✅ success
Upgrade checkRuns stress tests on server version from last release and then tries to upgrade it to the version from the PR. It checks if the new server can successfully startup without any errors, crashes or sanitizer asserts✅ success
Check nameDescriptionStatus
AST fuzzerRuns randomly generated queries to catch program errors. The build type is optionally given in parenthesis. If it fails, ask a maintainer for help❌ failure
CI runningA meta-check that indicates the running CI. Normally, it's in success or pending state. The failed status indicates some problems with the PR⏳ pending

@liuneng1994
Copy link
Copy Markdown
Contributor

I've seen performance gains due to fewer alloca calls. But arena grows linearly, so I didn't think that arena could be modified to achieve the same purpose. It's hard to understand that an additional function call has such a large fixed overhead.

😢

@amosbird
Copy link
Copy Markdown
Collaborator Author

It's hard to understand that an additional function call has such a large fixed overhead.

It's likely because continuous allocation is heavy. Actually we can do even better by allocating one full row per time. We already have skipSerializedInArena interface which can be used to calculate size before hand. In that case we need the devirtualization technique used in IAggregateFunction. It will be the next step.

@amosbird
Copy link
Copy Markdown
Collaborator Author

Performance is nearly on-par with one exception https://s3.amazonaws.com/clickhouse-test-reports/55809/b8a391e96953feaf6fae1b3a8324750196371f52/performance_comparison_[3_4]/report.html . This small portion should be related to virtualization cost.

@amosbird amosbird changed the title Alternative improvement of serializing nullable column values Improve the performance of serialized aggregation method when involving multiple [nullable] columns Oct 21, 2023
@amosbird amosbird added the pr-performance Pull request with some performance improvements label Oct 21, 2023
@robot-ch-test-poll3 robot-ch-test-poll3 removed the pr-improvement Pull request with some product improvements label Oct 21, 2023
@amosbird
Copy link
Copy Markdown
Collaborator Author

copied-2023-10-22-23_42_20_079

Heh, some unplanned test case is also optimized.

@liuneng1994
Copy link
Copy Markdown
Contributor

liuneng1994 commented Oct 23, 2023

Heh, some unplanned test case is also optimized.

Orz,太强了, Too strong.

@baibaichen
Copy link
Copy Markdown
Contributor

@amosbird anything we can do to speedup this PR ?

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.

I'm not sure it is a good idea to allocate memory externally. the callee should know better how much elements the array should have, isn't it?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Sure. I've updated so that called will do the resize if needed.

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.

I just hope you advanced memory correctly in all cases )

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It should work, or else tests will complain immediately.

Comment on lines 153 to 154
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.

Suggested change
UInt64 * indexes [[maybe_unused]];
UInt64 * next_index [[maybe_unused]];
[[maybe_unused]] UInt64 * indexes = nullptr;
[[maybe_unused]] UInt64 * next_index = nullptr;

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.

maybe it makes sense to create a separate function for this case. it will add copy-paste, but the code would be easier to read IMO

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Sure.

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.

should be removed I guess in favour of l.165-172

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Indeed.

@alexey-milovidov
Copy link
Copy Markdown
Member

@amosbird This looks great, let's merge it!

@zhanglistar
Copy link
Copy Markdown
Contributor

@amosbird Any update? Thanks.

@amosbird
Copy link
Copy Markdown
Collaborator Author

I'll try to make it mergeable in this month. Hopefully it can land in 24Q1.

@robot-ch-test-poll robot-ch-test-poll added the submodule changed At least one submodule changed in this PR. label Feb 25, 2024
@amosbird amosbird removed the submodule changed At least one submodule changed in this PR. label Feb 25, 2024
Copy link
Copy Markdown
Member

@nickitat nickitat left a comment

Choose a reason for hiding this comment

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

apart from minor suggestions looks great

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.

do you think PODArrayWithStackMemory would make sense here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I don't think so. A block of sizes is almost always larger than what we can put on stack.

@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the submodule changed At least one submodule changed in this PR. label Feb 29, 2024
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 pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.