GuestNo new alerts

Template Interpreter vs Template Transpiler

- Announcements1050
Anyone who's been poking around deeply has probably seen me use these phrases, possibly one, both, or neither, that works too. Gosora originally used Go's `html/template` template interpreter, which worked for a time, however as it was in fact an interpreter, it really really quite... slow.

Really slow. I recently did a benchmark to compare it to our faster template transpiler and it was about sixty times slower. Ouch. But, what is an interpreter really? It's essentially a giant loop recursively iterating over a large structure called an abstract syntax tree which contains nodes for every part of the code.

Text chunks. If statements. Even loops. Each of these nodes are iterated over, and these nodes in turn can contain child nodes. Almost like the DOM in a way. This is a bit of a simplication, but it should give you the general gist of how things roll.

And unfortunately, this is where the crux of the matter lies, no matter how well an interpreter is or isn't optimised, it still has to go through the overhead of a loop, many little indirects, and runtime reflection. In other words. This. is. slow.

However, C and Go are fast, blindly fast, so you usually don't feel too much pain from an interpreter, however. We don't want to settle for second-best here and it actually took up an incredible amount of time in comparison to the other parts of a route, even a query could be easily dwarfed by it.

So, what to do? Well, what if instead of essentially emulating the code with an interpreter, we instead... Ran the code natively. And so, we did. I wrote a custom template transpiler which takes a Go Template, transpiles it to pure Go, and runs it like any other piece of Go.

Instant speed boost, it wasn't even funny how fast it was compared to our previous solution, and thus we hopped to that. I quickly switched some of the most important and most frequently routes over to it. Topic list. Forum list. Topic page. Profiles. Topic creation page.

And over many months, I refined the transpiler, added new features, optimised it (e.g. with the fragment system), and recently started doing large refactors to help clean it up to make it more maintainable and to open the door to several features we have pulled yet.

In addition to that, I recently opened the door to various commonly hit interpreted templates like the login page, the registration page, the IP Search page (well, I like that page lol), and the error page. The plan is to expand things further, but many of the routes which aren't covered are generally one of a kind rarities or actions with no templates.

Thus, things stagnated once more, but plans are in the works to cover huge swathes of the system like the account manager in one fell swoop, before moving forward a step at a time onto the biggest fortress of interpretation, the Control Panel.

I'm happy with how things are going, and lets get things rolling further 😀