---
title: "JIT in ClickHouse"
description: "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."
url: "https://swe.quest/articles/jit-in-clickhouse-a3bd9587dc26e5d4"
canonical: "https://clickhouse.com/blog/clickhouse-just-in-time-compiler-jit"
source: "ClickHouse"
published: "2022-11-23"
difficulty: "hard"
kind: "Deep dive"
reading_minutes: 27
tags: ["compilers","query-engines","c-cpp","performance","analytics"]
---

# JIT in ClickHouse

A post on [ClickHouse](https://swe.quest/sources/clickhouse.md), published 2022-11-23, 27 min read, Deep dive. Original: [https://clickhouse.com/blog/clickhouse-just-in-time-compiler-jit](https://clickhouse.com/blog/clickhouse-just-in-time-compiler-jit)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Compilers](https://swe.quest/tags/compilers.md) (Compilers & runtimes)
- [Query engines](https://swe.quest/tags/query-engines.md) (Data & databases)
- [C & C++](https://swe.quest/tags/c-cpp.md) (Languages)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [Analytics & OLAP](https://swe.quest/tags/analytics.md) (Data & databases)

## Prerequisites

- C++ ([easy posts on C & C++](https://swe.quest/tags/c-cpp.md?difficulty=easy))
- x86-64 assembly basics
- calling conventions
- compiler IR concepts

## Keywords

ClickHouse, LLVM, LLVM IR, RuntimeDyld, ELF, CHJIT

## Related posts

- [chDB - A Rocket Engine on a Bicycle](https://swe.quest/articles/chdb-a-rocket-engine-on-a-bicycle-951148e84c12126b.md): ClickHouse · 2023-09-29 · Hard · 9 min read · Analytics & OLAP, Memory management, C & C++, Python, Performance engineering
  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.
- [Optimizing ClickHouse for Intel's ultra-high core count processors](https://swe.quest/articles/optimizing-clickhouse-for-intel-s-ultra-high-core-count-processors-93479313e687f95e.md): ClickHouse · 2025-09-17 · Hard · 22 min read · Performance engineering, Concurrency, Memory management, Analytics & OLAP, Hardware
  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.
- [CPU Dispatch in ClickHouse](https://swe.quest/articles/cpu-dispatch-in-clickhouse-5bbf7a6eb81cd552.md): ClickHouse · 2023-10-03 · Hard · 16 min read · C & C++, Performance engineering, Compilers, Hardware, Analytics & OLAP
  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.
- [How to use Rust in ClickHouse: avoiding a full rewrite](https://swe.quest/articles/how-to-use-rust-in-clickhouse-avoiding-a-full-rewrite-e470754e07a213d4.md): ClickHouse · 2025-12-17 · Medium · 13 min read · Rust, C & C++, Build systems, Refactoring & migrations, Testing
  ClickHouse, a 1.5M-line C++ analytic database, is adopting Rust incrementally rather than rewriting.
- [Compile and run C in JavaScript](https://swe.quest/articles/compile-and-run-c-in-javascript-951e10ff9004c507.md): Bun · 2024-09-18 · Medium · 8 min read · JavaScript, C & C++, Runtimes & VMs, Performance engineering
  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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
