Repository navigation
Borrow loop and set variable names from the chunk - #1062
Merged
Keats merged 1 commit intoNov 20, 2026
Merged
Conversation
The names of loop variables and of variables assigned with set/set_global were copied into a new String every time a loop started or an assignment ran. They live in the chunk for the whole render, so keep a reference instead.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every time a for loop starts,
StoreLocalcopies the name of the loop variable (and of the key infor k, v in ...) into a newString.{% set %}and{% set_global %}do the same on every execution to have a key to insert with, and when the variable already exists thatStringis dropped again right away. So an inner loop costs one allocation per iteration of the outer one, and an assignment in a loop costs one per iteration.The names live in the instructions of a chunk that is borrowed for the whole render (
&'tera Chunk), soForLoopandState::set_variablescan hold a&'tera strinstead.ForLoopgets a lifetime for that; it is crate-private and the publicStateAPI is untouched.__tera_contextand the list of available variables in the undefined-variable error still build owned strings like before.Allocations per render with 100 products (release build, counting global allocator):
big-tablefrom the benches (100x100)set_globalin a loopsetin a loopIn time, the nested loop goes from 15.6 to 13.3 µs and the
set_globalloop from 6.2 to 4.9 µs (best of three interleaved runs).big-tabledoesn't move, 100 allocations disappear next to 10,000 writes. The output is byte-identical in all of them.