Handlebars-Net / Handlebars.Net

A real .NET Handlebars engine
MIT License
1.26k stars 217 forks source link

Many small templates - high memory utilization #494

Closed jimfum closed 2 years ago

jimfum commented 2 years ago

Hi,

Looking for some thoughts here around overall memory consumption when a lot of small templates are needed.

Our email project has a need to process many templates. We tried to optimize memory usage so templates share as much as they have in common. Current attempt, when abstracted and without going into details why, is:

We see that even the simplest template, static, without any handlebars syntax (e.g. "hello") takes 30kB when compiled where 26kB is HandlebarsConfigurationAdapter. Multiplied by 120+ is >3MB per one email type. Even though 3MB doesn't sound terrible, when we have many different emails like that, we multiple further and it's becoming a problem.

Can you see any way to be more memory optimized? We're also thinking about not precompiling those short sentences but then are worried about a lot of repeated on the fly compilation, resolving and throw away and garbage collector to be fairly busy.

Btw. our initial solution looked like:

We though we'd be clever but looks like what we're trying now can be even worse.

oformaniuk commented 2 years ago

Discussion in progress here: https://github.com/Handlebars-Net/Handlebars.Net/discussions/493