Skip to content

perf: cache framework-path checks in console.deprecate frame walk#6736

Open
Alek99 wants to merge 2 commits into
mainfrom
claude/reflex-perf-optimizations-21kg8y-deprecate
Open

perf: cache framework-path checks in console.deprecate frame walk#6736
Alek99 wants to merge 2 commits into
mainfrom
claude/reflex-perf-optimizations-21kg8y-deprecate

Conversation

@Alek99

@Alek99 Alek99 commented Jul 10, 2026

Copy link
Copy Markdown
Member

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?

console.deprecate walks the call stack to locate the first non-framework frame so it can report where deprecated usage came from. For every frame on every call — even when the warning is already deduped — it called Path(inspect.getfile(frame)).resolve() (syscalls) and compared the result against ~300 excluded framework/stdlib roots with is_relative_to. That costs multiple milliseconds per call, and deprecated properties on hot paths pay it per access: any handler reading state.router.page eats it on every event. memo.py even carries a local workaround gating its own console.deprecate call for exactly this reason.

Fix: cache the per-filename "is this a framework path?" determination (functools.cache keyed on co_filename — module file locations don't move within a process), and read frame.f_code.co_filename directly instead of inspect.getfile (identical for frames). The frame walk itself stays, so the reported location and the per-call-site dedupe behavior are exactly as before.

The dedupe key includes the call-site location, so a "dedupe-check before walking" shortcut would have changed emission behavior (a second distinct call site would no longer warn); the cache achieves the same goal without any behavior change.

Micro-benchmark (Python 3.13, deduped steady-state deprecate() call through 25 framework-path frames, median of 200 runs):

variant per call
before 5.73 ms
after 0.038 ms

~150x faster. A side-by-side equivalence harness loads the before/after modules together and asserts byte-identical warning output for: two distinct user call sites of the same feature (both warn), repeat calls (deduped), and a second feature (warns once).

Linear: ENG-10100

🤖 Generated with Claude Code

https://claude.ai/code/session_01Sns7EuBPYvfGCxRSRZ1bUx


Generated by Claude Code

console.deprecate resolved and compared every stack frame's path against
~300 excluded framework/stdlib roots on every call, even when the warning
was already deduped -- 5-6ms per call. Deprecated properties on hot paths
(e.g. RouterData.page) paid this on every access from an event handler.

Cache the per-filename framework-root determination (module files do not
move within a process), turning repeat calls into dict lookups: ~6ms ->
~0.04ms (~150x). Emission behavior is unchanged, including one warning
per distinct user-code call site.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sns7EuBPYvfGCxRSRZ1bUx
@Alek99 Alek99 requested a review from a team as a code owner July 10, 2026 19:39
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sns7EuBPYvfGCxRSRZ1bUx
@greptile-apps

greptile-apps Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR speeds up console.deprecate frame walking. The main changes are:

  • Added cached framework-path classification by code filename.
  • Switched frame lookup to frame.f_code.co_filename.
  • Added a performance news entry for the change.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

Filename Overview
packages/reflex-base/src/reflex_base/utils/console.py Adds cached framework-path checks for deprecation frame walking without a concrete behavior regression found.
packages/reflex-base/news/6736.performance.md Documents the deprecation warning performance improvement.

Reviews (1): Last reviewed commit: "chore: add news fragment for #6736" | Re-trigger Greptile

@codspeed-hq

codspeed-hq Bot commented Jul 10, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 26 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing claude/reflex-perf-optimizations-21kg8y-deprecate (6f922e7) with main (9a5c4d3)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@linear-code

linear-code Bot commented Jul 10, 2026

Copy link
Copy Markdown

ENG-10100

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.

3 participants