Skip to content

benchmarks: add --reset-memory to return memory to the OS before each iteration - #27932

Draft
adriangb wants to merge 4 commits into
apache:mainfrom
adriangb:benchmarks-reset-memory
Draft

adriangb wants to merge 4 commits into
apache:mainfrom
adriangb:benchmarks-reset-memory

Conversation

@adriangb

@adriangb adriangb commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

mimalloc keeps freed memory for reuse. dfbench runs every query in one process, so the time of an iteration depends on what ran before it. A benchmark of a PR must not depend on that.

In an A/A experiment on a c4a-standard-16 VM, with 6 ClickBench queries, running another query first made an iteration slower:

Memory setting Mean slowdown after another query Between-process CV
default 7.2% 1.8%
reset before each iteration 0.6% 1.3%

This experiment used an earlier prototype of the reset. A larger run on three VMs (43 queries, forward and reversed order, injected regressions) is in progress. This PR stays a draft until its results are in.

What changes are included in this PR?

Two commits, best reviewed in order:

  1. Build the Tokio runtime in one place. This is a refactor and does not change behavior.
  2. Add --reset-memory. It returns free memory to the OS before each iteration, outside the timed part. It is off by default.
interleave.py
   ├── dfbench (main)                  ├── dfbench (PR)
   │     reset memory   (not timed)    │     reset memory   (not timed)
   │     send `ready`, wait for `run`  │     send `ready`, wait for `run`
   │     run the query  (timed)        │     run the query  (timed)

mi_collect empties only the heap of the calling thread. So the reset blocks one task on each runtime thread at a barrier, and then collects twice:

each thread: arrive ── collect ── arrive ── collect ── arrive
                │                    └ frees memory that another thread released in round one
                └ holds the thread, so no thread runs two of the tasks
Case Behavior
A thread stays busy The reset gives up after 2 seconds and prints a warning
A worker blocks while it holds the task of a parked worker The main thread spawns an empty task every 1 ms, which wakes workers
The build does not have mimalloc_extended, or uses snmalloc The process stops with an error

Not in this PR: interleave.py does not pass the flag yet, and mimalloc_extended is not a default feature.

What is the testing strategy for this PR?

  • A unit test asserts that the reset visits every thread exactly twice, and that a busy thread does not hang it.
  • I ran that unit test in 3000 processes (50 resets each), with no failures. Before the 1 ms wakeup, about 1 process in 400 failed. The reset's barrier code did not change after that run.
  • I ran the flag end to end through interleave.py on a small dataset, and in a build without mimalloc_extended to check the error.
  • ./dev/rust_lint.sh passes, except check_docs_html.sh, which needs tools that I do not have locally. This PR does not change docs/.

Are there any user-facing changes?

No change for DataFusion users. The benchmark binaries have a new --reset-memory flag.

New public items in the unpublished datafusion-benchmarks crate: util::build_runtime, CommonOpt::reset_memory, and Sampler::with_reset_memory.

🤖 Generated with Claude Code

adriangb and others added 2 commits December 1, 2026 14:24
`--reset-memory` (next commit) needs to count the threads of the runtime,
which `#[tokio::main]` does not allow. The four binaries that run an
iteration loop now call `util::build_runtime`. It has the settings of
`#[tokio::main]`, so this commit does not change behavior.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… iteration

mimalloc keeps freed memory for reuse, so the time of an iteration depends
on what ran before it in the same process. `--reset-memory` collects the
mimalloc heap of every runtime thread before each iteration, outside the
timed part. It is off by default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adriangb

adriangb commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

@alamb @HippoBaro (because we talked about similar in arrow-rs) i'm curious what you think of this change. from my measurements it seems that there's a lot of "lottery" effect that causes benchmark noise because each iteration of each query depends on the state of allocated memory in the run before it. i'm open to other ideas on how to address this problem if this is not a good solution, and i'm happy to walk through what i tried that didn't work.

@alamb alamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the idea -- I had some questions on specific details

run(&gates, Some(handle))
}

/// Returns all free memory of all threads to the OS. Must run on the thread

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the idea of returning all memory to the OS is a good one in my opinion

It is entirely unclear to me how this works -- is the idea that it tracks all threads and ensures that they have all exited? It would really help to document the design somewhere

Also if we are going to do this, why not always enable it? It seems like more reproducability would be a good thing in general so what is the usecase for not always clearing?

A review asked how the reset works. The module doc now explains why a
collection must run on each thread, how the barrier holds each thread,
and why the threads stay alive.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

#[cfg(all(feature = "mimalloc", feature = "mimalloc_extended"))]
fn collect() {
// SAFETY: `mi_collect` has no preconditions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this sould help to point out that mi_collect basically returns state to the OS?

) -> bool {
let deadline = Instant::now() + timeout;
let expected = workers + blocking + 1;
let gates: Arc<[Gate; 3]> =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is some crazy magic to me -- why are there 3 gates? What is this implementing? It seems like this is just trying to run a future on each thread?

A review asked why on_every_thread uses three gates, and what
mi_collect does. Doc and comments only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants