Skip to content

perf(compiler): place auto memos by the routes that load them - #8032

Open
benedikt-bartscher wants to merge 6 commits into
reflex-dev:mainfrom
benedikt-bartscher:auto-memo-route-placement
Open

benedikt-bartscher wants to merge 6 commits into
reflex-dev:mainfrom
benedikt-bartscher:auto-memo-route-placement

Conversation

@benedikt-bartscher

@benedikt-bartscher benedikt-bartscher commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

The memoize plugin mirrors auto memos into modules named after the __module__ of the page function that produced them (#6218, #6457). That fits @rx.memo, but not auto memos, whose page functions rarely say much about which pages use them:

  1. Pages registered through one helper all report the helper's module, so every route imports one memo module holding the memos of all routes:

    # site.py
    def register(app: rx.App, route: str, body) -> None:
        def render() -> rx.Component:
            return body()
    
        app.add_page(render, route=route)
    
    
    site.register(app, "/", page_a)  # page_a's memos inline ~64 KB of literals
    site.register(app, "/b", page_b)

    /b loads all of /'s memos. Wrappers such as an i18n plugin's per-locale page function have the same effect.

  2. Pages in separate modules each get their own copy of the memos they share (a navbar used by pages_a.py and pages_b.py is compiled into both modules), so every page change downloads it again.

This PR places auto memos by the routes that load them instead. Explicit @rx.memo keeps mirroring its source module.

  • A page imports its auto memos from its own route module, .web/utils/components/routes/<page>.jsx, so a page's output never depends on the other pages.
  • After all pages compiled, compile_memo_components computes the routes that load each memo through the compiled import graph, which covers memos nested in other memos, in @rx.memo bodies and in the app root.
  • A memo one route loads is defined in that route's module. Memos loaded by the same set of routes share a module, .web/utils/components/shared/<hash>.jsx, named after the memos it holds so that adding a page doesn't rename it. Memos the app root loads go to its module, which every page loads anyway. Route modules re-export what they don't define.
  • An auto memo's symbol is its content tag, without a module suffix, so identical memos from different modules (e.g. the same chrome rendered under / and /en) merge.
  • Grouping happens in the compiler because Vite 8's output.codeSplitting.groups can't split a module; one module per memo gave the same chunks but built in 21 s instead of 13 s on the site below.

Measured on a prerendered 129-route bilingual site, JS raw / gzip:

first view / / → /pricing / → /en all JS
main 1054 / 213 KB 86 / 15 KB 506 / 57 KB 10522 KB
main, each page function given its own __module__ 720 / 186 KB 105 / 18 KB 170 / 31 KB 12781 KB
this PR 728 / 188 KB 87 / 16 KB 160 / 30 KB 10509 KB

The __module__ workaround is no longer needed: with this PR the build is byte-identical with and without it. The prerendered HTML is unchanged. The first view's extra 8 KB over the workaround is react-router's route manifest, which lists the shared chunks of each route. In the helper repro above, /b drops from 549 to 483 KB raw.

For #6688's compile cache: a reused page's output stays valid when placement changes, and placement only needs each page's memo keys, which the cache already records. Placement follows the compiled import graph, so it gets tighter once #8013 drops descendant imports from passthrough memo modules.

Unnamed rx.client_state() created inside a page function draws a global counter name (cs$0, cs$3, ...), so memos that look identical on two pages differ and stay route-local. They're correct, just not deduplicated; naming them per page deterministically is a separate change.

View guided diff

@FarhanAliRaza FarhanAliRaza added the perf Performance-improving changes label Nov 26, 2026 — with ChatGPT Codex Connector
@greptile-apps

greptile-apps Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge; no blocking issues remain.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no blocking issues remain.

Summary

Auto memos now live in modules chosen by the routes that load them. Explicit @rx.memo components keep their source-module layout.

  • Pages load their own auto memos and share common ones through shared modules.
  • The passthrough memo helper no longer accepts a source module.

Reviews (3) · Last reviewed commit: "test(compiler): assert the route module ..." · Reviewed by Greptile

Comment thread tests/units/compiler/test_compiler.py Resolved
@codspeed

codspeed Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 17 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 146 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing benedikt-bartscher:auto-memo-route-placement (81e162f) with main (6f5c39c)

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. ↩

@benedikt-bartscher
benedikt-bartscher marked this pull request as ready for review November 26, 2026 22:37
@benedikt-bartscher
benedikt-bartscher requested a review from a team as a code owner November 26, 2026 22:37

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 15 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread tests/units/compiler/test_compiler.py Resolved
Comment thread reflex/compiler/plugins/memoize.py Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf Performance-improving changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants