jinja : reduce template rendering overhead (#32023)

* jinja : reduce value allocation and lookup overhead

Assisted-by: GPT-6 Astra

* jinja : address rendering review feedback

Assisted-by: GPT-6 Astra

* jinja : test multipart strings and simplify lookup

Assisted-by: GPT-6 Astra

* fix "undefined hint"

* fix divisibleby 0

* clean up

---------

Co-authored-by: Xuan Son Nguyen <son@huggingface.co>
This commit is contained in:
HaoJun ZHANGandXuan Son Nguyen authored and GitHub committed 2026-11-14 13:34:20 +01:00
1 parent 0cce7580b8
commit 4f2187e73d
7 files changed
+408 -165

No files matched your search

+115
View File
@@ -2,6 +2,7 @@
#include <iostream>
#include <random>
#include <cstdlib>
#include <future>
#include "chat-tts-norm.h"
#include "json.h"
@@ -42,6 +43,8 @@ static void test_string_parts(testing & t);
static void test_fuzzing(testing & t);
static void test_tts_norm_patterns(testing & t);
static void test_shared_render(testing & t);
static bool g_python_mode = false;
int main(int argc, char *argv[]) {
@@ -79,6 +82,7 @@ int main(int argc, char *argv[]) {
t.test("object methods", test_object_methods);
t.test("tts norm patterns", test_tts_norm_patterns);
if (!g_python_mode) {
t.test("shared render", test_shared_render);
t.test("hasher", test_hasher);
t.test("stats", test_stats);
t.test("caps", test_caps);
@@ -89,6 +93,45 @@ int main(int argc, char *argv[]) {
return t.summary();
}
static void test_shared_render(testing & t) {
t.test("shared AST with stats and input marking", [](testing & t) {
const std::string source = "{% macro emit(x) %}{{ x }}{% endmacro %}{% for i in range(2) %}{{ emit(prefix) }}{{ i }}{% endfor %}{% filter upper %}{{ body }}{% endfilter %}|{{ 'literal' is string }}|{{ obj.value }}";
const auto program = jinja::parse_from_tokens(jinja::lexer().tokenize(source));
std::vector<std::future<bool>> workers;
for (int worker = 0; worker < 8; ++worker) {
workers.push_back(std::async(std::launch::async, [&, worker] {
const auto prefix = "p" + std::to_string(worker);
for (int i = 0; i < 25; ++i) {
jinja::context ctx(source);
ctx.is_get_stats = i % 2 != 0;
const json input = {
{ "prefix", prefix },
{ "body", "hi" },
{ "obj", { { "value", "V" } } }
};
jinja::global_from_json(ctx, input, true);
const auto result = jinja::runtime::gather_string_parts(jinja::runtime(ctx).execute(program));
if (result->as_string().str() != prefix + "0" + prefix + "1HI|True|V") {
return false;
}
if (ctx.get_val("obj")->at("value")->stats.used != ctx.is_get_stats) {
return false;
}
if (result->val_str.parts.empty() || !result->val_str.parts.front().is_input ||
!result->val_str.parts.back().is_input) {
return false;
}
}
return true;
}));
}
for (auto & worker : workers) {
t.assert_true("request-local state", worker.get());
}
});
}
static void test_whitespace_control(testing & t) {
test_template(t, "trim_blocks removes newline after tag",
"{% if true %}\n"
@@ -502,6 +545,13 @@ static void test_expressions(testing & t) {
"c"
);
test_template(t, "array index boundaries",
"{% for items in arrays %}{% for i in indices %}{{ items[i] | default('_') }}|{% endfor %};{% endfor %}",
{{"arrays", json::array({json::array({"a", "b", "c"}), json::array()})},
{"indices", json::array({INT64_MIN, -4, -3, -1, 0, 2, 3, INT64_MAX})}},
"_|_|a|c|a|c|_|_|;_|_|_|_|_|_|_|_|;"
);
test_template(t, "array bool access",
"{{ items[true] }}",
{{"items", json::array({"a", "b", "c"})}},
@@ -575,6 +625,14 @@ static void test_expressions(testing & t) {
"('c', 'b', 'a')"
);
test_template(t, "multipart string comparison and access",
"{% set left = a + 'bc' %}{% set right = 'ab' + c %}{{ left == right }}|"
"{{ left[0] }}{{ left[1] }}{{ left[2] }}|{{ right[1:3] }}|"
"{{ left[3] is undefined }}|{{ empty[0] is undefined }}",
{{"a", "a"}, {"c", "c"}, {"empty", ""}},
"True|abc|bc|True|True"
);
test_template(t, "string slice negative step",
"{{ 'abcdef'[::-2] }}",
json::object(),
@@ -633,6 +691,31 @@ static void test_expressions(testing & t) {
"{{ " + std::string(1000, '(') + "1" + std::string(1000, ')') + " }}",
"template is nested more than 128 levels deep"
);
test_template_error(t, "undefined hint: missing object key (attr)",
"{% set m = {'a': 1} %}{{ m.missing_key() }}",
"Callee is not a function: got Undefined (hint: 'missing_key')"
);
test_template_error(t, "undefined hint: missing object key (subscript)",
"{% set m = {'a': 1} %}{{ m['missing_key']() }}",
"Callee is not a function: got Undefined (hint: 'missing_key')"
);
test_template_error(t, "undefined hint: unknown array attribute",
"{% set a = [1] %}{{ a.missing_key() }}",
"Callee is not a function: got Undefined (hint: 'missing_key')"
);
test_template_error(t, "undefined hint: unknown string attribute",
"{% set s = 'x' %}{{ s.missing_key() }}",
"Callee is not a function: got Undefined (hint: 'missing_key')"
);
test_template_error(t, "undefined hint: unknown int attribute",
"{% set n = 1 %}{{ n.missing_key() }}",
"Callee is not a function: got Undefined (hint: 'missing_key')"
);
}
static void test_set_statement(testing & t) {
@@ -1440,6 +1523,17 @@ static void test_tests(testing & t) {
"yes"
);
test_template(t, "is divisibleby -1 with INT64_MIN",
"{{ x is divisibleby(-1) }}",
{{"x", INT64_MIN}},
"True"
);
test_template_error(t, "is divisibleby zero",
"{{ 10 is divisibleby(0) }}",
"divisibleby: division by zero"
);
test_template(t, "is eq",
"{{ 'yes' if 3 is eq(3) }}",
json::object(),
@@ -2730,6 +2824,27 @@ static void test_string_parts(testing & t) {
return runtime.gather_string_parts(runtime.execute(ast))->as_string();
};
t.test("comparison across part boundaries", [](testing & t) {
jinja::string left, right;
left.parts = {{true, "a"}, {false, "bc"}};
right.parts = {{false, "ab"}, {true, "c"}};
t.assert_true("equal text with different partitions", left.equals(right) && right.equals(left));
right.parts = {{false, ""}, {false, "ab"}, {true, ""}, {true, "c"}, {false, ""}};
t.assert_true("empty parts do not change equality", left.equals(right) && right.equals(left));
t.assert_true("different bytes are unequal", !left.equals(jinja::string("abd")));
t.assert_true("prefixes are unequal", !left.equals(jinja::string("ab")) && !jinja::string("ab").equals(left));
jinja::string empty;
empty.parts = {{true, ""}, {false, ""}};
t.assert_true("empty partitions equal an empty string", empty.equals(jinja::string()) && jinja::string().equals(empty));
auto object = jinja::mk_val<jinja::value_object>();
object->insert(jinja::mk_val<jinja::value_string>(left), jinja::mk_val<jinja::value_int>(7));
const auto found = object->find(jinja::mk_val<jinja::value_string>(right));
t.assert_true("object keys ignore partition boundaries", found && found->as_int() == 7);
});
t.test("merge joins only the neighbours with the same type", [](testing & t) {
// "AB" comes from the input and merges, "-" comes from the template and must not
jinja::string res = render("{{ val.a }}{{ val.b }}-{{ val.c }}",