Skip to content

Commit

Permalink
Minor performance fixes (#4)
Browse files Browse the repository at this point in the history
* Try to speed up map creation performance

* Try to speed up some atom/map operations
  • Loading branch information
drtheuns-enreach committed Oct 9, 2023
1 parent de905e4 commit 8a3d6d2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/bubble_lib/dsl_struct.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule BubbleLib.DslStruct do
struct
|> Map.keys()
|> Enum.reject(&(&1 != "__struct__" && (is_list(only) && &1 in only)))
|> Enum.map(fn k -> {to_string(k), Map.get(struct, k)} end)
|> Enum.map(fn k -> {Atom.to_string(k), Map.get(struct, k)} end)
|> Map.new()
|> Jason.Encode.map(opts)
end
Expand Down Expand Up @@ -77,7 +77,7 @@ defmodule BubbleLib.DslStruct do
orig = apply(mod, :__struct__, [])

Map.keys(orig)
|> Enum.map(fn k -> {k, Map.get(struct, to_string(k)) || Map.get(orig, k)} end)
|> Enum.map(fn k -> {k, Map.get(struct, Atom.to_string(k)) || Map.get(orig, k)} end)
|> Map.new()
|> Map.put(:__struct__, mod)
end
Expand Down
31 changes: 16 additions & 15 deletions lib/bubble_lib/map_util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@ defmodule BubbleLib.MapUtil do
|> drop_nils()
end

def deatomize(%{__struct__: _} = struct) do
def deatomize(%mod{} = struct) do
struct
|> Map.keys()
|> Enum.reduce(struct, fn k, s -> Map.put(s, k, deatomize(Map.get(struct, k))) end)
|> Map.from_struct()
|> Enum.reduce(struct, fn
{k, v}, acc when is_map(v) or is_list(v) -> Map.put(acc, k, deatomize(v))
{k, v}, acc -> Map.put(acc, k, v)
end)
|> Map.put(:__struct__, mod)
end

def deatomize(%{} = map) do
map
|> Enum.map(fn
{%{__struct__: _} = k, v} -> {k, deatomize(v)}
{k, v} -> {to_string(k), deatomize(v)}
Map.new(map, fn
{%_{} = k, v} -> {k, deatomize(v)}
{k, v} when is_atom(k) and (is_map(v) or is_list(v)) -> {Atom.to_string(k), deatomize(v)}
{k, v} when is_map(v) or is_list(v) -> {k, deatomize(v)}
{k, v} when is_atom(k) -> {Atom.to_string(k), v}
{k, v} -> {k, v}
end)
|> Map.new()
end

def deatomize(list) when is_list(list) do
Expand Down Expand Up @@ -64,13 +69,11 @@ defmodule BubbleLib.MapUtil do
end

def deep_keyword_to_map(value) when is_map(value) do
Enum.map(value, fn {k, v} -> {k, deep_keyword_to_map(v)} end)
|> Enum.into(%{})
Map.new(value, fn {k, v} -> {k, deep_keyword_to_map(v)} end)
end

def deep_keyword_to_map([{_, _} | _] = value) do
Enum.map(value, fn {k, v} -> {k, deep_keyword_to_map(v)} end)
|> Enum.into(%{})
Map.new(value, fn {k, v} -> {k, deep_keyword_to_map(v)} end)
end

def deep_keyword_to_map(value) when is_list(value) do
Expand All @@ -88,9 +91,7 @@ defmodule BubbleLib.MapUtil do
end

def enum_materialize(%{} = map) do
map
|> Enum.map(fn {k, v} -> {k, enum_materialize(v)} end)
|> Map.new()
Map.new(map, fn {k, v} -> {k, enum_materialize(v)} end)
end

def enum_materialize(list) when is_list(list) do
Expand Down
2 changes: 1 addition & 1 deletion lib/bubble_lib/map_util/auto_map.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ defmodule BubbleLib.MapUtil.AutoMap do
end

def get_in(%{} = map, [head | tail]) when is_atom(head) do
get_in(map, [to_string(head) | tail])
get_in(map, [Atom.to_string(head) | tail])
end

def get_in(%ETS{} = struct, path) do
Expand Down

0 comments on commit 8a3d6d2

Please sign in to comment.