JIT in ClickHouse
Just-in-time compilation is built up from first principles: hand-encoding x86-64 instructions into an mmap'd executable page, calling back into host functions via pointers, and reading ELF sections to see where code and constants live. LLVM's JIT building blocks (IR, IRBuilder, pass managers, TargetMachine, RuntimeDyld dynamic linker, memory manager, symbol resolver) are then laid out as a compilation strategy, followed by ClickHouse's CHJIT wrapper, which compiles modules, registers external symbols like memset, supports per-module deletion and is thread-safe. The rest covers how ClickHouse applies JIT compilation to query execution.
Related posts
- chDB - A Rocket Engine on a Bicycle
ClickHouse hardMaster
chDB embeds the ClickHouse engine as an in-process Python module instead of shelling out to the 500MB clickhouse-local binary, using WriteBufferFromVector, Python memoryview and Pybind11 for zero-copy result retrieval. Getting jemalloc to work inside the shared library meant redirecting malloc with the linker flag -Wl,-wrap,malloc. That exposed crashes when glibc functions like getcwd returned glibc-malloc'd memory that was then freed by je_free, so the author patched jemalloc (PR #2424) to make arenas.lookup safe for checking ownership. The result cut memory usage by 50%, and chDB ranks as the fastest stateless/serverless engine on ClickBench. Later releases added SQL over Pandas DataFrames, query stats and Python UDFs.
- Optimizing ClickHouse for Intel's ultra-high core count processors
ClickHouse hardMaster
Intel engineers profiled all 43 ClickBench queries on 160–480 vCPU Xeon systems and landed a series of ClickHouse optimizations targeting lock contention, memory management and other scaling bottlenecks. Examples include double-checked locking with shared locks in the query condition cache (spinlock time cut from 76% to 1%, Q10/Q11 up ~85–89%), thread-local profiler timers, and raising jemalloc's lg_extent_max_active_fit to 8 to match two-level hash table reuse (Q35 up 96%, page faults down 71%). Individual fixes gave up to 10x on single queries and 2–10% geomean gains each, all merged upstream.
- CPU Dispatch in ClickHouse
ClickHouse hardMaster
CPU dispatch lets a single binary use SIMD instruction sets like AVX2 and AVX-512 while staying compatible with older CPUs: hot functions are compiled for several targets with the target attribute, and the best variant is chosen at runtime using CPUID feature checks. The walkthrough covers auto-vectorization basics with clang, including __restrict for aliasing, vzeroupper, and -Rpass remarks for diagnosing missed vectorization, plus a fix that moves a member-variable accumulator into a local. It also describes how ClickHouse structures its dispatch framework with a TargetArch enum and instruction-set-specific code blocks. Practical advice for finding candidates is to build with different instruction sets or compiler options and compare performance tests.
- How to use Rust in ClickHouse: avoiding a full rewrite
ClickHouse mediumAdept
ClickHouse, a 1.5M-line C++ analytic database, is adopting Rust incrementally rather than rewriting. It started with Corrosion to wire Rust into CMake and a BLAKE3 hash function, then took on a client history-navigation feature, the PRQL query language, and Databricks' delta-kernel-rs for Delta Lake support. The integration problems were mostly practical: hermetic reproducible builds with vendored crates, buggy FFI wrappers caught quickly by existing CI and fuzzing, library panics that crash the server, compiling Rust with sanitizers (now easier with an MSan toolchain), duplicated cross-compilation setup, and transitive dependencies linking system OpenSSL.
- Compile and run C in JavaScript
Bun mediumAdept
Bun v1.1.28 adds experimental `cc` in `bun:ffi`, which uses TinyCC to compile, link and relocate C in memory and generates inline wrappers that convert between JavaScriptCore values and C primitives with no dynamic dispatch. It is pitched as an alternative to N-API addons, which need node-gyp builds or prebuilt platform matrices and cost 7–15ns per call versus 2ns for the JavaScriptCore C++ API, and to WebAssembly, which cannot make system calls and must copy data across its isolated memory. Benchmarks show about 5ms to compile a small file and roughly 2ns of per-call overhead over native C. It suits glue code for C and system libraries such as ffmpeg or the macOS Keychain, not large projects needing Clang/GCC-level optimization.