Skip to content

Optimize aggregation performance of nullable String key when use AggregationMethodSerialized #51399

Merged
robot-ch-test-poll4 merged 11 commits intoClickHouse:masterfrom
liuneng1994:optimize_nullable_aggragate_serialized_method
Aug 16, 2023
Merged

Optimize aggregation performance of nullable String key when use AggregationMethodSerialized #51399
robot-ch-test-poll4 merged 11 commits intoClickHouse:masterfrom
liuneng1994:optimize_nullable_aggragate_serialized_method

Conversation

@liuneng1994
Copy link
Copy Markdown
Contributor

@liuneng1994 liuneng1994 commented Jun 26, 2023

Changelog category (leave one):

  • Performance Improvement

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

optimize aggregation performance of Nullable String key when using AggregationMethodSerialized

@liuneng1994
Copy link
Copy Markdown
Contributor Author

Same as #46812. The current implementation can improve TPCH as a whole by 4%.

@liuneng1994 liuneng1994 marked this pull request as draft June 26, 2023 05:05
@liuneng1994 liuneng1994 changed the title Optimize aggregation performance of nullable key when use AggregationMethodSerialized Optimize aggregation performance of nullable String key when use AggregationMethodSerialized Jun 26, 2023
@liuneng1994 liuneng1994 marked this pull request as ready for review June 26, 2023 05:44
@liuneng1994 liuneng1994 force-pushed the optimize_nullable_aggragate_serialized_method branch from 71a1298 to b9280d5 Compare June 26, 2023 05:48
@qoega qoega added the can be tested Allows running workflows for external contributors label Jun 26, 2023
@robot-clickhouse robot-clickhouse added the pr-performance Pull request with some performance improvements label Jun 26, 2023
@robot-clickhouse
Copy link
Copy Markdown
Member

robot-clickhouse commented Jun 26, 2023

This is an automated comment for commit 65aeb05 with description of existing statuses. It's updated for the latest CI running
The full report is available here
The overall status of the commit is 🟢 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🟢 success
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🟢 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 image for serversThe check to build and optionally push the mentioned image to docker hub🟢 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
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
Push to DockerhubThe check for building and pushing the CI related docker images to docker hub🟢 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

@liuneng1994 liuneng1994 force-pushed the optimize_nullable_aggragate_serialized_method branch 2 times, most recently from d35d8b8 to abad35b Compare June 27, 2023 03:08
@liuneng1994
Copy link
Copy Markdown
Contributor Author

image
The slower case doesn't seem to be related to the changed code, and the results that come out of the two runs are different and seem unstable

@nickitat nickitat self-assigned this Jun 27, 2023
Copy link
Copy Markdown
Member

@nickitat nickitat Jun 27, 2023

Choose a reason for hiding this comment

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

am I right that the whole idea of optimisation is to devirtualize serializeValueIntoArena? if so I think it would be better to just static_cast to ColumnString and call serializeValueIntoArena as usual.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not quite sure that the key to this optimization is devirtualize serializeValueIntoArena, and I've tested that this optimization has performance degradation on the number type. So need to determine whether the nested column is a ColumnString

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

image
I tested the above modification and there was no impact on performance

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not quite sure that the key to this optimization is devirtualize serializeValueIntoArena, and I've tested that this optimization has performance degradation on the number type. So need to determine whether the nested column is a ColumnString

There are some new discoveries that I will try again to apply in the fixed size column

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 would assume that calculating this condition for every element may hurt perf (especially for numeric types, because it is the fastest case). better would be to move this condition out of the hot loop

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@nickitat After my test, the performance improvement should be due to function inline and reduced allocContinue calls. If want to modify this part of the code to each column class, the amount of change will be very large

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added two variables to ColumnNullable to represent the type of nested column. I thought about moving these two variables to HashMethodSerialized, but this optimization can only be used in aggregation, and other places where serializeIntoArea is used cannot obtain optimization effects. So I think it's more appropriate to place these two variables in ColumnNullable

@liuneng1994
Copy link
Copy Markdown
Contributor Author

liuneng1994 commented Jun 28, 2023

If use getNestedColumn().valuesHaveFixedSize() to make a judgment, a lock instruction prefix is generated, which reduces performance

DB::ColumnNullable::serializeValueIntoArena(unsigned long, DB::Arena&, char const*&) const:
	pushq  %rbp
	pushq  %r15
	pushq  %r14
	pushq  %r13
	pushq  %r12
	pushq  %rbx
	subq   $0x28, %rsp
	movq   %rcx, %rbp
	movq   %rdx, %r15
	movq   %rsi, %rbx
	movq   %rdi, %r13
	movq   0x10(%rdi), %rdi
	movq   0x18(%r13), %r12
	movq   0x10(%r12), %rax
	movzbl (%rax,%rsi), %r14d
	movq   -0xfe8b163(%rip), %rcx     ; typeinfo for DB::ColumnString + 8
	cmpq   %rcx, -0xfe83ce2(%rip)     ; typeinfo for DB::IColumn + 8
	nopw   (%rax,%rax)
	je     0x177b2c98                 ; <+280> [inlined] DB::PODArray<unsigned long, 4096ul, Allocator<false, false>, 63ul, 64ul>::t_start() const at PODArray.h:324:83
	movq   (%rdi), %rax
	movq   -0x8(%rax), %rdx
	cmpq   %rcx, 0x8(%rdx)
	je     0x177b2c98                 ; <+280> [inlined] DB::PODArray<unsigned long, 4096ul, Allocator<false, false>, 63ul, 64ul>::t_start() const at PODArray.h:324:83
	callq  *0x260(%rax)
	nopl   (%rax)
	testb  %al, %al
	je     0x177b2d11                 ; <+401> at ColumnNullable.cpp
	movq   %r15, 0x10(%rsp)
	movq   0x10(%r13), %r13
	testq  %r13, %r13
	je     0x177b2bfb                 ; <+123> at ColumnNullable.cpp:160:26
	lock
	incl   0x8(%r13)
	movq   (%r13), %r15
	movq   %r13, %rdi
	movq   %rbx, %rsi
	movq   %r15, 0x8(%rsp)
	callq  *0x60(%r15)
	movq   %rax, 0x18(%rsp)
	movq   %r12, 0x20(%rsp)
	movq   %r13, %rdi
	callq  *0x278(%r15)
	movq   %rax, %r12
	incq   %rax
	testb  %r14b, %r14b
	movl   %r14d, %r15d
	movl   $0x1, %r14d
	cmoveq %rax, %r14
	movq   0x10(%rsp), %rdi
	movq   %r14, %rsi
	movq   %rbp, %rdx
	xorl   %ecx, %ecx
	callq  0xd9d3040                  ; DB::Arena::allocContinue(unsigned long, char const*&, unsigned long)
	movq   %rax, %rbp
	movq   0x20(%rsp), %rax
	movq   0x10(%rax), %rax
	movzbl (%rax,%rbx), %eax
	movb   %al, (%rbp)
	nopl   (%rax)
	testb  %r15b, %r15b
	jne    0x177b2c78                 ; <+248> [inlined] boost::detail::atomic_count::operator--() at atomic_count_gcc_atomic.hpp:43:16
	movq   %rbp, %rdi
	incq   %rdi
	movq   0x18(%rsp), %rsi
	movq   %r12, %rdx
	callq  0x1d11e740                 ; ::memcpy(void *__restrict, const void *__restrict, size_t) at memcpy.cpp:4
	lock
	decl   0x8(%r13)
	movq   0x8(%rsp), %r12
	jne    0x177b2d65                 ; <+485> at ColumnNullable.cpp:185:1
	movq   %r13, %rdi
	callq  *0x2b8(%r12)
	jmp    0x177b2d65                 ; <+485> at ColumnNullable.cpp:185:1
	movq   0x10(%rdi), %rax
	movq   0x28(%rdi), %rcx
	movq   %rcx, 0x10(%rsp)
	movq   -0x8(%rax,%rbx,8), %r13
	movq   %r15, %rdi
	movq   (%rax,%rbx,8), %r15
	subq   %r13, %r15
	leaq   0x9(%r15), %rax
	testb  %r14b, %r14b
	movl   $0x1, %esi
	movq   %rax, 0x8(%rsp)
	cmoveq %rax, %rsi
	movq   %rbp, %rdx
	xorl   %ecx, %ecx
	callq  0xd9d3040                  ; DB::Arena::allocContinue(unsigned long, char const*&, unsigned long)
	movq   %rax, %rbp
	movq   0x10(%r12), %rax
	movzbl (%rax,%rbx), %eax
	movb   %al, (%rbp)
	testb  %r14b, %r14b
	je     0x177b2cef                 ; <+367> at ColumnNullable.cpp
	movl   $0x1, %r14d
	jmp    0x177b2d65                 ; <+485> at ColumnNullable.cpp:185:1
	movq   0x10(%rsp), %rsi
	addq   %r13, %rsi
	movq   %r15, 0x1(%rbp)
	movq   %rbp, %rdi
	addq   $0x9, %rdi
	movq   %r15, %rdx
	callq  0x1d11e740                 ; ::memcpy(void *__restrict, const void *__restrict, size_t) at memcpy.cpp:4
	movq   0x8(%rsp), %r14
	jmp    0x177b2d65                 ; <+485> at ColumnNullable.cpp:185:1
	movl   $0x1, %r14d
	movl   $0x1, %esi
	movq   %r15, %rdi
	movq   %rbp, %rdx
	xorl   %ecx, %ecx
	callq  0xd9d3040                  ; DB::Arena::allocContinue(unsigned long, char const*&, unsigned long)
	movq   %rbp, %rcx
	movq   %rax, %rbp
	movq   0x10(%r12), %rax
	movzbl (%rax,%rbx), %eax
	movb   %al, (%rbp)
	movq   0x10(%r12), %rax
	cmpb   $0x0, (%rax,%rbx)
	jne    0x177b2d65                 ; <+485> at ColumnNullable.cpp:185:1
	movq   0x10(%r13), %rdi
	movq   (%rdi), %rax
	movq   %rbx, %rsi
	movq   %r15, %rdx
	callq  *0xf8(%rax)
	movq   %rax, %rbp
	movq   %rdx, %r14
	decq   %rbp
	incq   %r14
	movq   %rbp, %rax
	movq   %r14, %rdx
	addq   $0x28, %rsp
	popq   %rbx
	popq   %r12
	popq   %r13
	popq   %r14
	popq   %r15
	popq   %rbp
	retq
	jmp    0x177b2d7e                 ; <+510> at ColumnNullable.cpp
	jmp    0x177b2d7e                 ; <+510> at ColumnNullable.cpp
	movq   %rax, %rbx
	lock
	decl   0x8(%r13)
	jne    0x177b2d96                 ; <+534> at intrusive_ref_counter.hpp
	movq   %r13, %rdi
	movq   0x8(%rsp), %rax
	callq  *0x2b8(%rax)
	movq   %rbx, %rdi
	callq  0x1d1b09c0                 ; _Unwind_Resume at UnwindLevel1.c:435


@liuneng1994
Copy link
Copy Markdown
Contributor Author

image
image
I tried both of the above modifications. None worked

@baibaichen
Copy link
Copy Markdown
Contributor

@nickitat any suggestion?

@liuneng1994 liuneng1994 requested a review from nickitat July 3, 2023 01:46
@liuneng1994
Copy link
Copy Markdown
Contributor Author

@alexey-milovidov @nickitat Is this change acceptable or are there any other suggestions

@nickitat
Copy link
Copy Markdown
Member

nickitat commented Jul 4, 2023

@alexey-milovidov @nickitat Is this change acceptable or are there any other suggestions

my view is the following. ideally we want to have batch version of serializeValueIntoArena that will serialize whole block and this way we will not pay for virtual call and have benefits from inlining.
unfortunately we cannot implement it this way, because we have special logic that saves us memory:

inline void ALWAYS_INLINE keyHolderDiscardKey(DB::SerializedKeyHolder & holder)
{
[[maybe_unused]] void * new_head = holder.pool.rollback(holder.key.size);
assert(new_head == holder.key.data);
holder.key.data = nullptr;
holder.key.size = 0;
}

and for serialised aggregation saving memory is really important.
the only alternative I see is to do "devirtualisation" by hands. remember the type of column (e.g. string, fixed_string, vector or smth_else), inside ColumnNullable::serializeValueIntoArena do a static_cast to the actual column type (or fallback if type is smth_else) and call its serializeValueIntoArena. this way we won't have different serialisation algorithms for the same column type in different parts of code but still have non-virtual call and an inlining opportunity.
but it is not guaranteed to give big perf improvement...

@baibaichen
Copy link
Copy Markdown
Contributor

baibaichen commented Jul 5, 2023

@nickitat IIUC, we should change serializeValueIntoArena's signature so that we always choose nullable version of serializeValueIntoArena at ColumnNullable with static_cast

right?

@liuneng1994 liuneng1994 closed this Jul 5, 2023
@liuneng1994 liuneng1994 reopened this Jul 5, 2023
@liuneng1994 liuneng1994 force-pushed the optimize_nullable_aggragate_serialized_method branch 2 times, most recently from ecf8c82 to 94124d3 Compare July 5, 2023 08:33
@liuneng1994
Copy link
Copy Markdown
Contributor Author

liuneng1994 commented Jul 6, 2023

image

@nickitat I re-modified a version and added a new parameter null_bit to serializeValueIntoArena to pass the null value of nullable, here is to be able to combine nullable and specific types of serialization algorithms together. For the string type, I use static_cast to avoid calling virtual functions. For other fixed-length types, I still use virtual functions to choose, because there are many fixed-length types, and it is meaningless to make judgments on each of them and static_cast. When the null_bit is NULL, continue with the original serialization algorithm.

There are still some problems with this change, some code can be reused, and generally it can guarantee that there is only one serialization algorithm for a type. If this solution is acceptable in general, I will further improve the code.

If the method of modifying the interface is unacceptable, another way is to add additional functions for specific types (String, Number), and then call the function in nullable. If this method is acceptable, I can also implement it.

@nickitat
Copy link
Copy Markdown
Member

current solution looks good to me, let's continue

@liuneng1994 liuneng1994 force-pushed the optimize_nullable_aggragate_serialized_method branch from 7e7ac09 to 0388dc3 Compare August 7, 2023 06:25
@liuneng1994
Copy link
Copy Markdown
Contributor Author

image

@liuneng1994 liuneng1994 requested a review from nickitat August 7, 2023 10:13
@liuneng1994 liuneng1994 force-pushed the optimize_nullable_aggragate_serialized_method branch from af0ca7c to 65aeb05 Compare August 8, 2023 05:38
@baibaichen
Copy link
Copy Markdown
Contributor

@nickitat @alexey-milovidov

Any comments? would we merge this PR?

@nickitat
Copy link
Copy Markdown
Member

@nickitat @alexey-milovidov

Any comments? would we merge this PR?

guys, I really apologies for delayed responding. busy with some other tasks. will take another look by the end of the week.

@amosbird
Copy link
Copy Markdown
Collaborator

I came across this PR when trying to traceback the mysterious "const UInt8 *" argument in serializeValueIntoArena.

The optimization is likely not related to devirtualization, but to a reduced number of allocations. I've proposed a cleaner implementation in #55809. The complexity is constrained inside ColumnNullable instead of leaking around. @nickitat Could you help take a look?

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

Labels

can be tested Allows running workflows for external contributors pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants