creusefond
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.
Trending in Discussions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Most Liked- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Qqwy
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_stringto compile the EEx template once and then use it during the benchmark run.Running the benchmark on my computer shows the following results:
josevalim
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
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.