Skip to content

perf: avoid double dict lookup for line profiler results#2131

Merged
KRRT7 merged 2 commits intocf-perf-improvementsfrom
cf-perf-dict-get-pattern
May 7, 2026
Merged

perf: avoid double dict lookup for line profiler results#2131
KRRT7 merged 2 commits intocf-perf-improvementsfrom
cf-perf-dict-get-pattern

Conversation

@KRRT7
Copy link
Copy Markdown
Collaborator

@KRRT7 KRRT7 commented May 7, 2026

Summary

Replace if key in dict: dict[key] with single .get() call in handle_duplicate_candidate().

Part of #2132 — targeted E2E pipeline performance improvements.

Problem

handle_duplicate_candidate() used the pattern:

if key in optimized_line_profiler_results:
    value = optimized_line_profiler_results[key]

This computes the hash twice and performs two dict lookups. The dict is dict[str, str] so None is never a valid value.

Solution

Single .get() with a guard:

if (value := optimized_line_profiler_results.get(key)) is not None:

One hash, one lookup, same semantics.

Changes

  • codeflash/models/models.py: Single .get() + guard instead of in + subscript
  • tests/test_candidate_evaluation_context.py: 3 tests covering existing key, missing key, and no corruption

Test plan

  • uv run pytest tests/test_candidate_evaluation_context.py -v
  • Behavior identical — same assignment when key exists, no-op when missing

@KRRT7 KRRT7 merged commit 32c627c into cf-perf-improvements May 7, 2026
28 of 30 checks passed
@KRRT7 KRRT7 deleted the cf-perf-dict-get-pattern 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