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

Cache generated code #689

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 4 additions & 1 deletion askama_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ with-tide = []
with-warp = []

[dependencies]
base64 = "=0.13"
blake2 = "=0.10"
indexmap = "=1"
mime = "0.3"
mime_guess = "2"
nom = "7"
proc-macro2 = "1"
quote = "1"
serde = { version = "1.0", optional = true, features = ["derive"] }
syn = "1"
syn = { version = "1", features = ["extra-traits"] }
toml = { version = "0.5", optional = true }
9 changes: 9 additions & 0 deletions askama_derive/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use std::{env, fs};

fn main() {
println!("cargo:rerun-if-changed=build.rs");
let out_dir = env::var("OUT_DIR").unwrap();
let _ = fs::create_dir_all(&out_dir);
println!("cargo:rerun-if-env-changed=OUT_DIR");
println!("cargo:rustc-env=ASKAMA_DERIVE_OUTDIR={}", out_dir);
}
141 changes: 120 additions & 21 deletions askama_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,48 @@ use crate::input::{Print, Source, TemplateInput};
use crate::parser::{parse, Cond, CondTest, Expr, Loop, Node, Target, When, Whitespace, Ws};
use crate::CompileError;

use base64::{encode_config, URL_SAFE_NO_PAD};
use blake2::{Blake2s256, Digest};
use indexmap::map::{Entry, IndexMap};
use proc_macro::TokenStream;
use quote::{quote, ToTokens};

use std::collections::HashMap;
use std::hash::Hash;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::{cmp, hash, mem, str};
use std::{cmp, fs, hash, mem, str};

/// The actual implementation for askama_derive::Template
pub(crate) fn derive_template(input: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(input).unwrap();
match build_template(&ast) {
Ok(source) => source.parse().unwrap(),
Ok(stream) => stream,
Err(e) => e.into_compile_error(),
}
}

#[derive(Default)]
struct BlakeHash(Blake2s256);

impl hash::Hasher for BlakeHash {
fn finish(&self) -> u64 {
0
}

fn write(&mut self, bytes: &[u8]) {
self.0.update(&bytes.len().to_ne_bytes());
self.0.update(bytes);
}
}

/// Takes a `syn::DeriveInput` and generates source code for it
///
/// Reads the metadata from the `template()` attribute to get the template
/// metadata, then fetches the source from the filesystem. The source is
/// parsed, and the parse tree is fed to the code generator. Will print
/// the parse tree and/or generated source according to the `print` key's
/// value as passed to the `template()` attribute.
fn build_template(ast: &syn::DeriveInput) -> Result<String, CompileError> {
fn build_template(ast: &syn::DeriveInput) -> Result<TokenStream, CompileError> {
let template_args = TemplateArgs::new(ast)?;
let config_toml = read_config_file(&template_args.config_path)?;
let config = Config::new(&config_toml)?;
Expand All @@ -37,19 +55,79 @@ fn build_template(ast: &syn::DeriveInput) -> Result<String, CompileError> {
Source::Path(_) => get_template_source(&input.path)?,
};

let mut sources = HashMap::new();
let mut sources = IndexMap::new();
find_used_templates(&input, &mut sources, source)?;

let mut parsed = HashMap::new();
let mut parsed = IndexMap::new();
for (path, src) in &sources {
parsed.insert(path.as_path(), parse(src, input.syntax)?);
}

let mut contexts = HashMap::new();
let mut contexts = IndexMap::new();
for (path, nodes) in &parsed {
contexts.insert(*path, Context::new(input.config, path, nodes)?);
}

let filename = &{
let mut hash = BlakeHash::default();
macro_rules! features {
($($feature:literal)*) => {
$(
#[cfg(feature = $feature)]
$feature.hash(&mut hash);
)*
};
}
features! {
"config"
"humansize"
"markdown"
"urlencode"
"serde-json"
"serde-yaml"
"num-traits"
"with-actix-web"
"with-axum"
"with-gotham"
"with-mendes"
"with-rocket"
"with-tide"
"with-warp"
};

ast.hash(&mut hash);
for kv in &contexts {
kv.hash(&mut hash);
}
let mut filename = encode_config(&hash.0.finalize(), URL_SAFE_NO_PAD);
filename.push_str(".rs");
AsRef::<Path>::as_ref(&env!("ASKAMA_DERIVE_OUTDIR")).join(filename)
};
let result = {
let filename = filename.to_str().unwrap();
quote! {
const _: () = ::core::include!(#filename);
}
.into()
};

let mut tempfile = fs::OpenOptions::new()
.create(true)
.write(true)
.open(filename)
.map_err(|err| {
CompileError::from(format!("could not open temp file {:?}: {}", filename, err))
})?;
let tempfile_length = tempfile
.metadata()
.map_err(|err| {
CompileError::from(format!("could not query temp file {:?}: {}", filename, err))
})?
.len();
if tempfile_length > 32 {
return Ok(result);
}

let ctx = &contexts[input.path.as_path()];
let heritage = if !ctx.blocks.is_empty() || ctx.extends.is_some() {
Some(Heritage::new(ctx, &contexts))
Expand All @@ -72,7 +150,22 @@ fn build_template(ast: &syn::DeriveInput) -> Result<String, CompileError> {
if input.print == Print::Code || input.print == Print::All {
eprintln!("{}", code);
}
Ok(code)

if let Err(err) = tempfile.set_len(0) {
return Err(CompileError::from(format!(
"could not clear temp file {:?}: {}",
filename, err,
)));
}
if let Err(err) = tempfile.write_all(code.as_bytes()) {
let _ = tempfile.set_len(0);
return Err(CompileError::from(format!(
"could not write temp file {:?}: {}",
filename, err,
)));
}

Ok(result)
}

#[derive(Default)]
Expand Down Expand Up @@ -192,7 +285,7 @@ impl TemplateArgs {

fn find_used_templates(
input: &TemplateInput<'_>,
map: &mut HashMap<PathBuf, String>,
map: &mut IndexMap<PathBuf, String>,
source: String,
) -> Result<(), CompileError> {
let mut dependency_graph = Vec::new();
Expand Down Expand Up @@ -229,11 +322,12 @@ fn find_used_templates(
}
Ok(())
}
struct Generator<'a, S: std::hash::BuildHasher> {

struct Generator<'a> {
// The template input state: original struct AST and attributes
input: &'a TemplateInput<'a>,
// All contexts, keyed by the package-relative template path
contexts: &'a HashMap<&'a Path, Context<'a>, S>,
contexts: &'a IndexMap<&'a Path, Context<'a>>,
// The heritage contains references to blocks and their ancestry
heritage: Option<&'a Heritage<'a>>,
// Variables accessible directly from the current scope (not redirected to context)
Expand All @@ -256,14 +350,14 @@ struct Generator<'a, S: std::hash::BuildHasher> {
whitespace: WhitespaceHandling,
}

impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
impl<'a> Generator<'a> {
fn new<'n>(
input: &'n TemplateInput<'_>,
contexts: &'n HashMap<&'n Path, Context<'n>, S>,
contexts: &'n IndexMap<&'n Path, Context<'n>>,
heritage: Option<&'n Heritage<'_>>,
locals: MapChain<'n, &'n str, LocalMeta>,
whitespace: WhitespaceHandling,
) -> Generator<'n, S> {
) -> Generator<'n> {
Generator {
input,
contexts,
Expand All @@ -278,7 +372,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
}
}

fn child(&mut self) -> Generator<'_, S> {
fn child(&mut self) -> Generator<'_> {
let locals = MapChain::with_parent(&self.locals);
Self::new(
self.input,
Expand All @@ -292,6 +386,8 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
// Takes a Context and generates the relevant implementations.
fn build(mut self, ctx: &'a Context<'_>) -> Result<String, CompileError> {
let mut buf = Buffer::new(0);
buf.writeln("{")?;

if !ctx.blocks.is_empty() {
if let Some(parent) = self.input.parent {
self.deref_to_parent(&mut buf, parent)?;
Expand All @@ -316,6 +412,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
#[cfg(feature = "with-warp")]
self.impl_warp_reply(&mut buf)?;

buf.writeln("}")?;
Ok(buf.buf)
}

Expand Down Expand Up @@ -575,6 +672,9 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
}
let (_, orig_ty_generics, _) = self.input.ast.generics.split_for_impl();
let (impl_generics, _, where_clause) = generics.split_for_impl();

buf.writeln("#[allow(warnings)]")?;
buf.writeln("#[allow(clippy::warnings)]")?;
buf.writeln(
format!(
"{} {} for {}{} {{",
Expand Down Expand Up @@ -1173,7 +1273,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
let mut size_hint = 0;
let mut buf_format = Buffer::new(0);
let mut buf_expr = Buffer::new(buf.indent + 1);
let mut expr_cache = HashMap::with_capacity(self.buf_writable.len());
let mut expr_cache = IndexMap::with_capacity(self.buf_writable.len());
for s in mem::take(&mut self.buf_writable) {
match s {
Writable::Lit(s) => {
Expand All @@ -1192,7 +1292,6 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
),
};

use std::collections::hash_map::Entry;
let id = match expr_cache.entry(expression.clone()) {
Entry::Occupied(e) => *e.get(),
Entry::Vacant(e) => {
Expand Down Expand Up @@ -1961,7 +2060,7 @@ where
K: cmp::Eq + hash::Hash,
{
parent: Option<&'a MapChain<'a, K, V>>,
scopes: Vec<HashMap<K, V>>,
scopes: Vec<IndexMap<K, V>>,
}

impl<'a, K: 'a, V: 'a> MapChain<'a, K, V>
Expand All @@ -1971,14 +2070,14 @@ where
fn new() -> MapChain<'a, K, V> {
MapChain {
parent: None,
scopes: vec![HashMap::new()],
scopes: vec![IndexMap::new()],
}
}

fn with_parent<'p>(parent: &'p MapChain<'_, K, V>) -> MapChain<'p, K, V> {
MapChain {
parent: Some(parent),
scopes: vec![HashMap::new()],
scopes: vec![IndexMap::new()],
}
}

Expand Down Expand Up @@ -2014,7 +2113,7 @@ where
}

fn push(&mut self) {
self.scopes.push(HashMap::new());
self.scopes.push(IndexMap::new());
}

fn pop(&mut self) {
Expand Down
37 changes: 27 additions & 10 deletions askama_derive/src/heritage.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use indexmap::IndexMap;

use crate::config::Config;
use crate::parser::{Expr, Loop, Macro, Node};
use crate::CompileError;
Expand All @@ -11,9 +12,9 @@ pub(crate) struct Heritage<'a> {
}

impl Heritage<'_> {
pub(crate) fn new<'n, S: std::hash::BuildHasher>(
pub(crate) fn new<'n>(
mut ctx: &'n Context<'n>,
contexts: &'n HashMap<&'n Path, Context<'n>, S>,
contexts: &'n IndexMap<&'n Path, Context<'n>>,
) -> Heritage<'n> {
let mut blocks: BlockAncestry<'n> = ctx
.blocks
Expand All @@ -32,14 +33,30 @@ impl Heritage<'_> {
}
}

type BlockAncestry<'a> = HashMap<&'a str, Vec<(&'a Context<'a>, &'a Node<'a>)>>;
type BlockAncestry<'a> = IndexMap<&'a str, Vec<(&'a Context<'a>, &'a Node<'a>)>>;

pub(crate) struct Context<'a> {
pub(crate) nodes: &'a [Node<'a>],
pub(crate) extends: Option<PathBuf>,
pub(crate) blocks: HashMap<&'a str, &'a Node<'a>>,
pub(crate) macros: HashMap<&'a str, &'a Macro<'a>>,
pub(crate) imports: HashMap<&'a str, PathBuf>,
pub(crate) blocks: IndexMap<&'a str, &'a Node<'a>>,
pub(crate) macros: IndexMap<&'a str, &'a Macro<'a>>,
pub(crate) imports: IndexMap<&'a str, PathBuf>,
}

impl<'a> std::hash::Hash for Context<'a> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.nodes.hash(state);
self.extends.hash(state);
for kv in self.blocks.iter() {
kv.hash(state);
}
for kv in self.macros.iter() {
kv.hash(state);
}
for kv in self.imports.iter() {
kv.hash(state);
}
}
}

impl Context<'_> {
Expand All @@ -50,8 +67,8 @@ impl Context<'_> {
) -> Result<Context<'n>, CompileError> {
let mut extends = None;
let mut blocks = Vec::new();
let mut macros = HashMap::new();
let mut imports = HashMap::new();
let mut macros = IndexMap::new();
let mut imports = IndexMap::new();
let mut nested = vec![nodes];
let mut top = true;

Expand Down Expand Up @@ -104,7 +121,7 @@ impl Context<'_> {
top = false;
}

let blocks: HashMap<_, _> = blocks
let blocks: IndexMap<_, _> = blocks
.iter()
.map(|def| {
if let Node::BlockDef(_, name, _, _) = def {
Expand Down
Loading