Skip to content

Move capture of context in renderer allowing reuse - #86

Open
Dherse wants to merge 1 commit into
PgBiel:mainfrom
Dherse:perf/constructor-context-captures
Open

Dherse wants to merge 1 commit into
PgBiel:mainfrom
Dherse:perf/constructor-context-captures

Conversation

@Dherse

@Dherse Dherse commented Sep 20, 2026

Copy link
Copy Markdown

Broadly this does the following:

  • Move the constructor's rendering logic into a helper defined once per element, allowing reuse of the captured variables/context and therefore all of its hashing & cloning happening inside of typst. This makes the capture work once per instance to once per element declared.
  • Also replaced a few large .with(...) calls with anonymous closures, allowing their captured environments to benefit from cached hashing while preserving their printed representations.

Performance

Codly’s many-small workload: 120 independent seven-line Rust blocks.

Metric Before After
Median compile time 4.004 s 2.373 s
Median peak RSS 241.9 MiB 232.5 MiB

This is a 40.7% reduction in compile time for this workload.

This should pass the entire CI suite, although I have only tested v0.15.1 so far so we need to see if the CI passes.

Disclaimer

Generative AI was used in the making of the original code: it found the optimization potential and I implemented part of the changes, with other parts being done directly by genAI.

This translate to a substential reduction in constructor call costs as the body no longer needs to always re-capture (and re-hash/clone) the entire context. This can save substential performance.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant