perf: use set for O(1) membership check in TestFiles.add()#2130
Merged
KRRT7 merged 2 commits intocf-perf-improvementsfrom May 7, 2026
Merged
perf: use set for O(1) membership check in TestFiles.add()#2130KRRT7 merged 2 commits intocf-perf-improvementsfrom
KRRT7 merged 2 commits intocf-perf-improvementsfrom
Conversation
The previous implementation used `if test_file not in self.test_files` which performs O(n) equality comparison across all TestFile objects. Replace with a `_seen_paths` set keyed on `instrumented_behavior_file_path` for O(1) deduplication lookups.
This was referenced May 7, 2026
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.
Summary
Add
_seen_paths: set[Path]private attribute toTestFilesfor O(1) duplicate detection.Part of #2132 — targeted E2E pipeline performance improvements.
Problem
TestFiles.add()checkedif test_file not in self.test_fileswhich triggers Pydantic__eq__on every element in the list — O(n) per add. With tens of test files per function, this becomes quadratic in aggregate.Solution
Maintain a
_seen_paths: set[Path]keyed oninstrumented_behavior_file_path(always unique per TestFile). O(1) set membership check replaces O(n) Pydantic equality scan. UsesPrivateAttrso the set doesn't appear in serialization or equality.Changes
codeflash/models/models.py: AddPrivateAttrset,model_post_init, and O(1) check inadd()tests/test_test_files_add.py: 3 tests covering add, duplicate rejection, and bulk addsTest plan
uv run pytest tests/test_test_files_add.py -v