perf(compile): read only set props and cache literal Var dispatch
Merged
FA
2mo ago+382+382+012345678901234567890123456789-61-61-01234567890123456789
Summary
First of three stacked compile-performance PRs. This one is pure hot-path work with no change to generated output.
Component._iter_set_props:_render,_get_vars, and the prop-component scan walked every declared prop (34 to 70 per Radix component) through the field descriptor to find the few that are set. They now iterate the instance dict plus class-level defaults, in declaration order. Literal defaults,@propertyoverrides, and factory defaults still surface._literal_var_for: caches the literal Var class per exact value type instead of copying and walking the twelve-entry isinstance ladder on everyLiteralVar.create. Cleared when a literal subclass registers.insert_app_wrapsshort-circuits on identity before the field-by-field component comparison.CommonTagnow holds the fields and generic render protocol shared by every tag class, withrender(children=None)defined asdict(self.set(children=children)).Tagsubclasses it and overridesrenderwith a direct fast path that produces the same dictionary (tested).CondTag,MatchTag,IterTag, andTaglessinheritCommonTag, soComponent._renderand its overrides are annotated-> CommonTag.render_proppasses strings and dicts straight through.cached_propertykeys use a plainobject()instead ofuuid4.- The memoize plugin's component imports are hoisted to module level (no import cycle exists).
Measurements
Docs site (docs/app, 511 routes), App._compile(dry_run=True) in a fresh process, warm runs:
| state | compile |
|---|---|
| main | 46.5 to 47.1 s |
| this PR | 39.7 to 40.0 s |
cProfile of the same compile: field-descriptor reads fell from 14.9M to 3.1M calls; _render -6.2 s, _get_vars -3.1 s, _create_literal_var -5.1 s, Tag.add_props -2.5 s (profiled time).
Benchmark pages (tests/benchmarks/fixtures.py, full-context compile): complicated page 29.3 ms to 25.0 ms.
Stack
- this PR
- #7122 perf(events): share one chain per handler and trigger across call sites
- #7123 perf(memo): evaluate passthrough bodies once and reuse their analysis
Each later PR's diff includes the earlier ones; merge in order.
Test plan
- New tests: set-prop iteration with defaults, plain-tag render protocol, custom-tag render protocol,
render_proppassthrough, literal dispatch invalidation on late registration,cached_propertyidentity, pickle, and release. uv run pytest tests/units/components tests/units/reflex_base tests/units/compiler tests/units/test_event.pygreen apart from failures that reproduce on cleanmainin this environment.- ruff, pyright, and pre-commit clean.