alichoopani

alichoopani

Need advice to improve an OOP code to Elixir (make more functional)

I came across such a piece of code and tried to implement it functionally. I found various ways to do it and benchmarked them using Benchee and I got some interesting results.
Codes and results can be seen in this gist.

First of all, I realized the huge negative impact of using ++ to add items to the list.
I expected using Enum.reduce/2 and Enum.map_reduce/3 to perform best, followed by tail_recursion_with_reverse/2 and then recursion/2. But the results were almost the opposite.
Now I have some questions that I will ask. Thanks in advance for all the feedback and help.

  1. In order to benchmark such questions, what other items should I consider in addition to the average execution time?
  2. That being said, is there any situation where ++ is the right choice for working with lists?
  3. Is there a better solution to this problem?
  4. Is it always better to write recursive functions in general than to use Enum functions?
  5. Does ‌Beam perform a special optimization in the recursion/2 function that is written in the code, which performs better than tail_recursion_with_reverse/2?
  def reduce(items, y) do
    {items, _} = Enum.reduce(items, {[], y},
      fn %{height: h} = item, {converted_items, y} -> {converted_items ++ [%{item | y: y}], y + h} end)
    items
  end

  def reduce_with_reverse(items, y) do
    {items, _} = Enum.reduce(items, {[], y},
      fn %{height: h} = item, {converted_items, y} -> {[%{item | y: y} | converted_items], y + h} end)
    Enum.reverse(items)
  end

  def map_reduce(items, y) do
    {items, _} = Enum.map_reduce(items, y, fn item, acc -> {Map.put(item, :y, acc), acc + item.height} end)
    items
  end

  def recursion([], _), do: []
  def recursion([%{height: h} = item | t], y), do: [%{item | y: y} | recursion(t, h + y)]

  def tail_recursion(items, y, converted_items \\ [])
  def tail_recursion([], _y, converted_items), do: converted_items
  def tail_recursion([h | t], y, converted_items), do:
    tail_recursion(t, y + h.height, converted_items ++ [%{y: y, height: h.height}])

  def tail_recursion_with_reverse(items, y, converted_items \\ [])
  def tail_recursion_with_reverse([], _y, converted_items), do: Enum.reverse(converted_items)
  def tail_recursion_with_reverse([h | t], y, converted_items), do:
    tail_recursion_with_reverse(t, y + h.height, [%{y: y, height: h.height} | converted_items])

Most Liked

al2o3cr

al2o3cr

I personally prefer Enum functions chained together, for instance:

  def enum_chain(items, y) do
    new_ys =
      [y | Enum.scan(items, y, fn item, acc -> item.height + acc end)]

    items
    |> Enum.zip(new_ys)
    |> Enum.map(fn {item, new_y} -> %{item | y: new_y} end)
  end

This produces a little more garbage to collect, due to intermediate results - but each step has a clear responsibility and is readable at a glance (assuming you remember Enum.scan :slight_smile: )

If items is large, you could avoid that garbage generation by using Stream:

  def stream_chain(items, y) do
    new_ys =
      Stream.concat([y], Stream.scan(items, y, fn item, acc -> item.height + acc end))

   items
   |> Stream.zip(new_ys)
   |> Enum.map(fn {item, new_y} -> %{item | y: new_y} end)
  end

In this case there are drop-in Stream replacements for Enum functions, so the code doesn’t materially change. This implementation trades off performance for efficiency: no intermediate values to GC but longer runtime because Stream uses lots of anonymous functions.

lud

lud

In order to benchmark such questions, what other items should I consider in addition to the average execution time?

You may look at Benchee, it’s great.

That being said, is there any situation where ++ is the right choice for working with lists?

When you are mapping / reducing over a list, it’s alway better not to use ++. But sometimes you get a list from elsewere and the only thing you have to do with it is to append. In that case the better choice is ++. This operator is not forbidden :slight_smile: A general rule of thumb is “do not use ++ in a loop, only once.”

Is there a better solution to this problem?

I find that your map_reduce implementation is the cleanest of all. There may be other solutions but I would just use the more readable. A tip though: you can put the clauses matching on [] (empty list) below the clause matching on items. There is no need to try this clause at each loop iteration.

Is it always better to write recursive functions in general than to use Enum functions?

I think that “better” depends on what you want. If you need the best performance, then custom recursive function should be faster. If you want clean code, I think Enum.map() is better as you will separate the logic from the unpacking/repacking of the list items. For reduce() it depends.

Does Beam perform a special optimization in the recursion/2 function that is written in the code, which performs better than tail_recursion_with_reverse/2?

There is this note in erlang docs:

A tail-recursive function that does not need to reverse the list at the end is faster than a body-recursive function, as are tail-recursive functions that do not construct any terms at all (for example, a function that sums all integers in a list).

source.

In your case, you have reverse, so it is not certain that it will be faster than the body-recursive function.

@mpope

[1, 2, 3] ++ [4]

Only a single element suffers from the left hand copy in that case.

I don’t think so. This snippets builds a new list like this : [1|[2|[3|[4]]]], it re-builds the full list of 4 items. This is why ++ is generally avoided, because it creates a copy of the left-hand list. If you need to concat 2 lists then it is fine, but it is not recommended to append a few items to a large list.

sabiwara

sabiwara

Elixir Core Team

You might not need to :slight_smile:

From the efficiency guide

Pattern matching in function head as well as in case and receive clauses are optimized by the compiler. With a few exceptions, there is nothing to gain by rearranging clauses.

There seems to be some gotchas and edge cases, but most of the time this should make no difference.

mpope

mpope

For point 2, using ++ is relatively cheap when adding a new element to the end of a list, such as:

[1, 2, 3] ++ [4]

Only a single element suffers from the left hand copy in that case.

As for recursion out performing tail_recursion_with_reverse, there is the overhead of the reverse to consider. That is one less n length operation that the function needs to perform. I suggest you checkout The Seven Myths of Erlang Performance. They explain the history behind the assumption that tail recursion is more performant.

As for question 3, I personally prefer pure recursive functions over the standard libraries when the task is simple. The recursion function is simple and elegant. But that is personal preference, and optimization should only be done after measurement in production, so any of the above solutions will be fine in most scenarios.

As for question 1, I’d suggest trying Streams, and maybe ParallelStream, and also trying a recursive solution with a preallocated tuple instead of a list accessed with element/2. Could get a speedup from the spacial cache locality.

Also as a note, function calls and pattern matching aren’t ‘free’. It looks like recursion uses the least amount of both function calls and branches. That could give things a boost as well.

Nice benchmark!

alichoopani

alichoopani

Thanks for the explanation and links. I need to read about Stream and ParallelStream.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement