Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip caching calls (Resolves #667) #668

Merged
merged 2 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions askama_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::CompileError;
use proc_macro::TokenStream;
use quote::{quote, ToTokens};

use std::collections::HashMap;
use std::collections::hash_map::{Entry, HashMap};
use std::path::{Path, PathBuf};
use std::{cmp, hash, mem, str};

Expand Down Expand Up @@ -1197,10 +1197,9 @@ impl<'a> Generator<'a> {
),
};

use std::collections::hash_map::Entry;
let id = match expr_cache.entry(expression.clone()) {
Entry::Occupied(e) => *e.get(),
Entry::Vacant(e) => {
Entry::Occupied(e) if s.is_cachable() => *e.get(),
e => {
let id = self.named;
self.named += 1;

Expand All @@ -1209,7 +1208,10 @@ impl<'a> Generator<'a> {
buf_expr.write(&expression);
buf_expr.writeln(",")?;

e.insert(id);
if let Entry::Vacant(e) = e {
e.insert(id);
}

id
}
};
Expand Down
33 changes: 33 additions & 0 deletions askama_derive/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,39 @@ impl Expr<'_> {
_ => false,
}
}

/// Returns `true` if the outcome of this expression may be used multiple times in the same
/// `write!()` call, without evaluating the expression again, i.e. the expression should be
/// side-effect free.
pub(crate) fn is_cachable(&self) -> bool {
match self {
// Literals are the definition of pure:
Expr::BoolLit(_) => true,
Expr::NumLit(_) => true,
Expr::StrLit(_) => true,
Expr::CharLit(_) => true,
// fmt::Display should have no effects:
Expr::Var(_) => true,
Expr::Path(_) => true,
// Check recursively:
Expr::Array(args) => args.iter().all(|arg| arg.is_cachable()),
Expr::Attr(lhs, _) => lhs.is_cachable(),
Expr::Index(lhs, rhs) => lhs.is_cachable() && rhs.is_cachable(),
Expr::Filter(_, args) => args.iter().all(|arg| arg.is_cachable()),
Expr::Unary(_, arg) => arg.is_cachable(),
Expr::BinOp(_, lhs, rhs) => lhs.is_cachable() && rhs.is_cachable(),
Expr::Range(_, lhs, rhs) => {
lhs.as_ref().map_or(true, |v| v.is_cachable())
&& rhs.as_ref().map_or(true, |v| v.is_cachable())
}
Expr::Group(arg) => arg.is_cachable(),
Expr::Tuple(args) => args.iter().all(|arg| arg.is_cachable()),
// We have too little information to tell if the expression is pure:
Expr::Call(_, _) => false,
Expr::RustMacro(_, _) => false,
Expr::Try(_) => false,
}
}
}

pub(crate) type When<'a> = (Ws, Target<'a>, Vec<Node<'a>>);
Expand Down