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

Refactor CapnProto format to improve input/output performance #49752

Merged
merged 30 commits into from Jun 13, 2023

Conversation

Avogar
Copy link
Member

@Avogar Avogar commented May 10, 2023

Changelog category (leave one):

  • Performance Improvement

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

Rewrite CapnProto input/output format to improve its performance. Map column names and CapnProto fields case insensitive, fix reading/writing of nested structure fields.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Information about CI checks: https://clickhouse.com/docs/en/development/continuous-integration/

@robot-clickhouse robot-clickhouse added the pr-performance Pull request with some performance improvements label May 10, 2023
@robot-clickhouse
Copy link
Member

robot-clickhouse commented May 10, 2023

This is an automated comment for commit 24d70a2 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 🔴 failure

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🔴 failure
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

@Avogar
Copy link
Member Author

Avogar commented May 12, 2023

Benchmark for hits table:
Before this PR:

avogar-dev :) select * from test.hits limit 1000000 into outfile 'data.capnp' settings format_schema='hits_schema:Message'

SELECT *
FROM test.hits
LIMIT 1000000
INTO OUTFILE 'data.capnp'
SETTINGS format_schema = 'hits_schema:Message'

Query id: 3633d222-f294-484a-8c65-690fdfa3bb8e

1000000 rows in set. Elapsed: 50.638 sec. Processed 1.00 million rows, 937.17 MB (19.75 thousand rows/s., 18.51 MB/s.)

avogar-dev :) insert into test.hits from infile 'data.capnp' settings format_schema='hits_schema:Message'

INSERT INTO test.hits FROM INFILE 'data.capnp'
SETTINGS format_schema = 'hits_schema:Message'

Query id: f373b3c6-6f92-4efa-ba8a-bb01daa0183a

Ok.

1000000 rows in set. Elapsed: 36.840 sec. Processed 1.00 million rows, 937.16 MB (27.14 thousand rows/s., 25.44 MB/s.)

After this PR:

avogar-dev :) select * from test.hits limit 1000000 into outfile 'data.capnp' settings format_schema='hits_schema:Message'

SELECT *
FROM test.hits
LIMIT 1000000
INTO OUTFILE 'data.capnp'
SETTINGS format_schema = 'hits_schema:Message'

Query id: 1c17dfd9-9d65-4073-b0e9-951762ce4d3b


1000000 rows in set. Elapsed: 11.876 sec. Processed 1.00 million rows, 937.17 MB (84.20 thousand rows/s., 78.91 MB/s.)

avogar-dev :) insert into test.hits from infile 'data.capnp' settings format_schema='hits_schema:Message'

INSERT INTO test.hits FROM INFILE 'data.capnp'
SETTINGS format_schema = 'hits_schema:Message'

Query id: 23ff7c36-dd4a-4861-b7a2-2828e81cbc44

Ok.

1000000 rows in set. Elapsed: 11.951 sec. Processed 1.00 million rows, 937.16 MB (83.67 thousand rows/s., 78.41 MB/s.)

x5 improvement for output, x3 improvement for input. But still 10s is too long for 1000000 rows, but it's already the limitation of capnp library, seems like it's not well optimized for generic messages writing and reading (too many unnecessary copies per each message).

@@ -0,0 +1,298 @@
#include <Formats/CapnProtoSchema.h>
Copy link
Member Author

@Avogar Avogar May 12, 2023

Choose a reason for hiding this comment

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

Code from this file is not new, it's copied from old file CapnProtoUtils.cpp. The new code is in CapnProtoSerializer.cpp

@al13n321 al13n321 self-assigned this May 13, 2023
@Avogar
Copy link
Member Author

Avogar commented May 17, 2023

By updating capnp library (just add additional arguments for some methods to avoid copying, will test it more and make a patch) I can achive x1.5-x2 performance improvement. But the main problem with CapnProto input/output format is that we first serialize/deserialize message in memory and then read data from it. Perfect solution will be to read/write message directly from/into buffer, but it will require writing our own CapnProto message serializer/deserializer (as it's done for Protobuf format). But I am not ready for it now.

@robot-ch-test-poll1 robot-ch-test-poll1 added the submodule changed At least one submodule changed in this PR. label May 31, 2023
@Avogar
Copy link
Member Author

Avogar commented May 31, 2023

New results:

avogar-dev :) select * from test.hits limit 1000000 into outfile 'data.capnp' settings format_schema='hits_schema:Message'

SELECT *
FROM test.hits
LIMIT 1000000
INTO OUTFILE 'data.capnp'
SETTINGS format_schema = 'hits_schema:Message'

Query id: 1c17dfd9-9d65-4073-b0e9-951762ce4d3b


1000000 rows in set. Elapsed: 3.539 sec. Processed 1.00 million rows, 937.17 MB (287.00 thousand rows/s., 268.91 MB/s.)

avogar-dev :) insert into test.hits from infile 'data.capnp' settings format_schema='hits_schema:Message'

INSERT INTO test.hits FROM INFILE 'data.capnp'
SETTINGS format_schema = 'hits_schema:Message'

Query id: 23ff7c36-dd4a-4861-b7a2-2828e81cbc44

Ok.

1000000 rows in set. Elapsed: 4.006 sec. Processed 1.00 million rows, 889.45 MB (249.65 thousand rows/s., 222.06 MB/s.)

Total improvement now ~15x for output and ~10x for input.

@al13n321 I think it's ready for review

Copy link
Member

@al13n321 al13n321 left a comment

Choose a reason for hiding this comment

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

Looks reasonable, nice results!

@@ -959,7 +959,7 @@ class IColumn;
M(Bool, output_format_orc_string_as_string, false, "Use ORC String type instead of Binary for String columns", 0) \
M(ORCCompression, output_format_orc_compression_method, "lz4", "Compression method for ORC output format. Supported codecs: lz4, snappy, zlib, zstd, none (uncompressed)", 0) \
\
M(EnumComparingMode, format_capn_proto_enum_comparising_mode, FormatSettings::EnumComparingMode::BY_VALUES, "How to map ClickHouse Enum and CapnProto Enum", 0) \
M(CapnProtoEnumComparingMode, format_capn_proto_enum_comparising_mode, FormatSettings::CapnProtoEnumComparingMode::BY_VALUES, "How to map ClickHouse Enum and CapnProto Enum", 0) \
Copy link
Member

Choose a reason for hiding this comment

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

format_capn_proto_enum_comparising_mode

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately, this setting exists for a long time and we cannot change it's name(

src/Formats/CapnProtoSerializer.cpp Outdated Show resolved Hide resolved
src/Formats/CapnProtoSerializer.cpp Outdated Show resolved Hide resolved
if (enum_comparing_mode == FormatSettings::CapnProtoEnumComparingMode::BY_VALUES)
assert_cast<ColumnVector<EnumType> &>(column).insertValue(static_cast<EnumType>(capnp_enum_value));
else
assert_cast<ColumnVector<EnumType> &>(column).insertValue(capnp_to_ch_values[capnp_enum_value]);
Copy link
Member

Choose a reason for hiding this comment

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

Ditto.

Comment on lines +356 to +416
std::unordered_map<EnumType, UInt16> ch_to_capnp_values;
std::unordered_map<UInt16, EnumType> capnp_to_ch_values;
Copy link
Member

Choose a reason for hiding this comment

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

These could be std::vectors, for speed. Since all indices are at most 16 bits, it'll be at most 128 KB (+ maybe a 64 KB null mask to detect unexpected indices), so don't event need a fallback to hashtable for sparse enums.

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense, I will try to change to vectors

Copy link
Member Author

Choose a reason for hiding this comment

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

I decided to leave it as map, because by using vectors we won't be able to determine if we have invalid enum value

src/Formats/CapnProtoSerializer.cpp Outdated Show resolved Hide resolved
src/Formats/CapnProtoSerializer.cpp Outdated Show resolved Hide resolved
@Avogar Avogar merged commit 8fdcd91 into ClickHouse:master Jun 13, 2023
256 of 257 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 submodule changed At least one submodule changed in this PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants