Optimization: Avoid quadratic behavior in IdMaker.make_unique_parameterset_ids#14739
Open
NoxiousTab wants to merge 2 commits into
Open
Optimization: Avoid quadratic behavior in IdMaker.make_unique_parameterset_ids#14739NoxiousTab wants to merge 2 commits into
NoxiousTab wants to merge 2 commits into
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Problem
IdMaker.make_unique_parameterset_ids(used to generate node ids for@pytest.mark.parametrize) rebuiltset(resolved_ids)from scratch onevery 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
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.
testing/python/metafunc.py::TestMetafunc::test_idmaker_*tests pass unchanged.
testing/python/suite passes (629 passed, 24 skipped, 2 xfailed— the xfails are pre-existing and unrelated to this change).