From f146ac920e5080e68efb9c9b443926b7677ccc7c Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Wed, 28 Jun 2023 13:34:07 +0200 Subject: Avoid reparsing included templates if possible --- askama_derive/src/generator.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'askama_derive/src/generator.rs') diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 5e70849..cfd34c5 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -1,7 +1,7 @@ use crate::config::{get_template_source, read_config_file, Config, WhitespaceHandling}; use crate::heritage::{Context, Heritage}; use crate::input::{Print, Source, TemplateInput}; -use crate::parser::{parse, Cond, CondTest, Expr, Loop, Node, Target, When, Whitespace, Ws}; +use crate::parser::{Cond, CondTest, Expr, Loop, Node, Target, When, Whitespace, Ws}; use crate::CompileError; use proc_macro::TokenStream; @@ -1047,8 +1047,19 @@ impl<'a> Generator<'a> { .input .config .find_template(path, Some(&self.input.path))?; - let src = get_template_source(&path)?; - let nodes = parse(&src, self.input.syntax)?; + + enum CowNodes<'a> { + Owned(Parsed), + Borrowed(&'a [Node<'a>]), + } + + let nodes = match self.contexts.get(path.as_path()) { + Some(ctx) => CowNodes::Borrowed(&ctx.nodes), + None => { + let src = get_template_source(&path)?; + CowNodes::Owned(Parsed::new(src, self.input.syntax)?) + } + }; // Make sure the compiler understands that the generated code depends on the template file. { @@ -1065,7 +1076,15 @@ impl<'a> Generator<'a> { // Since nodes must not outlive the Generator, we instantiate // a nested Generator here to handle the include's nodes. let mut gen = self.child(); - let mut size_hint = gen.handle(ctx, &nodes, buf, AstLevel::Nested)?; + let mut size_hint = gen.handle( + ctx, + match &nodes { + CowNodes::Owned(parsed) => parsed.nodes(), + CowNodes::Borrowed(nodes) => nodes, + }, + buf, + AstLevel::Nested, + )?; size_hint += gen.write_buf_writable(buf)?; size_hint }; -- cgit