Skip to content

Optimization: Avoid quadratic behavior in IdMaker.make_unique_parameterset_ids#14739

Open
NoxiousTab wants to merge 2 commits into
pytest-dev:mainfrom
NoxiousTab:optimize-idmaker-dedup
Open

Optimization: Avoid quadratic behavior in IdMaker.make_unique_parameterset_ids#14739
NoxiousTab wants to merge 2 commits into
pytest-dev:mainfrom
NoxiousTab:optimize-idmaker-dedup

Conversation

@NoxiousTab

Copy link
Copy Markdown

The Problem

IdMaker.make_unique_parameterset_ids (used to generate node ids for
@pytest.mark.parametrize) rebuilt set(resolved_ids) from scratch on
every candidate suffix it tried while de-duplicating colliding ids.
That set-rebuild is O(n), and it sat inside a loop over every parameter
set, making the whole de-duplication step O(n**2) in the number of
parameter sets whenever many of them produce the same id (e.g.
parametrizing with many values that stringify identically, or with
duplicate user-provided ids).

Fix

Build the set of in-use ids once, and update it incrementally as new
ids are assigned, instead of rebuilding it from the list on every
check. This brings the de-duplication step down to O(n).

Testing

  • Verified with an isolated benchmark calling
    make_unique_parameterset_ids() directly with N colliding ids:
    runtime scaling went from ~4x per doubling of N (quadratic) to ~2x
    per doubling (linear), roughly 55x faster at N=8000.
  • Existing testing/python/metafunc.py::TestMetafunc::test_idmaker_*
    tests pass unchanged.
  • Full testing/python/ suite passes (629 passed, 24 skipped, 2 xfailed
    — the xfails are pre-existing and unrelated to this change).

make_unique_parameterset_ids rebuilt set(resolved_ids) from
scratch on every candidate suffix it tried while de-duplicating
parametrize ids. For parametrizations with many colliding ids this
made the method scale O(n^2) with the number of parameter sets.

Build the set of in-use ids once and keep it updated incrementally
instead, bringing this down to linear time. Confirmed with a manual
benchmark, runtime dropped from ~0.64s to ~0.01s, and the
doubling ratio changed from ~4x (quadratic) to ~2x
(linear).
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant