Skip to content

Introduce caching for strict templates - #189

Merged
oscarotero merged 2 commits into
mainfrom
strict-template-caching
Oct 6, 2026
Merged

oscarotero merged 2 commits into
mainfrom
strict-template-caching

Conversation

@vrugtehagel

@vrugtehagel vrugtehagel commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Currently, in Vento strict mode, templates are re-evaluated every time they are run, because the variables passed to the template can change.

This introduces a "strict cache", i.e. cache for strict mode, which caches templates separately based on the variables passed to them. This improves rendering performance significantly, since most of the time, the templates receive the same set of variables. On my laptop I'm seeing an improvement to the rendering benchmark of more than 50%!

Note that this respects Vento's env.cache.clear() because the variable-based caches are attached to the template objects, which are deleted upon clearing with env.cache.clear().

Currently, in Vento strict mode, templates are re-evaluated every
time they are run, because the variables passed to the template can
change.

This introduces a "strict cache", i.e. cache for strict mode, which
caches templates separately based on the variables passed to them.
This improves performance significantly, since most of the time, the
templates receive the same set of variables.
@oscarotero

Copy link
Copy Markdown
Member

This is a smart solution, thank you!
Can you add tests for this? Maybe just checking the cache keys after calling the same template multiple times with different variables.

@vrugtehagel
vrugtehagel force-pushed the strict-template-caching branch from 711dd31 to 83d8dfb Compare October 5, 2026 21:35
@vrugtehagel

Copy link
Copy Markdown
Contributor Author

Of course! I added a test that checks the keys in strictCache, which doesn't completely prove that it works, but is at least an indication. It's a bit difficult to test it thoroughly without having to make adjustments to the implementation just for testing purposes, but I think this test should do well enough, and really, it doesn't really matter what it does, as long as the benchmarks are good and the rest of the tests pass.

By the way, I found using a Map instead of a plain object to be significantly slower somehow. Maybe just because it's a more expensive data structure, but I thought this was interesting 馃槃

@oscarotero

oscarotero commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Thank you!.

I found using a Map instead of a plain object to be significantly slower somehow.

That's interesting. I suspect Map and Set are faster with big volume or data, but if the object or array has only a few items, maybe it doesn't worth it. At least that's my experience using them. I also use them for inmutable data: data that is defined once and then you only check if it's there, without adding or removing elements. For example here.

@oscarotero
oscarotero merged commit 537160a into main Oct 6, 2026
1 check passed
@oscarotero
oscarotero deleted the strict-template-caching branch October 6, 2026 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants