diff --git a/.github/workflows/clippy.yml b/.github/workflows/clippy.yml index fafd81908..76fabbabd 100644 --- a/.github/workflows/clippy.yml +++ b/.github/workflows/clippy.yml @@ -34,8 +34,9 @@ jobs: save-if: ${{ github.ref == 'refs/heads/main' }} # With the Cassandra and MongoDB crates' `test-hooks` features, so the # hook modules (`src/test_hooks.rs`) and their call sites, which only - # the integration test binaries build otherwise, are linted too. - - run: cargo clippy --all-targets --features extenddb-storage-cassandra/test-hooks,extenddb-storage-mongodb/test-hooks -- -D warnings + # the integration test binaries build otherwise, are linted too. With the + # bin crate's `mimalloc` feature as well, which no other job builds. + - run: cargo clippy --all-targets --features extenddb-storage-cassandra/test-hooks,extenddb-storage-mongodb/test-hooks,extenddb/mimalloc -- -D warnings # The backup job's kill-point pauses (`EXTENDDB_BACKUP_PAUSE`), which # `devtools/backup-kill-points.sh` runs by hand on a build with the # feature; no other job builds it. diff --git a/Cargo.lock b/Cargo.lock index e7e39f911..a71fb5342 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1670,6 +1670,7 @@ dependencies = [ "extenddb-storage-mongodb", "extenddb-storage-postgres", "extenddb-storage-sqlite", + "mimalloc", "sqlx", "tokio", "uuid", @@ -2924,6 +2925,15 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981" +[[package]] +name = "libmimalloc-sys" +version = "0.1.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a45a52f43e1c16f667ccfe4dd8c85b7f7c204fd5e3bf46c5b0db9a5c3c0b8e9" +dependencies = [ + "cc", +] + [[package]] name = "libredox" version = "0.1.16" @@ -3091,6 +3101,15 @@ dependencies = [ "rapidhash", ] +[[package]] +name = "mimalloc" +version = "0.1.52" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d4139bb28d14ad1facf21d5eb8825051b326e172d216b39f6d31df53cc97862" +dependencies = [ + "libmimalloc-sys", +] + [[package]] name = "mime" version = "0.3.17" diff --git a/Cargo.toml b/Cargo.toml index 138d50d1e..5bcf803d7 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,6 +59,9 @@ extenddb-auth = { path = "crates/auth", version = "=1.0.0" } extenddb-server = { path = "crates/server", version = "=1.0.0" } extenddb-app = { path = "crates/app", version = "=1.0.0" } +# Memory allocator, opt-in through the `mimalloc` feature of the bin crate +mimalloc = { version = "0.1", default-features = false } + # Serialization serde = { version = "1", features = ["derive"] } serde_json = "1" diff --git a/crates/bin/Cargo.toml b/crates/bin/Cargo.toml index 7a2705c20..31536922b 100755 --- a/crates/bin/Cargo.toml +++ b/crates/bin/Cargo.toml @@ -38,6 +38,9 @@ mongodb-test-hooks = [ # fails (`main.rs` checks this at compile time). A production `sqlite` build # leaves it off. dev-mode = ["extenddb-app/dev-mode"] +# Use mimalloc as the global allocator in place of the system allocator. Off by +# default. See ADR-0030 for the measurements. +mimalloc = ["dep:mimalloc"] [dependencies] extenddb-app = { workspace = true } @@ -48,6 +51,7 @@ extenddb-storage-postgres = { workspace = true, optional = true } extenddb-storage-sqlite = { workspace = true, optional = true } anyhow = { workspace = true } extenddb-config = { workspace = true } +mimalloc = { workspace = true, optional = true } [dev-dependencies] sqlx = { workspace = true } diff --git a/crates/bin/src/main.rs b/crates/bin/src/main.rs index 0f8e81bdd..bfd5a8cc3 100755 --- a/crates/bin/src/main.rs +++ b/crates/bin/src/main.rs @@ -17,6 +17,13 @@ //! installs one backend per process, so a build with more than one would be ambiguous //! and is rejected at compile time. +// The `mimalloc` feature replaces the system allocator with mimalloc. The +// request path allocates for each JSON value, attribute and string that it +// builds, on many tokio worker threads. See ADR-0030 for the measurements. +#[cfg(feature = "mimalloc")] +#[global_allocator] +static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; + // Exactly one backend feature must be enabled. #[cfg(any( all(feature = "postgres", feature = "cassandra"), diff --git a/docs/adr/0030-optional-mimalloc-allocator.md b/docs/adr/0030-optional-mimalloc-allocator.md new file mode 100644 index 000000000..af890f6c3 --- /dev/null +++ b/docs/adr/0030-optional-mimalloc-allocator.md @@ -0,0 +1,174 @@ +# ADR-0030: mimalloc is an opt-in global allocator for the server binary + +- Status: Accepted +- Date: 2026-11-10 +- Deciders: @parsnips + +## Context + +The `extenddb` binary uses the system allocator. On Linux that is the malloc +of the C library that the binary links. The container images and a +`cargo build` on a glibc host use glibc malloc. The Linux binaries of the npm +packages are static musl builds, and they use the musl malloc. This ADR +measured glibc only. + +The request path allocates often. A request body becomes a `serde_json::Value`, +then `AttributeValue`s. A PartiQL statement becomes a `PutItem` body, which is +parsed again. The item becomes JSONB for the storage backend. Each step +allocates strings, maps and vectors, and frees them on one of many tokio worker +threads. + +We measured the PostgreSQL build of `main` at 9c3fbfb, with and without the +feature, under conditional inserts with unique keys. The method below gives +the load, the settings and how each figure was read. + +In a `perf` profile of the server under the `PutItem` load with 32 requests in +flight, code in `libc.so.6` took 16% of the server's samples, which is 22% of +the samples in user space. + +### Method + +- **Builds.** `cargo build --release --locked -p extenddb`, and the same + command with `--features mimalloc`, in the `rust:1.97-bookworm` image (rustc + 1.97.1, glibc 2.36) with `RUSTFLAGS=-Cforce-frame-pointers=yes`. The two + builds differ in the feature only. +- **Host.** One machine ran the load generator, the server and PostgreSQL: an + AMD Ryzen 9 9900X (12 cores, 24 threads) with 32 GB of memory, on Linux + x86_64. The containers had no CPU or memory limit. The host also ran other + work. +- **PostgreSQL.** The `postgres:16-alpine` image, with `--shm-size 2g`, a data + directory made with `initdb --no-sync`, and these settings: + `max_connections=400`, `fsync=off`, `synchronous_commit=off`, + `full_page_writes=off`, `checkpoint_timeout=1h`, `max_wal_size=2GB`, + `shared_buffers=2GB`, `effective_cache_size=6GB`, `work_mem=64MB`, + `maintenance_work_mem=512MB`, `wal_buffers=64MB`. +- **Server.** The binary in a `debian:bookworm-slim` container on the host + network, after `extenddb init`, with TLS on. `[storage.postgres]` has + `pool_size = 20` and `catalog_pool_size = 20`. The settings + `control_plane_delay_seconds` and `gsi_propagation_delay_ms` are 0, and + `throttling_enabled` is false. The tokio runtime has its default number of + worker threads, which is one for each of the 24 logical CPUs. +- **Load generator.** A Go program that uses the AWS SDK for Go v2. Its + source and the script that runs one measurement are in the description of + pull request #954. It creates a table with a binary hash key and a binary + range key, on-demand billing, no index and no stream. Each request inserts + an item with a new 16-byte hash key, a 1-byte range key and a 256-byte + binary value. The `put` operation sends `PutItem` with + `attribute_not_exists(#pk)`. The `partiql` operation sends + `ExecuteStatement` with `INSERT INTO "