Skip to content

Commit 6fb5372

Browse files
author
José Valim
committed
Optimize MapSet collectable
1 parent d18700c commit 6fb5372

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

lib/elixir/lib/map_set.ex

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ defmodule MapSet do
4141
@opaque t(value) :: %__MODULE__{map: %{optional(value) => []}}
4242
@type t :: t(term)
4343

44+
# TODO: Remove version key on Elixir 2.0
4445
defstruct map: %{}, version: 2
4546

4647
@doc """
@@ -169,11 +170,8 @@ defmodule MapSet do
169170

170171
defp filter_not_in([key | rest], map2, acc) do
171172
case map2 do
172-
%{^key => _} ->
173-
filter_not_in(rest, map2, acc)
174-
175-
_ ->
176-
filter_not_in(rest, map2, [{key, @dummy_value} | acc])
173+
%{^key => _} -> filter_not_in(rest, map2, acc)
174+
_ -> filter_not_in(rest, map2, [{key, @dummy_value} | acc])
177175
end
178176
end
179177

@@ -384,14 +382,14 @@ defmodule MapSet do
384382
end
385383

386384
defimpl Collectable do
387-
def into(original) do
385+
def into(map_set) do
388386
fun = fn
389-
map_set, {:cont, x} -> MapSet.put(map_set, x)
390-
map_set, :done -> map_set
387+
list, {:cont, x} -> [{x, []} | list]
388+
list, :done -> %{map_set | map: Map.merge(map_set.map, Map.new(list))}
391389
_, :halt -> :ok
392390
end
393391

394-
{original, fun}
392+
{[], fun}
395393
end
396394
end
397395

0 commit comments

Comments
 (0)