Skip to content

perf: optimize tracer hot path with string-based path ops and caching#1686

Merged
KRRT7 merged 5 commits intomainfrom
trace_ops
Feb 27, 2026
Merged

perf: optimize tracer hot path with string-based path ops and caching#1686
KRRT7 merged 5 commits intomainfrom
trace_ops

Conversation

@KRRT7
Copy link
Collaborator

@KRRT7 KRRT7 commented Feb 27, 2026

Summary

  • Cache path resolution and validity checks to avoid redundant os.path.realpath and os.path.exists calls on repeated function entries
  • Replace Path.is_relative_to and Path.resolve with os.path.realpath + str.startswith in the hot path for lower overhead
  • Defer Path construction to first-seen functions only, keeping the per-call path as string-based
  • Restore tests-root to tests in pyproject.toml

Test plan

  • Existing tracer tests should pass
  • Profile with a real project to verify reduced overhead in profile_callback

Replace per-call Path.is_relative_to() and Path.exists() (stat syscall)
with a single cached lookup per unique co_filename. Use os.path.realpath
and str.startswith instead of pathlib to avoid object allocation on the
hot path. Profile showed these pathlib ops at ~15% self-time.
…uction

Keep file_name as a plain str throughout the per-call path. Path is only
constructed once per unique function in the first-time-seen branch where
filter_files_optimized and FunctionModules need it.
@KRRT7
Copy link
Collaborator Author

KRRT7 commented Feb 27, 2026

this was causing 10-15% overhead

@claude
Copy link
Contributor

claude bot commented Feb 27, 2026

PR Review Summary

Prek Checks

All checks passed (ruff check, ruff format) — no issues found.

Mypy

48 pre-existing mypy errors in codeflash/tracing/tracing_new_process.py (missing type annotations, untyped dicts, etc.) and 1 in tests/test_worktree.py (missing return type on test function). No new mypy errors introduced by this PR.

Code Review

This PR makes solid performance optimizations to the tracer hot path:

  1. String-based path operations — replaces Path().resolve() with os.path.realpath() and Path.is_relative_to() with str.startswith() in the trace callback. This is the right call since tracer_logic is called on every traced function invocation.

  2. Cached validity checkspath_cache now stores (resolved_path, is_valid) tuples, avoiding repeated Path construction and filesystem checks for already-seen filenames. Invalid paths short-circuit immediately on cache hit.

  3. Deferred Path constructionPath(file_name) is only constructed when seeing a function for the first time (for filter_files_optimized and FunctionModules), not on every call.

Minor issue found:

  • tests/test_worktree.py:63 — Stale comment says tests_root is configured as "codeflash" but pyproject.toml now has "tests" (inline comment posted with suggestion).

No critical bugs, security issues, or breaking API changes detected.

Test Coverage

File Stmts (PR) Miss (PR) Cover (PR) Cover (main) Delta
codeflash/tracing/tracing_new_process.py 508 422 17% 17% 0%
tests/test_worktree.py 48 0 100% 100% 0%
Overall 50042 10810 78% 78% 0%
  • No coverage regression
  • tracing_new_process.py has low coverage (17%) but this is pre-existing — the tracer requires subprocess execution and is difficult to unit test in isolation. The changed lines follow the same coverage pattern as the code they replaced.
  • test_worktree.py has 100% coverage
  • 8 test failures are pre-existing on both main and PR branch (same test_tracer_with_complex_arguments assertion error)

Last updated: 2026-02-27T14:50 UTC

# All paths are relative to this pyproject.toml's directory.
module-root = "codeflash"
tests-root = "codeflash"
tests-root = "tests"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests-root change from "codeflash" to "tests" breaks tests/test_worktree.py::test_mirror_paths_for_worktree_mode which hardcodes the old value:

# tests_root is configured as "codeflash" in pyproject.toml
assert optimizer.args.tests_root == worktree_dir / "codeflash"

The test at lines 63-67 needs to be updated to expect worktree_dir / "tests" instead of worktree_dir / "codeflash".

@@ -61,9 +61,9 @@ def test_mirror_paths_for_worktree_mode(monkeypatch: pytest.MonkeyPatch):
assert optimizer.args.test_project_root == worktree_dir
assert optimizer.args.module_root == worktree_dir / "codeflash"
# tests_root is configured as "codeflash" in pyproject.toml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale comment: says tests_root is configured as "codeflash" in pyproject.toml but it's now "tests".

Suggested change
# tests_root is configured as "codeflash" in pyproject.toml
# tests_root is configured as "tests" in pyproject.toml

@KRRT7 KRRT7 merged commit 50a2538 into main Feb 27, 2026
34 of 36 checks passed
@KRRT7 KRRT7 deleted the trace_ops branch February 27, 2026 15:14
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