Skip to content

fix: run setup before every timed call - #53

Merged
HaoZeke merged 8 commits into
airspeed-velocity:mainfrom
HaoZeke:fix/966-setup-before-each-timed-call
Aug 4, 2026
Merged

HaoZeke merged 8 commits into
airspeed-velocity:mainfrom
HaoZeke:fix/966-setup-before-each-timed-call

Conversation

@HaoZeke

@HaoZeke HaoZeke commented Aug 4, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes airspeed-velocity/asv#966 for the runner side.

timeit.Timer(stmt, setup=redo_setup).timeit(number=N) runs setup once,
then the statement N times: setup was per sample, never between the
inner calls, and warmup suppressed it entirely. With setup hooks
present, every timed call now observes freshly set-up state:

  • Warmup re-runs setup between calls, so warmup matches the measurement
    loop. This was the remaining failure for explicit number=1 reported
    in the issue thread.
  • Auto-calibrated number resolves to 1 under plain timeit.
  • An explicitly set number > 1 batches through a timeit-compatible
    timer that re-runs setup before every call and accumulates only the
    calls (gc disabled per sample, matching timeit), at the cost of two
    clock reads per call.
  • Benchmarks without setup hooks are unchanged, and setup_cache
    remains the batching path for input-building setups.
  • timeraw opts out: each of its samples is a fresh subprocess.

Also fixes asv_runner.__version__ for source-tree imports: the
in-repo __init__.py was empty (pdm-backend writes the static version
only into built artifacts), so editable installs had no __version__
and no-scm tarball builds fell back to 0.0.0. It now resolves the
installed distribution version with a 0.0.0.dev0 fallback.

Also fixes release publishing: both build jobs uploaded a v4 artifact
under the default name artifact, and the name-filtered download in
upload_pypi took only one of them, so v0.2.5 reached PyPI wheel-only
(downstream packagers then built from GitHub archive tarballs, which
carry no scm version; the conda-forge 0.2.5 artifact ships
__version__ = 0.1.dev24+gf49f9a3). Artifacts are now named and merged
into dist/, and checkouts fetch full history so branch builds get a
real described version.

Companion docs: airspeed-velocity/asv#1608

Test plan

  • python -m unittest discover -s tests (25 tests)
  • #966 reproducer (auto number) passes under default and zero warmup
  • Explicit number=1 survives warmup; explicit number=10 with a
    mutating setup passes and reports number=10; auto calibration still
    picks number>1 without setup hooks
  • Wheel build writes the static version; source tree reports the
    metadata (or fallback) version

timeit runs setup once then the statement N times. Stateful setup()
must re-run between samples; with number>1 (auto or explicit) the
stmt mutates state without re-setup (asv#966).

When any setup hooks are registered, pin number=1 so each sample is
one call and redo_setup runs between samples.
@github-actions

github-actions Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

Documentation preview for this PR: documentation.zip. Unzip and open index.html in a browser (Sphinx HTML under the zip root).

Comment thread asv_runner/benchmarks/time.py Outdated
# re-setup. Benchmarks with a setup() that restores state need
# number=1 so each sample is one call (setup between samples via
# redo_setup). See asv#966.
if self._setups and number != 1:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Would make sense to warn the user than its number is not taken into account.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The merged design honors an explicit number instead of overriding it: with setup hooks present it batches through per-call timing with setup interleaved between the calls, so nothing is silently dropped. Auto-calibration with setup hooks resolves to number=1, which the docs spell out (airspeed-velocity/asv#1608). Added a stderr note when the interleaved path engages so the changed methodology is visible in benchmark output: 3538c94.

HaoZeke added 5 commits August 4, 2026 05:54
Warmup suppresses redo_setup to stay cheap; with setup hooks present
that hands every warmup call the state mutated by the previous one,
which is the failure reported for explicit number=1 in asv#966. Keep
the suppression only when no setup hooks exist, so warmup observes the
same freshly-set-up state as the measurement loop.
Pinning number=1 for every benchmark with a setup hook turns fast
benchmarks with benign input-building setups into single-shot samples
at timer granularity and makes the number attribute dead. Pin only
when number is auto (0): the asv#966 reproducer uses auto number and
stays fixed, while an explicit number keeps timeit batching for
authors who accept shared state within a sample. Timeraw samples run
in fresh subprocesses, so TimerawBenchmark opts out of the pinning.
Run the asv#966 reproducer under default CPython warmup instead of
warmup_time=0, which masked the warmup-loop state reuse. Add explicit
number=1 (warmup semantics) and number=7 (batching preserved with a
benign setup) cases, and require auto calibration to pick number>1
without setup hooks.
An explicit number > 1 with setup hooks previously shared state
mutated by earlier calls within the sample. Route that case through a
timeit-compatible batch timer that re-runs setup (redo_setup) before
every call and accumulates only the calls, with gc disabled per sample
to match timeit. Costs two clock reads per call, which only explicit
batching with setup hooks pays; auto calibration still resolves to
number=1 under plain timeit and hook-free benchmarks are unchanged.
The packaged __init__.py is generated by pdm-backend (write_to) and
carries a static __version__; the in-repo file was empty, so editable
and sys.path imports had no __version__ at all and no-scm tarball
builds fell back to 0.0.0. Resolve the installed distribution version
via importlib.metadata with a 0.0.0.dev0 fallback; built artifacts
still get the static string.
@HaoZeke HaoZeke changed the title fix: force number=1 when timing benchmarks have setup() fix: run setup before every timed call Aug 4, 2026
HaoZeke added 2 commits August 4, 2026 06:29
Both build jobs uploaded a v4 artifact under the default name
'artifact'; same-named v4 artifacts from parallel jobs are distinct
objects and the name-filtered download in upload_pypi took only one,
so v0.2.5 reached PyPI wheel-only and downstream packagers fell back
to GitHub archive tarballs that carry no scm version. Name the
artifacts, merge them into dist/ for publishing, and fetch full
history so pdm-backend describes a real version on branch builds.
@HaoZeke
HaoZeke merged commit 152c61e into airspeed-velocity:main Aug 4, 2026
12 checks passed
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.

setup() not being run before benchmark runs

2 participants