Skip to content

Stop rendering once render_block has its block - #1053

Open
patrick-immometrica wants to merge 1 commit into
Keats:masterfrom
patrick-immometrica:perf/render-block-early-exit
Open

patrick-immometrica wants to merge 1 commit into
Keats:masterfrom
patrick-immometrica:perf/render-block-early-exit

Conversation

@patrick-immometrica

Copy link
Copy Markdown

render_block interprets the whole template into io::sink() and keeps the requested block from block_buffer. Everything after the block, often the bulk of the page, is still evaluated and then thrown away.

Block names are unique and the parser doesn't allow blocks inside for loops, so once the requested block has been rendered nothing that follows can change it. This sets a flag on the state when the block is captured, and the RenderBlock arm returns right after it, which unwinds through any outer blocks too.

One behaviour change: an error that only happens after the block (an undefined variable further down, say) no longer fails render_block. I think that's what you'd expect when asking for one block, and it's what the new test checks, for a plain template, a nested block and a child template using super(). An error inside the block itself still surfaces.

Before/after for a template with a small header block followed by a 2,000-row loop (release build, µs per call, three runs):

before after
render_block("page.html", "header") 149–407 0.1–0.3
render("page.html") 618–811 336–654

Full renders don't change; the spread there is just noise.

render_block rendered the whole template into a sink and only kept the
requested block, so everything after the block was evaluated for
nothing. Blocks can't be inside loops and their names are unique, so
once the block is captured nothing later can change it and we can
unwind straight away.
@patrick-immometrica
patrick-immometrica marked this pull request as ready for review October 24, 2026 21:13

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