Handlebars-Net / Handlebars.Net

A real .NET Handlebars engine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Many small templates - high memory utilization

jimfum opened this issue · comments

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:

  • 1 big email body compiled
  • 20 email subjects compiled (same template body can be used with multiple subjects)
  • 100+ various small text pieces that are compiled (typically short sentences possibly also containing handlebars syntax that are included into the email by our code - they're language specific). Overall, the character sum of these texts is around the size of the email body.

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:

  • 20 big email bodies compiled
  • 20 email subjects compiled (same template body can be used with multiple subjects)

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

Discussion in progress here: #493