Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Heuristic:Duckdb Duckdb Build Parallelism Tuning

From Leeroopedia





Knowledge Sources
Domains Optimization, Build_System
Last Updated 2026-02-07 12:00 GMT

Overview

Build speed optimization techniques using Ninja, ccache/sccache, and CMAKE_BUILD_PARALLEL_LEVEL to reduce DuckDB compilation time while avoiding system lockups.

Description

DuckDB is a large C++ project that can take significant time to compile. The default `make` invocation does not maximize parallelism. Using the Ninja build system, compiler caching (ccache or sccache), and properly tuned parallel job counts can dramatically reduce build times. However, naive parallelism can lock up the system if the CPU-to-memory ratio is unfavorable (e.g., many cores but limited RAM).

Usage

Apply this heuristic when you experience slow build times during DuckDB development, or when the system becomes unresponsive during compilation. Particularly important on development machines with high core counts but limited RAM (e.g., 16-core machine with 16GB RAM).

The Insight (Rule of Thumb)

  • Action 1: Use the Ninja build system instead of Make for parallel builds.
    • Command: `GEN=ninja make`
    • Trade-off: Requires installing Ninja; no functional difference in output.
  • Action 2: Restrict parallel processes if system locks up.
    • Command: `CMAKE_BUILD_PARALLEL_LEVEL=4 GEN=ninja make`
    • Rule: Set parallel level to approximately RAM_GB / 2 (each compilation unit can use 1-2GB).
    • Trade-off: Fewer parallel jobs means slower builds, but prevents OOM/lockup.
  • Action 3: Install ccache or sccache for automatic rebuild caching.
    • Command: `sudo apt-get install ccache` (auto-detected by CMake)
    • Trade-off: Disk space for cache (~5-10GB) in exchange for much faster incremental builds.
  • Action 4: Without Ninja, set parallel level explicitly.
    • Command: `CMAKE_BUILD_PARALLEL_LEVEL=$(nproc) make`
    • Trade-off: None; uses all cores available.

Reasoning

DuckDB's codebase has hundreds of translation units. Without parallelism, compilation is sequential and very slow. Ninja provides better dependency tracking and job scheduling than Make's default behavior. The ccache/sccache integration is automatic (CMakeLists.txt:41-59 auto-detects them) and caches object files by content hash, so unchanged files are not recompiled even after a `make clean`.

The CPU-to-memory ratio warning is critical because each C++ compilation unit (especially with template-heavy code) can consume 1-2GB of RAM. A 32-core machine with 16GB RAM running 32 parallel jobs will trigger OOM kills and system thrashing.

Code evidence from `CONTRIBUTING.md:51-54`:

* For parallel builds, you can use the Ninja build system: `GEN=ninja make`.
  * The default number of parallel processes can lock up the system depending on
    the CPU-to-memory ratio. If this happens, restrict the maximum number of build
    processes: `CMAKE_BUILD_PARALLEL_LEVEL=4 GEN=ninja make`.
  * Without using Ninja, build times can still be reduced by setting
    `CMAKE_BUILD_PARALLEL_LEVEL=$(nproc)`.
* To speed up rebuilds, install ccache. The build system will automatically detect
  and use it if available.

Auto-detection of ccache from `CMakeLists.txt:41-48`:

if(NOT DEFINED CMAKE_C_COMPILER_LAUNCHER)
    find_program(COMPILER_LAUNCHER NAMES ccache sccache)
    if(COMPILER_LAUNCHER)
        message(STATUS "Using ${COMPILER_LAUNCHER} as C compiler launcher")
        set(CMAKE_C_COMPILER_LAUNCHER
                "${COMPILER_LAUNCHER}"
                CACHE STRING "" FORCE)
    endif()
endif()

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment