perf(compile): read only set props and cache literal Var dispatch

Merged
FA
2mo ago
+382-61

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, @property overrides, 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 every LiteralVar.create. Cleared when a literal subclass registers.
  • insert_app_wraps short-circuits on identity before the field-by-field component comparison.
  • CommonTag now holds the fields and generic render protocol shared by every tag class, with render(children=None) defined as dict(self.set(children=children)). Tag subclasses it and overrides render with a direct fast path that produces the same dictionary (tested). CondTag, MatchTag, IterTag, and Tagless inherit CommonTag, so Component._render and its overrides are annotated -> CommonTag. render_prop passes strings and dicts straight through.
  • cached_property keys use a plain object() instead of uuid4.
  • 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:

statecompile
main46.5 to 47.1 s
this PR39.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

  1. this PR
  2. #7122 perf(events): share one chain per handler and trigger across call sites
  3. #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_prop passthrough, literal dispatch invalidation on late registration, cached_property identity, pickle, and release.
  • uv run pytest tests/units/components tests/units/reflex_base tests/units/compiler tests/units/test_event.py green apart from failures that reproduce on clean main in this environment.
  • ruff, pyright, and pre-commit clean.

https://claude.ai/code/session_01PmizE1eQhtYZyVs1RK2ke3