-
|
At the moment, we unfortunately have massive compile-time performance problems ( It's a simple table that is composed of invocations: {% import "components/product_table.jinja" as product_table %}
{{ product_table::row("row1", []) }}
{{ product_table::row("row2", []) }}
{# ... #}40 lines of this generate 40300 lines of rust code, because each macro invocation inserts the macro code inline. I came up with the idea to convert the offending macro (that is called a lot of times) into its own Template struct: #[derive(Template)]
#[template(ext = "html", source = r#"
{% import "components/product_table.jinja" as product_table %}
{{ product_table::row(title, values) }}
"#)]
pub struct Row<'a> {
title: &'a str,
values: &'a [TyValue<'a>],
help: Option<&'a str>,
}and to then use it "macro-like": {{ ui::Row::new("row1", []) | safe }}
{{ ui::Row::new("row2", []) | safe }}This refactoring reduced the 40300 lines of generated code down to 3000. Now, the The askama documentation states this behaves like expected, but does it make sense? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
|
Since I assume that I'm not the only one with compile time performance issues, maybe we could think of some mechanism to make this escape-hatch more .. elegant? An idea I came up with is introducing the keyword {% macrofn stuff(a, b) %}
{{ a }} {{ b }}
{% endmacro %}which would cause a macro to be expanded only once as its own rust function, instead of being instanced inplace. pub fn stuff<AskamaW, T0, T1>(
__askama_writer: &mut AskamaW,
__askama_values: &dyn askama::Values,
a: T0,
b: T1
) { /* ... */}With the clear disadvantage, that it's variable scope is isolated. Only what is manually passed in is accessible. |
Beta Was this translation helpful? Give feedback.
-
Have a look at the trait impl askama::filters::HtmlSafe for Row<'_> {}Then you don't have to manually mark invocations as |
Beta Was this translation helpful? Give feedback.
Have a look at the trait
HtmlSafe. If you add the line:Then you don't have to manually mark invocations as
| safe.