Any tips or learning resources for writing highly performant Elixir code?

They are - just use NIFs :wink:

There are lots of possible improvements, for example:

  1. Depending on specific case i.e. you have 2 or more algorithms, benchmark them using benchee and use desired solution
  2. NIF and related language features not only can speed up code, but also allows to use many non-Elixir libraries.
  3. Stream, Flow and other modules instead of Enum - instead of storing whole enumerable in memory we can read and process them separately.. Similarly we can process multiple list elements at the same time. There are lots of solutions depending on what you want to achieve
  4. Metaprogramming i.e. improve runtime code by for example generating a pattern-matching
  5. Try to limit piped Enum calls, so iterate one list as few times as possible, often [head | tail]-based recursive function or Enum.reduce/3 is useful in such cases.

Please remember that there is no #1 optimisation. Sometimes you optimise resources usage and sometimes you use all available resources to speed up the work. Also it’s worth to mention that raw speed it not always welcome if you have to write very strict code. Often writing more generic code without every possible optimisation may save you huge amount of time in case you would be rewriting or enhancing it by supporting more structs and so on … Again, there is no a single good solution here as it’s very case-specific topic.

6 Likes