Skip to content

Comments

perf: remove redundant project_root_path.resolve() from hot path#1541

Merged
KRRT7 merged 17 commits intomainfrom
narrow-sentry-stdlib-integration
Feb 19, 2026
Merged

perf: remove redundant project_root_path.resolve() from hot path#1541
KRRT7 merged 17 commits intomainfrom
narrow-sentry-stdlib-integration

Conversation

@KRRT7
Copy link
Collaborator

@KRRT7 KRRT7 commented Feb 19, 2026

Summary

  • Resolve project_root_path and tests_project_rootdir once at construction time (TestConfig.__post_init__) instead of on every call to module_name_from_file_path
  • Remove the redundant .resolve() syscall from module_name_from_file_path — eliminating ~776 filesystem syscalls from the hot path
  • Resolve early in FunctionOptimizer.__init__ and filter_functions entry to ensure consistency with symlink-canonical paths

Test plan

  • pytest tests/test_code_utils.py -k module_name_from_file_path — 4 pass
  • pytest tests/test_async_function_discovery.py::test_get_functions_to_optimize_with_async — pass
  • pytest tests/test_formatter.py::test_formatting_file_with_many_diffs — pass
  • pytest tests/test_function_discovery.py::test_filter_functions_tests_root_overlaps_source — pass

KRRT7 and others added 5 commits February 18, 2026 21:57
StdlibIntegration monkeypatches subprocess.Popen, adding span/breadcrumb
overhead to every subprocess call. Profiling showed ~342k samples (~8.9%
of total runtime) in sentry_sdk.integrations.stdlib and sentry_sdk.utils.
Disable subprocess instrumentation and set traces/profiles sample rates
to 0 since codeflash is a CLI tool that doesn't need performance tracing.
Error capturing (capture_exception, capture_message, LoggingIntegration)
remains fully functional.
…arse_test_output

Replace lazy `.*?` quantifiers in matches_re_start/matches_re_end with
negated character classes (`[^:]`, `[^#]`, `[^.:]`) to eliminate
quadratic backtracking. Replace per-line regex search for the pytest
FAILURES header with a simple `"= FAILURES =" in line` string check.
Add tests for the regex patterns and failure header detection.
Resolve project root paths once at construction time (TestConfig.__post_init__,
FunctionOptimizer.__init__, filter_functions entry) instead of on every call to
module_name_from_file_path — eliminating ~776 redundant filesystem syscalls.
StdlibIntegration in sentry-sdk 2.x doesn't accept
subprocess_instrumentation as a parameter. Use disabled_integrations
instead, which also avoids httplib patching overhead.
github-actions bot and others added 3 commits February 19, 2026 03:56
Drop .resolve() from ImportResolver, TestsCache, init_javascript,
create_pr, and filter_functions where callers already pass resolved
paths via CLI init or TestConfig.__post_init__.
KRRT7 and others added 2 commits February 18, 2026 23:05
Drop .resolve() from ImportResolver, TestsCache, init_javascript,
create_pr, and filter_functions where callers already pass resolved
paths via CLI init or TestConfig.__post_init__. Also exclude test
and fixture dirs from mypy to match ruff/ty config.
@claude
Copy link
Contributor

claude bot commented Feb 19, 2026

PR Review Summary

Prek Checks

Passed — Prek found 1 unused import (import os in code_context_extractor.py), already auto-fixed in commit 5efa1eeb.

Mypy

⚠️ Pre-existing issues only — No new mypy errors introduced by this PR. The disabled_integrations=[StdlibIntegration] type mismatch in sentry.py is a sentry SDK type-stub limitation (runtime behavior is correct).

Code Review

No critical issues found in this re-review.

Changes reviewed:

  • TestConfig.__post_init__: Resolves project_root_path and tests_project_rootdir once at construction — correct, dataclass is not frozen
  • sentry.py: Disables StdlibIntegration, sets traces/profiles sample rate to 0 — reduces unnecessary overhead from Sentry's stdlib patching
  • parse_test_output.py: Regex refactored from greedy (.*?) to precise ([^:]*) patterns with documentation; FAILURES header detection simplified from regex to string in check — both correct
  • code_context_extractor.py: is_project_path now uses resolve().relative_to() instead of fragile str.startswith(str + os.sep); definition_path guarded against None from Jedi — correct
  • function_optimizer.py: self.project_root resolved once in __init__; path.resolve() in reformat_code_and_helpers ensures consistency — correct
  • create_pr.py: Removed redundant .resolve() since tests_project_rootdir is now resolved at construction — correct
  • New test file: tests/test_parse_test_output_regex.py (189 lines) — comprehensive coverage of the regex refactoring

All 3 existing inline review comments already resolved (✅ Fixed).

Test Coverage

File Main PR Delta
cli_cmds/init_javascript.py 41% 41%
code_utils/instrument_existing_tests.py 92% 92%
discovery/discover_unit_tests.py 74% 74%
discovery/functions_to_optimize.py 70% 70%
languages/javascript/import_resolver.py 72% 72%
languages/python/context/code_context_extractor.py 91% 92% +1%
optimization/function_optimizer.py 18% 18%
result/create_pr.py 67% 67%
telemetry/sentry.py
verification/parse_test_output.py 58% 58%
verification/verification_utils.py 60% 61% +1%
tests/test_parse_test_output_regex.py (new) 99%

Overall coverage: 78.59% → 78.61% (+0.02pp) — No regression.

New file tests/test_parse_test_output_regex.py has 99% coverage ✅

Note: 8 pre-existing test failures on both branches (7 in test_tracer.py, 1 in test_code_context_extractor.py — the latter is fixed on the remote by commit a726b464 but wasn't in local checkout).


Last updated: 2026-02-19

KRRT7 and others added 4 commits February 18, 2026 23:14
Paths with .. segments (e.g. Path(__file__) / "../foo") don't match
resolved file_path in module_name_from_file_path. Resolve once at the
public entry point and pass down to the async variant.
Also exclude test dirs from mypy config.
…istency

On Windows, Path.resolve() can return 8.3 short names (e.g. RUNNER~1)
that differ from long-name forms. Jedi returns resolved paths that may
not match the project root's form, causing relative_to() failures.

- Normalize Jedi definition paths through the project root via
  safe_relative_to to ensure consistent dict keys
- Resolve both sides in is_project_path and module_name_from_file_path
- Restore resolve in TestsCache for consistent cache keys
@KRRT7
Copy link
Collaborator Author

KRRT7 commented Feb 19, 2026

@claude fix the windows unit tests, fix the root cause

…istency

TestConfig.__post_init__ resolves project_root_path to long names, but
FunctionToOptimize.file_path could retain Windows 8.3 short names
(e.g., RUNNER~1 vs runneradmin), causing relative_to() failures.

Co-authored-by: Kevin Turcios <KRRT7@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@KRRT7
Copy link
Collaborator Author

KRRT7 commented Feb 19, 2026

@claude fix the unit tests failures and push

…olution

FunctionToOptimize is a frozen Pydantic dataclass, so directly assigning
to file_path raises FrozenInstanceError. Use dataclasses.replace() to
create a new instance with the resolved path instead.

Co-authored-by: Kevin Turcios <KRRT7@users.noreply.github.com>
@KRRT7
Copy link
Collaborator Author

KRRT7 commented Feb 19, 2026

@claude fix the failing unit tests

@claude
Copy link
Contributor

claude bot commented Feb 19, 2026

Claude finished @KRRT7's task in 7m 1s —— View job


Fixing Failing Unit Tests

  • Identify failing tests — 9 failures in tests/test_unused_helper_revert.py (Windows only)
  • Analyze root causes
  • Fix the failing tests
  • Push changes

Root cause: Path mismatch in revert_unused_helper_functions (unused_definition_remover.py:597).

On Windows, tempfile.mkdtemp() returns paths with 8.3 short names (e.g., C:\Users\RUNNER~1\...). The original_helper_code dict is keyed with these unresolved paths, but unused_helpers[*].file_path (from Jedi discovery) are resolved to long-form paths. The dict lookup file_path in original_helper_code fails silently, so the unused helpers are never reverted.

Fix (commit 8f65c9f7): Resolve both sides of the path comparison in revert_unused_helper_functions:

  • Resolve original_helper_code keys via {p.resolve(): code for p, code in original_helper_code.items()}
  • Resolve helper.file_path when grouping by file

This ensures consistent path comparison regardless of Windows 8.3 name transformations.


|

…name consistency

On Windows, tempfile.mkdtemp() returns paths with 8.3 short names while
Jedi resolves to long-form paths. The dict lookup for original_helper_code
failed silently, causing unused helpers to never be reverted.

Co-authored-by: Kevin Turcios <KRRT7@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@KRRT7 KRRT7 merged commit 401d24c into main Feb 19, 2026
27 of 28 checks passed
@KRRT7 KRRT7 deleted the narrow-sentry-stdlib-integration branch February 19, 2026 05:59
KRRT7 added a commit that referenced this pull request Feb 19, 2026
…ration

perf: remove redundant project_root_path.resolve() from hot path
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