Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

docs(performance): note the CPU allocator effect on large batches (#6073)

Dmytro Mishkindae268c41660
diff --git a/docs/generate_benchmarks.py b/docs/generate_benchmarks.py
index 8ce39f9e6..1b979d254 100644
--- a/docs/generate_benchmarks.py
+++ b/docs/generate_benchmarks.py
@@ -78,6 +78,26 @@ Reproduce or contribute a machine with::
Most suites are a ``flagship.py``; ``benchmarks/README.md`` maps each suite to its script.
+CPU: large batches and the memory allocator
+-------------------------------------------
+
+With glibc on Linux, an operation whose temporaries are large gets them as fresh memory from the operating system on
+every call and faults it in page by page: glibc serves a buffer above its mmap threshold (which rises to at most 32 MiB
+on 64-bit systems) with a fresh mapping and unmaps it when it is freed, and it returns the free top of its heap to the
+system once that exceeds the trim threshold. Smaller temporaries stop faulting once those thresholds have adapted to the
+process, so a fresh process overstates their cost. Comparing ``resource.getrusage(resource.RUSAGE_SELF).ru_minflt``
+before and after a call counts its faults. At batch 32 of 256 x 256 float32 images with 4 threads, on an i7-14700K under
+WSL2 with torch 2.14, ``bilateral_blur`` took 544 ms per call, ``ssim`` 270 ms and ``dice_loss`` 390 ms (the median of
+three processes), faulting 440,000 to 730,000 pages per call; with those per-call faults removed by::
+
+ GLIBC_TUNABLES=glibc.malloc.mmap_max=0:glibc.malloc.trim_threshold=68719476736 python train.py
+
+they took 344 ms, 135 ms and 214 ms, with no faults per call after the first. The setting keeps freed memory in the
+process, so its resident memory stays at its peak. Loading jemalloc or tcmalloc through ``LD_PRELOAD`` is an
+alternative, not measured here. Faults cost less on native Linux than under WSL2. This is glibc's behaviour: macOS uses
+another allocator and is not covered here, and CUDA tensors come from PyTorch's caching allocator, which keeps freed
+blocks.
+
"""
diff --git a/tests/test_generate_benchmarks.py b/tests/test_generate_benchmarks.py
index 7c4796c7d..aaeac603d 100644
--- a/tests/test_generate_benchmarks.py
+++ b/tests/test_generate_benchmarks.py
@@ -130,6 +130,16 @@ def test_render_page_empty_results_dir(tmp_path: Path) -> None:
assert "No benchmark results" in rst # page still builds, honestly empty
[email protected]("seeded", [False, True])
+def test_render_page_explains_the_cpu_allocator(tmp_path: Path, seeded: bool) -> None:
+ # The page is generated, so the CPU allocator note lives in the generator; an edit to performance.rst is lost.
+ rst = generate_benchmarks.render_page(_seed(tmp_path) if seeded else tmp_path)
+ heading = "CPU: large batches and the memory allocator"
+ assert f"{heading}\n{'-' * len(heading)}\n" in rst
+ assert "GLIBC_TUNABLES=glibc.malloc.mmap_max=0:glibc.malloc.trim_threshold=68719476736" in rst
+ assert "ru_minflt" in rst
+
+
def test_refresh_llms_replaces_only_marker_block(tmp_path: Path) -> None:
llms = tmp_path / "llms-full.txt"
llms.write_text("before\n<!-- BENCH:BEGIN -->\nold\n<!-- BENCH:END -->\nafter\n")