Skip to content

perf: check class scripts and served files in the cache once a minute, not every render - #366

Merged
JuroOravec merged 1 commit into
mainfrom
fix/dependencies-cache-round-trips
Oct 25, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
fix/dependencies-cache-round-trips

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

Refs #154. This is part 1, the cache round trips. Part 2 (the default cache growing with each distinct css_data() value) and the css_data() stylesheets' round trips follow separately, once the bound is decided.

What changes

Every render asked the cache backend about each component's JS and CSS, and about each served Dependencies file:

  • it read both the versioned and the stable script key back and compared the payloads;
  • it ran has on a served file, then read the file back.

With Redis, that was several network round trips per component per render.

The engine already keeps a record of the compiled Vue asset keys it saw stored, and asks the backend again at most once a minute (citry.cache._store_if_missing). These entries now use the same record:

  • Keys named by their content. The versioned script key and a served file are named by their content, so a key the backend holds holds that content. They use _store_if_missing.
  • The stable key. Its value changes with the class, so a new _store_unless_matched compares it, at most once a minute for one class version, and rewrites it when it differs. Citry never serves the stored stable value, because each worker returns the object built from its own class.
  • Emission uses the served file's content in hand instead of reading the entry back. That removes the TypeError path the issue mentions.
  • A request that finds an entry missing makes its process forget the key. A class script is then written back at once, by the route's existing repair. A served file is written on this process's next render.
  • reset_files() forgets the keys it deletes.
  • The record's order. A checked key moves to the record's recent end, so the size limit drops keys no page links any more.

One contract changes. If the backend drops a served Dependencies file, other workers can answer 404 for it for up to a minute, the same trade-off the Vue assets make. docs_site/content/performance/cache-backends.md ("Temporary 404s") already describes this in general terms.

Tests

New, in tests/test_deps_emission.py:

  • Five renders cost exactly one check and one write per entry. Fails on main.
  • The stable key: a foreign value is kept within the interval, a new class version is compared at once, and after the interval the value is compared and rewritten.
  • reset_files() writes the class entries again at once.
  • A request for an evicted served file returns 404, and this process's next render writes it back.

Mutations were checked against these tests: each new forget, the comparison against the mark, and the compare in _store_unless_matched.

Changed:

Test Reason
test_class_capture_repairs_evicted_shared_cache Locked the old "rewrite on every call" behavior. It now checks that the record is trusted within the interval and that the entries are rewritten after it.
test_a_request_for_an_evicted_version_writes_it_back_at_once (new) Requests the versioned URL after an eviction and gets 200.

Evidence

Run on this branch, based on main 2640d24, with citry_core built from main b3491f0:

  • packages/py/citry/tests and packages/py/citry_ui/tests (-m "not e2e"): 9,585 passed.
  • Two test_render_body_snapshots.py::test_an_error_three_levels_deep_matches_the_snapshot cases fail the same way on origin/main with that citry_core build, because main has newer Rust. PR CI builds fresh Rust.
  • ruff check, ruff format, and mypy (also --platform linux).

This change has no Rust; PR CI runs the cargo phases.

Review

An independent review with a separate prose pass is folded in:

  • the served-file trade-off was named wrongly;
  • a stale cache_component_js docstring and design-doc sentence;
  • the record's order;
  • the missing tests the mutations showed.

🤖 Generated with Claude Code

…, not every render

Every render asked the cache backend about each component's JS and CSS
and each served Dependencies file. It read both the versioned and the
stable key back and compared the payloads, or ran `has` and then read
the served file back. With a shared backend such as Redis, that was
several network round trips per component per render.

The engine already remembers, for the compiled Vue assets, which keys
it saw stored, and asks again at most once a minute
(`_store_if_missing`). These entries now use the same record:
- The versioned script key and a served file are named by their
  content, so they use `_store_if_missing`.
- The stable script key's value changes with the class, so a new
  `_store_unless_matched` compares it, at most once a minute for one
  class version, and rewrites it when it differs.
- Emission uses the served file's content in hand instead of reading
  the entry back, which removes the TypeError path for a missing
  entry.
- A request that finds a versioned key or a served file missing makes
  its process forget the key. A class script is then written back at
  once, and a served file on this process's next render.
- `reset_files()` forgets the keys it deletes.
- A key that is checked stays at the recent end of the record, so the
  record's size limit drops the keys no page links any more.

One contract changes. If the backend drops a served `Dependencies`
file, other workers can answer 404 for it for up to a minute, the same
trade-off the Vue assets make. A class script is still written back by
the first request for it; a render now trusts its record for up to a
minute instead of rewriting it at once, and the test that locked the
old behavior now locks the new one.

The css_data() stylesheets are left for a separate change, with the
bound #154 asks for.

Refs #154

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit c100d18 into main Oct 25, 2026
14 of 27 checks passed
@JuroOravec
JuroOravec deleted the fix/dependencies-cache-round-trips branch October 25, 2026 08:49
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