Skip to content

Vue compiler output repeats each element's full path, so it grows with the square of template depth #537

Open

Description

@JuroOravec

What happens

The Vue compiler wrapper (crates/citry_vue_compiler) writes each element's full path from the template root into its output: every ancestor's segment, such as i:1:74656d706c617465. A segment is i:, the element's position among its siblings and its tag in hex, or k:, its static key and its tag in hex. The same path is written up to three times:

  • elements[].path, in the element metadata;
  • hydrationPlan.elements[].path;
  • hydrationPlan.anchors[].path.

An element at depth d carries d segments, so the output grows with the square of the depth. The Rust side also builds, clones and frees each path several times while it compiles. Found while working on #501.

Measured on a release build, one component (the templates from #501):

Template Output JSON Path bytes
<div>, 512 levels 4.2 MB 1.7 MB in elements, 1.7 MB in hydrationPlan.elements
c-if/c-elif/c-else, 256 levels 8.4 MB 2.3 MB in elements, 3.0 MB in hydrationPlan.anchors

With the per-element hash fix from the #501 PR (#538) applied, compile plus serialization of the c-elif template takes 7.6 ms at 128 levels and 24.5 ms at 256 levels (3.2 times per doubling). With both the paths and the indentation from #536 left out of the output, it takes 4.2 ms and 11.3 ms; I did not measure leaving out the paths alone. The path clones inside the walk still remain in those numbers. Python's json.loads of the output grows the same way: 4 ms at 128 levels and 12 ms at 256 levels.

A release build of the whole citry check grows about 2 times per doubling on these templates, so nobody hits this in practice today. This issue records the fix for when deep templates matter.

Prior art

  • Python validates the hydration paths. _hydration_path (packages/py/citry/citry/_vue/compiler.py:946) turns them into HydrationAnchor.path and HydrationElement.path, and no production code reads those fields. Python identifies an element by its siteId and reads no field of elements[].path.
  • _compiled_content_id (packages/py/citry/citry/_vue/compiler.py:1319-1346) hashes the whole elements and hydrationPlan payloads, paths included, into each component's definition id. That id keys window.__citryRuntimeDefinitions and becomes CompiledRender.id. So the change below alters every component's definition id, not only deep ones, and this json.dumps of the paths is one more cost that grows with the square of the depth, on the render path.
  • No stored cache keeps the hydration plan or the paths. ext/cache/artifact.py:30-41 stores the compiled template text and compiles it again on replay, and its key includes the installed Citry version.
  • Tests read them: packages/py/citry/tests/test_vue_prepared.py:149, :471, :484, :490 and :519 (the invalid-path test, which must be rewritten), and the hydration tests in crates/citry_vue_compiler/src/hydration.rs (about lines 1908 to 2127).
  • No JS package reads them.
  • In Rust, the code after the walk (crates/citry_vue_compiler/src/lib.rs, about lines 459 to 535) finds each element's local calls, slot outlets and fills by filtering every recorded path with starts_with. That costs elements times records times depth.
  • element_position in hydration.rs looks an element up by its opening position and clones the path it finds.

Proposed design

The output schema stays at version 1, and every producer, consumer and test moves in the same change.

  • elements[]: replace path with parent, the index of the parent element in elements (or null at the top level), and segment, the element's own path segment. A consumer that needs a full path can still rebuild it by following parent.
  • hydrationPlan.anchors[] and hydrationPlan.elements[]: replace path with element, an index into elements, or null when the position has no element. Add root: true for an anchor that covers the whole template; today that anchor has path: [].
  • Python: HydrationAnchor.path and HydrationElement.path become element: int | None, plus root on the anchor. The validator checks that each index is in range, and a new check validates parent.
  • Definition ids change for every component, because _compiled_content_id hashes these payloads. Nothing stores them across Citry versions, but snapshot tests that contain ids move.
  • Rust: the walk records elements in source order, so an element's descendants are the indices right after it. The code after the walk compares index ranges instead of path prefixes, and the per-element path clones go away.
  • PyO3: no change. _compile_vue still returns one JSON string.

Alternatives considered

  • Keep the paths and share their segments in memory (Arc<str>). This saves allocations, but the JSON keeps the same size, so serialization and json.loads still grow with the square of the depth.
  • Replace each path with the element's siteId. That is linear and already identifies the element, but the template root and parser-added elements such as tbody need special values, and every lookup needs a map. A parent index is simpler.

Error modes

  • An element index out of range fails validation in Python with the same RuntimeError the validator raises today for a malformed hydration path.
  • A parent index out of range, or one that does not come before its child, fails a new check with the same kind of RuntimeError. Python validates no element path today.
  • A missing element key is treated like any other missing key in the hydration plan: rejected.

What would show this design is wrong

  • A consumer that needs full paths and that the search above missed.
  • The compile still growing about 3 times per doubling after the change, which would mean another part of the walk grows with the square of the depth.

Part of the follow-up from #501.

Activity

  1. added
    next-sprint
    on Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    next-sprint

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions