Why is the "params" in the Phoenix framework a map and not a hash? Can anyone also explain the inner implementation level details.
-
Params are maps because that will be the preferred way of handling data structures of that sort going forward. Hashes in Elixir are still there because maps aren't performant for large maps (yet). – Onorio Catenacci Oct 26 '15 at 11:48
2 Answers
From the getting started guide:
Note: Maps were recently introduced into the Erlang VM with EEP 43. Erlang 17 provides a partial implementation of the EEP, where only “small maps” are supported. This means maps have good performance characteristics only when storing at maximum a couple of dozens keys. To fill in this gap, Elixir also provides the HashDict module which uses a hashing algorithm to provide a dictionary that supports hundreds of thousands keys with good performance.
One of the key benefits of maps is partial pattern matching:
def edit(conn, %{"id" => id} = params)
...
The above will match for any map which contains the string id
as a key.
In OTP 18, maps have improved performance as you can see at https://gist.github.com/BinaryMuse/bb9f2cbf692e6cfa4841. And it is likely that HashDict
will be deprecated in the future.
There is some great information about Elixir data types in this answer: What is the benefit of Keyword Lists?
Elixir introduced hashes/dicts as part of the core language, the Erlang VM does not have support for hashes. Underneath hashes are implemented on top of maps and keywords lists (list of paired tuples).
HashDict is implemented on top structs and structs are implemented on top maps.
it is confusing, future versions of elixir hashes/dicts are going to be deprecated there only going 2 data structures maps and mapsets.

- 1,376
- 11
- 7