Fl4m3Ph03n1x

Fl4m3Ph03n1x

Reached Elixir's compiler implementation limit

Background

In my fictitious car company I have a module that tells me if I can sell a car with some specifications in a given country. This module Cars.Jurisdiction reads a CSV file at compile time and it exposes a function that given a triple, compares that with the compiled CSV and returns true or false depending if I can sell or not the car in that country.

defmodule Cars.Jurisdiction do

#########################
# Compile time madness! #
#########################

  csv_data =
    :cars
    |> :code.priv_dir()
    |> Path.join("cars_to_sell.csv")
    |> File.stream!
    |> CSV.decode!(headers: false, separator: ?;)
    |> Stream.map(&List.to_tuple/1)
    |> Stream.uniq
    |> Enum.map(fn {car, country, engine_type} ->
      {String.trim(car), String.trim(country), String.trim(engine_type)}
    end)

  cars_with_electric_engine =
    csv_data
    |>  # Filter and mapping operations

  car_country_combo_with_diesel_engine =
    csv_data
    |>  # Filter and mapping operations

  allowed_cars =
    csv_data
    |>  # Filter and mapping operations

  allowed_countries =
    Enum.map(csv_data, fn {_car, country, _engine} -> country end)

  allowed_engines =
    Enum.map(csv_data, fn {_car, _country, engine} -> engine end)

  @allowed_cars_countries_engines csv_data
  @cars_with_electric_engine cars_with_electric_engine
  @car_country_combo_with_diesel_engine car_country_combo_with_diesel_engine
  @allowed_cars allowed_cars
  @allowed_countries allowed_countries
  @allowed_engines allowed_engines

################################
# End of compile time madness! #
################################

  @spec is_blocked?(String.t, String.t, String.t, [{String.t, String.t, String.t}]) :: boolean
  def is_blocked?(car, country, engine, csv_input \\ @allowed_cars_countries_engines) do
  # never trust a Human made CSV! Humans are carbon based! How flimsy!  
  csv_data =
      Enum.map(csv_input, &trim_csv/1) 

    cond do
      car not in @allowed_cars -> true
      car in @cars_with_electric_engine -> false
      {car, country} in @car_country_combo_with_diesel_engine -> false
      country not in @allowed_countries -> true
      engine not in @allowed_engines -> true
      true -> {car, country, engine} not in csv_data
    end
  end

  defp trim_csv({car, country, engine}), do:
    {String.trim(sport), String.trim(country), String.trim(league)}

end

Problem

Now, because I have a lot of stuff happening at compile time, I expected the compile time to go up a little, specially since this is running in Elixir 1.5, which is missing a ton of improvements to compile time that newer versions have.

However,I didn’t expect this to take over 20 minutes to compile. I also didn’t expect to see compilation failing with the following error:

== Compilation error in file lib/cars/jurisdiction.ex ==
** (CompileError) Elixir.Cars.Jurisdiction: function 'is_blocked?'/2+1029:
  An implementation limit was reached.
  Try reducing the complexity of this function.
  Instruction: {bif,'=:=',{f,0},[{x,2},{literal,<<"handball">>}],{x,1023}}
    (stdlib) lists.erl:1338: :lists.foreach/2
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
make[1]: *** [compile] Error 1

I am not sure I understand what is going on here. It is complaining about my cond statement.
I am aware that this code would be cleaner with functions using pattern matching (in fact if I do so, I avoid this error, but the code still takes half an hour to compile) but this cond statement only has 6 cases.

Questions

  1. Why am I getting this error?
  2. Why don’t I get the error if I use functions with pattern matching?
  3. Why does it take over half an hour to compile?

And most importantly:

  1. How can I git rid of the error and the long compilation time ?

Marked As Solved

ericmj

ericmj

Elixir Core Team

You are getting the error because the function is too complex which means it has too many expressions.

You can try avoiding the in expansion by using Enum.member?/2 instead or by moving the literals to other functions:

def is_blocked?(...) do
  cond do
    car not in allowed_cars() -> true
    ...
  end
end

defp allowed_cars(), do: @allowed_cars

Also Liked

josevalim

josevalim

Creator of Elixir

You probably want to use a MapSet, which provides better performance for element lookups compared to lists, which are linear.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

We ran into this with Absinthe a bit. If the body of a module expanded into overly complex code the compile times eventually became non linear. We never did figure out why, and instead refocused our efforts on just minimizing the amount of code we dumped in the module body. I’m not 100% sure that your scenario is the same though because your expansion is happening principally inside function bodies.

eksperimental

eksperimental

just for the record Elixir v1.11 solves this limiation

ericmj

ericmj

Elixir Core Team

In this case I don’t think you should focus on which is more efficient since one of them doesn’t work. In the general case in should be more efficient when given a literal on the right hand side.

Enum.member? is a normal function call, while in is a macro that will do more work at compile time and can therefor be slower, but it’s unclear if it’s in by itself that is slow or the code it produces that is slow to compile. It can also be something else that causes the slowdown, we don’t have enough information to reproduce your issue. My suggestion was more about fixing the compilation error and less about fixing the slowness issue.

You can find ins documentation here: Kernel — Elixir v1.11.4.

Where Next?

Popular in Questions Top

itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
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
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
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
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
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement