minhajuddin

minhajuddin

Defmap - embed maps into a module for faster/easier lookups. Feedback please

Defmap is a utility which allows you to embed a map into a module for faster/easier lookups.

First 9 of 9 Posts Switch mode

OvermindDL1

OvermindDL1

Ah that’s cool. :slight_smile:

Eiji

Eiji

Can you explain me the reason of use macros in your project?
For me (I don’t see a special use case) it’s unnecessary. Instead of this I recommend to use ETS (Erlang Term Storage) or Agent.
For example look at example KV.Bucket implementation.
In my case library do not need to reserve get method name and code is much more cleaner (at least for me).
Your library also does not allow to override Map.
Can you agree with me or I missed something? What do you think about my comment?

Edit
One more example:

defmodule Example do                                                   
  @map %{
    400 => "Bad Request",
    401 => "Unauthorized",
    403 => "Forbidden"
  }

  def get(code), do: @map[code] || :error                                
end
OvermindDL1

OvermindDL1

To me it looks like a way to define an immutable enumeration.

Eiji

Eiji

Ok, how about my example in edit?

bbense

bbense

Well, there can be some performance advantages in functional heads vs just looking things up in a map. This is more or less the way unicode support is implemented in Elixir core.

I think I’d like this more if it did not use the Map get as the function name and I’d really like it a lot more if it wasn’t called Defmap. The function is really just get_constant, or even just constant or attribute. It’s a generic tool for exposing module attributes as functions. Map is just a convenient way to structure the input, you aren’t defining a Map in any practical sense at all.

The only thing wrong with this is actually the hardest part of programming, naming stuff. I think what it does is useful.

minhajuddin

minhajuddin OP

Thanks for all the feedback guys :slight_smile:

I have benchmarked the simple map implementation from @Eiji

Elixir 1.3.3
Benchmark suite executing with the following configuration:
warmup: 2.0s
time: 5.0s
parallel: 1
Estimated total run time: 14.0s

Benchmarking defmap...
Benchmarking plainmap...

Name               ips        average  deviation         median
defmap          4.87 K      205.17 μs     ±6.77%      201.00 μs
plainmap        4.46 K      224.04 μs    ±11.79%      215.00 μs

Comparison: 
defmap          4.87 K
plainmap        4.46 K - 1.09x slower

The difference is performance is not huge. However, there is a huge difference in memory usage. I checked the memory usage of the defmap version using :observer.start (Please recommend a good memory profiling tool, I didn’t know any :slight_smile: ) and defmap used ~43mb of memory whereas plainmap used ~200mb, This was my initial assumption as for every lookup with the map implementation we are instantiating a full map and then selecting one value from it. You can checkout the test implementation @ GitHub - minhajuddin/defmap_test · GitHub

Eiji

Eiji

Ok, now I see your point.
Thanks for performance testing, they give me a lot to think about.

I have a feature request:
more configuration - allow to send opts like:

defmodule MyProject do
  YourLibrary.your_macro(my_first_map)
  YourLibrary.your_macro(my_second_map, [caller: :get_value])
  YourLibrary.your_macro(my_third_map, [private: true])
end

to allow define multiple embed maps (also private maps) into one module with different callers.
Your default configuration config.exs:

config :defmap, :default_opts,
  caller: :get,
  private: false
minhajuddin

minhajuddin OP

Thanks for the feedback :slight_smile: and ideas. I’ll add them soon

sasajuric

sasajuric

Author of Elixir In Action

I’m not sure you need a lib and a macro for this. You can get the same thing with something like this (untested):

defmodule Foo do
  for {code, message} <- {400 => "Bad Request", 401 => "Unauthorized", 403 => "Forbidden"} do
    def get(unquote(code)), do: {:ok, unquote(message)}
  end
  def get(_), do: :error
end

I occasionally use this technique.

— All posts loaded —

Where Next?

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 14960 120
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
quatermain
Hello, I’m sharing my plugin here in forum after some time so it has time to mature and proof yourself. I use Claude Code daily on a pr...
New

Other Trending Topics Top

type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
bjorng
We want to introduce a new native datatype to Erlang: native records. Although replacing all tuple records with native records is not our...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New

We're in Beta

About us Mission Statement