Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Let's Talk Process Dictionary

Greg Vaughn
February 23, 2018

Let's Talk Process Dictionary

A tale of heated opinions, information whispered in the shadows, and mutants living in your BEAM at this very moment.
From Lonestar ElixirConf 2018
Video available: https://www.youtube.com/watch?v=zDIoFWwfBO0
Code available: https://github.com/gvaughn/process_dictionary_talk

Greg Vaughn

February 23, 2018
Tweet

More Decks by Greg Vaughn

Other Decks in Programming

Transcript

  1. WHAT IS A PROCESS DICTIONARY? A per-process key/value map Straightforward

    API Process.get Process.put Process.delete Process.get_keys Process.info
  2. iex(1)> f = fn -> ...(1)> IO.inspect(Process.get_keys(), label: "get_keys()") ...(1)>

    IO.inspect(Process.put(:lonestar, :elixirconf), label: "put(:lonestar, :elixirconf") ...(1)> IO.inspect(Process.get_keys(), label: "get_keys()") ...(1)> IO.inspect(Process.get(:lonestar), label: "get(:lonestar)") ...(1)> IO.inspect(Process.put(:lonestar, :rocks), label: "put(:lonestar, :rocks") ...(1)> end #Function<20.99386804/0 in :erl_eval.expr/5> iex(2)> spawn(f) get_keys(): [] put(:lonestar, :elixirconf): nil get_keys(): [:lonestar] get(:lonestar): :elixirconf put(:lonestar, :rocks): :elixirconf
  3. • Process Dictionary erases all the functional benefits we get

    from Elixir! • It should be removed from the language! • You should never use it! OH NO!
  4. • Process Dictionary helps Elixir be multi-paradigm! • There's plenty

    of examples of its use by responsible programmers! • Use it whenever it is convenient!
  5. Elixir is not pure Side effects are essential for useful

    programs – a receive loop is useless without them Multi-paradigm It means the language refuses to promote one approach over another and leads to unmaintainable messes Isolating where side effects happen and keeping the rest of your program functional/immutable is The Elixir Way™
  6. It should be removed from the language! There's plenty of

    examples of its use by responsible programmers!
  7. • Erlang's rand and crypto modules store the seed •

    proc_lib stores $initial_call and $ancestors • mnesia stores a transaction identifier • Logger stores logging metadata • Gettext stores locale • Ecto transactions store a connection • iex stores history
  8. Default to not using the Process Dictionary. Ensure you know

    how to solve your problem without it. In a GenServer, use the state you already have. Consider reserving part of that state to pass into some stateful helper module. Safest: a write once variable, avoiding mutation. Do: centralize where reads/writes happen near each other. Space and time. Caution: it is not garbage collected and will be copied via Process.info/2 so don't put large datasets in there.