More Than 2x Faster Hashing in ClickHouse Using Rust

Denis Bolonin
6 min readbeginner
--
View Original

Overview

The article discusses the integration of Rust into the ClickHouse DBMS to achieve more than 2x faster hashing using the BLAKE3 cryptographic hash function. It details the challenges faced during integration, the performance benefits observed, and the potential for future use of Rust libraries within ClickHouse.

What You'll Learn

1

How to integrate Rust libraries into a C++ project using CMake

2

Why BLAKE3 is preferred over SHA224 and SHA256 for hashing in ClickHouse

3

How to address compatibility issues between Rust and C++ data types

Prerequisites & Requirements

  • Understanding of C++ and CMake build systems
  • Familiarity with Rust and Cargo package manager(optional)

Key Questions Answered

How does integrating Rust improve hashing performance in ClickHouse?
Integrating Rust allows ClickHouse to utilize the BLAKE3 hash function, which performs more than 2x faster than SHA224 and SHA256, providing significant performance improvements for hashing operations. This integration leverages Rust's memory safety and performance characteristics, making it a suitable choice for high-performance applications.
What challenges were faced during the integration of Rust into ClickHouse?
Challenges included compatibility issues between Rust and C++ data types, false positives from the C++ Memory Sanitizer, and difficulties with multiplatform building and linking. These issues were resolved through shim functions and careful configuration, ensuring successful integration without compromising existing functionality.
What is the performance overhead of using shim methods for Rust integration?
The overhead from shim methods, which handle data type conversions between Rust and C++, was measured to take about 1.15% of the total query time. This indicates that while there is some overhead, it is relatively minimal compared to the performance gains achieved by using BLAKE3.

Key Statistics & Figures

Performance comparison of hashing functions
more than 2x faster than SHA224 or SHA256
BLAKE3's performance was measured against other hashing algorithms in ClickHouse.
Overhead from shim methods
1.15%
This represents the percentage of total query time taken by the conversion processes in the shim functions.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Programming Language
Rust
Used for implementing the BLAKE3 hash function within ClickHouse.
Programming Language
C++
Main language of the ClickHouse DBMS.
Build System
Cmake
Used for building ClickHouse and integrating Rust libraries.
Package Manager
Cargo
Rust's package manager used for managing Rust dependencies.

Key Actionable Insights

1
Consider integrating Rust libraries into existing C++ projects to leverage performance benefits, especially for computationally intensive tasks like hashing.
This integration can result in significant performance improvements, as demonstrated by the BLAKE3 hash function's speed compared to traditional hashing algorithms.
2
Utilize tools like Corrosion-rs for easier integration of Rust into CMake projects.
This utility simplifies the process of adding Rust dependencies, reducing the complexity of managing multiple build systems.
3
Be aware of compatibility issues when interfacing between Rust and C++ data types.
Implementing shim functions can help bridge the gap, but developers should anticipate the need for additional testing to ensure data integrity.

Common Pitfalls

1
Assuming that Rust integration will not affect existing C++ functionality.
Integration can introduce compatibility issues and additional complexity, requiring careful management to avoid breaking changes.
2
Overlooking the performance overhead introduced by shim functions.
While shim functions facilitate interoperability between Rust and C++, they can add overhead that may impact performance if not properly accounted for.

Related Concepts

Cryptographic Hashing Algorithms
Memory Safety In Programming Languages
Cross-compilation Strategies