askama/bench-build
René Kijewski 6ce85f318c Make code generator re-usable for other projects
This PR
* removes the crate `askama_derive_standalone`,
* makes `askama_derive` a normal library, and
* adds the proc-macro crate `askama_macros`,

Before, it was not possible for another crate to re-export
`askama::Template` in a useful way, because the generated code assumes
that it has access to an `extern crate askama`.

`askama_derive` will export the function `derive_template()` like
`askama_derive_standalone` did, but it has an additional argument to
accept a `TokenStream` that should contain (an) statement(s) to define
the identifier `askama`, e.g. `quote! { extern crate askama; }`.

The new proc-macro crate `askama_macros` now defines the derive-macro
`Template` by calling `askama_derive::derive_template()`.

Prior art: [`encase`] → [`encase_derive`] → [`encase_derive_impl`];
[2298a3e].

[`encase`]: <https://crates.io/crates/encase/0.11.0>
[`encase_derive`]: <https://crates.io/crates/encase_derive/0.11.0>
[`encase_derive_impl`]: <https://crates.io/crates/encase_derive_impl/0.11.0>
[2298a3e]: <2298a3efd5>
2025-05-17 13:20:21 +02:00
..
2025-02-09 17:47:17 +01:00
2025-02-09 17:47:17 +01:00
2025-02-09 17:47:17 +01:00
2025-02-09 17:47:17 +01:00
2025-02-09 17:47:17 +01:00
2025-02-09 17:47:17 +01:00
2025-02-09 17:47:17 +01:00
2025-04-22 23:37:23 +02:00
2025-02-09 17:47:17 +01:00
2025-02-09 17:47:17 +01:00

Run the script ./run.sh in this directory to compare the compile compile of askama

  • uses feature derive vs
  • it does not use that feature.

The output might look like:

Benchmark 1: cargo run --features=derive
  Time (mean ± σ):      3.378 s ±  0.041 s    [User: 7.944 s, System: 1.018 s]
  Range (min … max):    3.345 s …  3.424 s    3 runs
 
Benchmark 2: cargo run
  Time (mean ± σ):      3.283 s ±  0.130 s    [User: 8.400 s, System: 1.091 s]
  Range (min … max):    3.141 s …  3.398 s    3 runs
 
Summary
  cargo run ran
    1.03 ± 0.04 times faster than cargo run --features=derive

----------

Benchmark 1: cargo run --release --features=derive
  Time (mean ± σ):      4.733 s ±  0.050 s    [User: 9.026 s, System: 0.749 s]
  Range (min … max):    4.689 s …  4.788 s    3 runs
 
Benchmark 2: cargo run --release
  Time (mean ± σ):      4.504 s ±  0.032 s    [User: 9.010 s, System: 0.733 s]
  Range (min … max):    4.481 s …  4.541 s    3 runs
 
Summary
  cargo run --release ran
    1.05 ± 0.01 times faster than cargo run --release --features=derive

This shows that while it is less convenient for small projects it might be better to use the following setup. This might be especially true if you are using askama in a library. Without the feature, cargo will be able to compile more dependencies in parallel.

# Cargo.toml
[dependencies]
askama = { version = "0.14.0", default-features = false, features = ["std"] }
askama_derive = { version = "0.14.0", features = ["std"] }
// lib.rs
use askama::Template as _;
use askama_derive::Template;

The script uses hyperfine. Install it with cargo install hyperfine.