creusefond

creusefond

Benchmark : String templating at runtime

Hi all,

I am facing the issue of having large template strings containing an unknown number of variables, and those templates need to be bound thousands of times.
Due to performance constraints, I would like for this procedure to be optimized. I had multiple ways of doing it.

Threads such as this one suggest to use EEx. I saw that I could use EEx.compile_string to get a quoted expression, that I can resolve with Code.eval_quoted (on the benchmark : eex). I also benchmarked a direct call to EEx.eval_string (eex_eval_string).

The previous implementation of this was to simply parse the content of the string for each variable, using a Regex. I added that method to the benchmark (base).

I also tried a manual implementation. The template is parsed once, and broken down into parts. During the binding, I simply append each variable value and each part (manual).

You can see the implementation here, the benchmark here and the results here.

Conclusions :

  • Manual implementation outperforms the rest, by a wide margin.
  • EEx seriously underperforms for this task (40x-170x slower than manual). Maybe I’m not using it right.
  • Creating this benchmark was incredibly simple. Kudos to Benchee.

Please do not hesitate if you have feedback, or if you’d like to add another method to the test.

Most Liked

Qqwy

Qqwy

TypeCheck Core Team

The way you were using EEx indeed does not result in very good performance: It re-compiles the EEx template on every benchmarking iteration.

I’ve sent you a PR which contains an implementation using EEx’s function_from_string to compile the EEx template once and then use it during the benchmark run.

Running the benchmark on my computer shows the following results:

$ mix run benchmarks/string_templating.ex 
Compiling 1 file (.ex)
"ALL THE FOLLOWING SHOULD BE TRUE"
true
true
true
true
Operating System: Linux
CPU Information: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
Number of Available Cores: 8
Available memory: 7.60 GB
Elixir 1.10.2
Erlang 22.3.4.1

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 35 s

Benchmarking base...
Benchmarking eex...
Benchmarking eex_compiled...
Benchmarking eex_eval_string...
Benchmarking manual...

Name                      ips        average  deviation         median         99th %
eex_compiled           758.61        1.32 ms    ±20.23%        1.27 ms        2.41 ms
manual                 125.65        7.96 ms    ±14.40%        7.89 ms       11.80 ms
base                    17.07       58.57 ms     ±5.70%       58.51 ms       65.98 ms
eex                      2.22      450.36 ms     ±5.85%      442.40 ms      500.30 ms
eex_eval_string          1.42      704.79 ms     ±9.19%      714.84 ms      787.16 ms

Comparison: 
eex_compiled           758.61
manual                 125.65 - 6.04x slower +6.64 ms
base                    17.07 - 44.43x slower +57.25 ms
eex                      2.22 - 341.65x slower +449.04 ms
eex_eval_string          1.42 - 534.66x slower +703.47 ms
warning: redefining module EexExampleModule (current version defined in memory)
  lib/templating_benchmarks.ex:27

Operating System: Linux
CPU Information: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
Number of Available Cores: 8
Available memory: 7.60 GB
Elixir 1.10.2
Erlang 22.3.4.1

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 35 s

Benchmarking base...
Benchmarking eex...
Benchmarking eex_compiled...
Benchmarking eex_eval_string...
Benchmarking manual...

Name                      ips        average  deviation         median         99th %
eex_compiled             5.23        0.191 s    ±41.72%        0.175 s         0.47 s
manual                   2.43         0.41 s     ±7.35%         0.41 s         0.47 s
base                    0.199         5.02 s     ±0.00%         5.02 s         5.02 s
eex                    0.0213        47.00 s     ±0.00%        47.00 s        47.00 s
eex_eval_string        0.0142        70.61 s     ±0.00%        70.61 s        70.61 s

Comparison: 
eex_compiled             5.23
manual                   2.43 - 2.15x slower +0.22 s
base                    0.199 - 26.24x slower +4.83 s
eex                    0.0213 - 245.76x slower +46.81 s
eex_eval_string        0.0142 - 369.18x slower +70.42 s
josevalim

josevalim

Creator of Elixir

It is worth adding that there are improvements in Erlang/OTP 24 and Elixir master in regards to code evaluation with many vars which should improve it about 5-6x at least.

axelson

axelson

Scenic Core Team

Not a direct answer, but have you looked at using iolists for this? Here’s a nice article that touches on the performance benefits of iolists:
https://www.bignerdranch.com/blog/elixir-and-io-lists-part-1-building-output-efficiently/

The hard part would probably be parsing the “template” into something you can use to build an iolist. Also if you gave a few examples of the types of input you expect that would probably help people give you suggestions.

Where Next?

Popular in Discussions Top

CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New
ben-pr-p
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
acrolink
How does the two languages compare when it comes to server side application development? Any experiences or ideas? Thank you.
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement