Skip to content

Add State::in_timed_section() helper - #2319

Merged
LebedevRI merged 2 commits into
google:mainfrom
nicholasjng:push-sqnnlxmpmstu
Oct 8, 2026
Merged

LebedevRI merged 2 commits into
google:mainfrom
nicholasjng:push-sqnnlxmpmstu

Conversation

@nicholasjng

Copy link
Copy Markdown
Contributor

In some scenarios, it can be useful to know if we are currently in a benchmark loop. The "are we currently in a loop" check was already used internally, but only in asserts around timings.

This change just packages that check into a public API on State, which makes user queries on the benchmark status a bit nicer.


Same case, implementing Pythonic bindings around State::{Pause,Resume}Timing(). In particular, this allows me to catch a pause outside of a benchmark loop with if(!state->in_timing_loop()) and emit a std::runtime_error, which is translated to a Python RuntimeError by nanobind.

@dmah42

dmah42 commented Oct 5, 2026

Copy link
Copy Markdown
Member

why would users need this? i understand that you caught a bug with it, but if that bug is resolved (and that suggests we're missing a test) then is this more generally useful?

@nicholasjng

Copy link
Copy Markdown
Contributor Author

In the end, this is only relevant for the Python bindings, where I'm using that assert condition to detect wrong usage by Python developers directly in the bindings. I'm compiling in release mode, so I wouldn't hit that in the library code.

But I agree, the value for users is limited. I figured it fits in with the rest of the State lifecycle methods.

dmah42
dmah42 previously approved these changes Oct 5, 2026
Comment thread include/benchmark/state.h Outdated
In some scenarios, it can be useful to know if we are currently in a benchmark loop.
The "are we currently in a timed section" check was already used internally,
but only in asserts around timings.

This change just packages that check into a public API on `State`, which makes
user queries on the benchmark status a bit nicer.
@nicholasjng nicholasjng changed the title Add State::in_timing_loop() helper Add State::in_timed_section() helper Oct 7, 2026
@LebedevRI
LebedevRI merged commit e662de9 into google:main Oct 8, 2026
104 checks passed
@LebedevRI

Copy link
Copy Markdown
Collaborator

@nicholasjng thank you!

@nicholasjng
nicholasjng deleted the push-sqnnlxmpmstu branch October 8, 2026 16:05
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.

3 participants