Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continue optimizing branch miss of if function when result type is float*/decimal*/int* #59148

Merged
merged 14 commits into from Jan 31, 2024

Conversation

taiyang-li
Copy link
Contributor

@taiyang-li taiyang-li commented Jan 24, 2024

Changelog category (leave one):

  • Performance Improvement

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

Continue optimizing branch miss of if function when result type is float*/decimal*/int* , follow up of #57885

The POCs helps me prove my ideas: https://github.com/bigo-sg/gluten/commits/improve_if/ (the last two commits)

Q1: with rand32() % 2 as x select if(x, materialize(1.234), materialize(2.456)) from numbers(1000000000) format Null;
Old: 0 rows in set. Elapsed: 8.865 sec. Processed 1.00 billion rows, 8.00 GB (112.81 million rows/s., 902.47 MB/s.)
New: 0 rows in set. Elapsed: 3.324 sec. Processed 1.00 billion rows, 8.00 GB (300.82 million rows/s., 2.41 GB/s.)
Q2: with rand32() % 2 as x, 1.234::Decimal64(3) as a, 2.456::Decimal64(3) as b select if(x, materialize(a), materialize(b)) from numbers(1000000000) format Null;
Old: 0 rows in set. Elapsed: 5.892 sec. Processed 1.00 billion rows, 8.00 GB (169.74 million rows/s., 1.36 GB/s.)
New: 0 rows in set. Elapsed: 5.237 sec. Processed 1.00 billion rows, 8.00 GB (190.93 million rows/s., 1.53 GB/s.)
with rand32() % 2 as x, 1::Int8 as a, -1::Int8 as b select if(x, a, b) from numbers(1000000000) format Null; 
0 rows in set. Elapsed: 8.973 sec. Processed 1.00 billion rows, 8.00 GB (111.44 million rows/s., 891.55 MB/s.)
0 rows in set. Elapsed: 2.419 sec. Processed 1.00 billion rows, 8.00 GB (413.38 million rows/s., 3.31 GB/s.)

with rand32() % 2 as x, 1::Int64 as a, -1::Int64 as b select if(x, a, b) from numbers(1000000000) format Null; 
0 rows in set. Elapsed: 2.268 sec. Processed 1.00 billion rows, 8.00 GB (440.90 million rows/s., 3.53 GB/s.)
0 rows in set. Elapsed: 1.785 sec. Processed 1.00 billion rows, 8.00 GB (560.32 million rows/s., 4.48 GB/s.) 

with rand32() % 2 as x, 1::Decimal64(3) as a, -1::Decimal64(3) as b select if(x, a, b) from numbers(1000000000) format Null; 
0 rows in set. Elapsed: 2.286 sec. Processed 1.00 billion rows, 8.00 GB (437.45 million rows/s., 3.50 GB/s.)
0 rows in set. Elapsed: 1.831 sec. Processed 1.00 billion rows, 8.00 GB (546.11 million rows/s., 4.37 GB/s.)

with rand32() % 2 as x, 1::Decimal128(3) as a, -1::Decimal128(3) as b select if(x, a, b) from numbers(1000000000) format Null; 
0 rows in set. Elapsed: 3.315 sec. Processed 1.00 billion rows, 8.00 GB (301.69 million rows/s., 2.41 GB/s.)
0 rows in set. Elapsed: 2.774 sec. Processed 1.00 billion rows, 8.00 GB (360.47 million rows/s., 2.88 GB/s.)

with rand32() % 2 as x, 1::Decimal256(3) as a, -1::Decimal256(3) as b select if(x, a, b) from numbers(100000000) format Null; 
0 rows in set. Elapsed: 1.004 sec. Processed 100.00 million rows, 800.00 MB (99.60 million rows/s., 796.83 MB/s.)
0 rows in set. Elapsed: 0.353 sec. Processed 100.00 million rows, 800.00 MB (283.16 million rows/s., 2.27 GB/s.)

with rand32() % 2 as x, 1::Int128 as a, -1::Int128 as b select if(x, a, b) from numbers(1000000000) format Null; 
0 rows in set. Elapsed: 3.330 sec. Processed 1.00 billion rows, 8.00 GB (300.33 million rows/s., 2.40 GB/s.)
0 rows in set. Elapsed: 2.755 sec. Processed 1.00 billion rows, 8.00 GB (363.00 million rows/s., 2.90 GB/s.)

with rand32() % 2 as x, 1::Int256 as a, -1::Int256 as b select if(x, a, b) from numbers(1000000000) format Null; 
0 rows in set. Elapsed: 9.825 sec. Processed 1.00 billion rows, 8.00 GB (101.78 million rows/s., 814.27 MB/s.)
0 rows in set. Elapsed: 3.406 sec. Processed 1.00 billion rows, 8.00 GB (293.59 million rows/s., 2.35 GB/s.)

@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-performance Pull request with some performance improvements label Jan 24, 2024
@robot-ch-test-poll4
Copy link
Contributor

robot-ch-test-poll4 commented Jan 24, 2024

This is an automated comment for commit 818fb98 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
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
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 server and keeper imagesThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
Docs checkThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
Fast testsThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ 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
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
Style checkThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ 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
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
Stress testRuns stateless functional tests concurrently from several clients to detect concurrency-related errors❌ failure

@Avogar Avogar self-assigned this Jan 24, 2024
@taiyang-li taiyang-li changed the title Optimize branch miss of if function when result type is floating point or decimal Optimize branch miss of if function when result type is float*/decimal*/int* Jan 25, 2024
@taiyang-li taiyang-li changed the title Optimize branch miss of if function when result type is float*/decimal*/int* Continue optimize branch miss of if function when result type is float*/decimal*/int* Jan 25, 2024
@taiyang-li taiyang-li changed the title Continue optimize branch miss of if function when result type is float*/decimal*/int* Continue optimizing branch miss of if function when result type is float*/decimal*/int* Jan 25, 2024
@taiyang-li
Copy link
Contributor Author

taiyang-li commented Jan 26, 2024

Performance report on x86-64

image

Performance report on aarch64

image

Some bad cases

  • if: SELECT count() FROM zeros(1000000000) WHERE NOT ignore(if(rand32() < 42949673, 1, 2)). Slow down.
  • if_to_multiif: WITH number AS x SELECT sum(x < 1 ? 1 : (x < 5 ? 2 : 3)) FROM numbers(100000000). Not permanent.
  • if_to_multiif: WITH number AS x SELECT sum(x < 1 ? 1 : (x < 5 ? 2 : (x < 10 ? 3 : (x % 2 ? 4 : 5)))) FROM numbers(100000000). Not permanent.

@taiyang-li
Copy link
Contributor Author

Bad cases are solved

@Algunenano
Copy link
Member

Fast test failures are unrelated (unpinned dependency update). Please rebase with master

// This macro performs a branch-free conditional assignment for floating point types.
// It uses bitwise operations to avoid branching, which can be beneficial for performance.
#define BRANCHFREE_IF_FLOAT(TYPE, vc, va, vb, vr) \
using UIntType = typename NumberTraits::Construct<false, false, sizeof(TYPE)>::Type; \
Copy link
Member

Choose a reason for hiding this comment

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

Interestingly, we have a similar approach in AggregateFunctionSum:

static_assert(sizeof(Value) == 4 || sizeof(Value) == 8);
using equivalent_integer = typename std::conditional_t<sizeof(Value) == 4, UInt32, UInt64>;
constexpr size_t unroll_count = 128 / sizeof(T);
T partial_sums[unroll_count]{};
const auto * unrolled_end = ptr + (count / unroll_count * unroll_count);
while (ptr < unrolled_end)
{
for (size_t i = 0; i < unroll_count; ++i)
{
equivalent_integer value;
std::memcpy(&value, &ptr[i], sizeof(Value));
value &= (!condition_map[i] != add_if_zero) - 1;
Value d;
std::memcpy(&d, &value, sizeof(Value));

@Avogar Avogar assigned Algunenano and unassigned Avogar Jan 26, 2024
@Avogar
Copy link
Member

Avogar commented Jan 26, 2024

@Algunenano I know you like such oprimizations, so, please be a reviewer instead of me :)

@taiyang-li
Copy link
Contributor Author

Copy link
Member

@Algunenano Algunenano left a comment

Choose a reason for hiding this comment

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

Looks really nice to me. Thanks a lot, specially for adding the extra parenthesis in for+if situations (helps readability IMO). I've left a few comments. Can you have a look, please?

src/Functions/if.cpp Outdated Show resolved Hide resolved
src/Functions/if.cpp Show resolved Hide resolved
src/Functions/if.cpp Show resolved Hide resolved
@taiyang-li
Copy link
Contributor Author

taiyang-li commented Jan 31, 2024

BTW, this is another opt of if function when input type is map: #59413 @Algunenano

@Algunenano Algunenano merged commit f67bff1 into ClickHouse:master Jan 31, 2024
250 of 255 checks passed
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.

None yet

4 participants