Skip to content

perf: use set for O(1) membership check in TestFiles.add()#2130

Merged
KRRT7 merged 2 commits intocf-perf-improvementsfrom
cf-perf-set-test-files
May 7, 2026
Merged

perf: use set for O(1) membership check in TestFiles.add()#2130
KRRT7 merged 2 commits intocf-perf-improvementsfrom
cf-perf-set-test-files

Conversation

@KRRT7
Copy link
Copy Markdown
Collaborator

@KRRT7 KRRT7 commented May 7, 2026

Summary

Add _seen_paths: set[Path] private attribute to TestFiles for O(1) duplicate detection.

Part of #2132 — targeted E2E pipeline performance improvements.

Problem

TestFiles.add() checked if test_file not in self.test_files which 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 on instrumented_behavior_file_path (always unique per TestFile). O(1) set membership check replaces O(n) Pydantic equality scan. Uses PrivateAttr so the set doesn't appear in serialization or equality.

Changes

  • codeflash/models/models.py: Add PrivateAttr set, model_post_init, and O(1) check in add()
  • tests/test_test_files_add.py: 3 tests covering add, duplicate rejection, and bulk adds

Test plan

  • uv run pytest tests/test_test_files_add.py -v
  • Behavior identical — same ValueError raised on duplicates

KRRT7 added 2 commits May 6, 2026 22:01
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.
@KRRT7 KRRT7 merged commit 32c627c into cf-perf-improvements May 7, 2026
27 of 29 checks passed
@KRRT7 KRRT7 deleted the cf-perf-set-test-files branch May 7, 2026 03:03
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.

1 participant