Skip to content

Commit f94f4d5

Browse files
authored
Mechanical refactor of Descr, avoid intermediate lists (#14230)
1 parent d9ffe76 commit f94f4d5

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

lib/elixir/lib/module/types/descr.ex

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ defmodule Module.Types.Descr do
11031103
[{name, [], arguments} | acc]
11041104
else
11051105
negs
1106-
|> Enum.map(fn {ty, lst} ->
1106+
|> non_empty_map_or(fn {ty, lst} ->
11071107
args =
11081108
if subtype?(lst, @empty_list) do
11091109
[to_quoted(ty, opts)]
@@ -1113,7 +1113,6 @@ defmodule Module.Types.Descr do
11131113

11141114
{name, [], args}
11151115
end)
1116-
|> Enum.reduce(&{:or, [], [&2, &1]})
11171116
|> Kernel.then(
11181117
&[
11191118
{:and, [], [{name, [], arguments}, {:not, [], [&1]}]}
@@ -1853,10 +1852,10 @@ defmodule Module.Types.Descr do
18531852
end
18541853

18551854
# Use heuristics to normalize a map dnf for pretty printing.
1856-
defp map_normalize(dnf) do
1857-
dnf
1858-
|> Enum.reject(&map_empty?([&1]))
1859-
|> Enum.map(fn {tag, fields, negs} ->
1855+
defp map_normalize(dnfs) do
1856+
for dnf <- dnfs, not map_empty?([dnf]) do
1857+
{tag, fields, negs} = dnf
1858+
18601859
{fields, negs} =
18611860
Enum.reduce(negs, {fields, []}, fn neg = {neg_tag, neg_fields}, {acc_fields, acc_negs} ->
18621861
if map_empty_negation?(tag, acc_fields, neg) do
@@ -1874,7 +1873,7 @@ defmodule Module.Types.Descr do
18741873
end)
18751874

18761875
{tag, fields, negs}
1877-
end)
1876+
end
18781877
|> map_fusion()
18791878
end
18801879

@@ -1955,8 +1954,7 @@ defmodule Module.Types.Descr do
19551954

19561955
_ ->
19571956
negative_maps
1958-
|> Enum.map(&map_literal_to_quoted(&1, opts))
1959-
|> Enum.reduce(&{:or, [], [&2, &1]})
1957+
|> non_empty_map_or(&map_literal_to_quoted(&1, opts))
19601958
|> Kernel.then(
19611959
&{:and, [], [map_literal_to_quoted({tag, positive_map}, opts), {:not, [], [&1]}]}
19621960
)
@@ -2309,8 +2307,7 @@ defmodule Module.Types.Descr do
23092307

23102308
_ ->
23112309
negative_tuples
2312-
|> Enum.map(&tuple_literal_to_quoted(&1, opts))
2313-
|> Enum.reduce(&{:or, [], [&2, &1]})
2310+
|> non_empty_map_or(&tuple_literal_to_quoted(&1, opts))
23142311
|> Kernel.then(
23152312
&{:and, [], [tuple_literal_to_quoted({tag, positive_tuple}, opts), {:not, [], [&1]}]}
23162313
)
@@ -2575,8 +2572,9 @@ defmodule Module.Types.Descr do
25752572
none()
25762573
else
25772574
n..(m - 1)//1
2578-
|> Enum.map(&tuple_values(:closed, tuple_fill(elements, &1), negs))
2579-
|> Enum.reduce(none(), &union/2)
2575+
|> Enum.reduce(none(), fn i, acc ->
2576+
tuple_values(:closed, tuple_fill(elements, i), negs) |> union(acc)
2577+
end)
25802578
|> union(
25812579
if neg_tag == :open do
25822580
none()
@@ -2971,4 +2969,8 @@ defmodule Module.Types.Descr do
29712969
end
29722970

29732971
defp iterator_non_disjoint_intersection?(:none, _map), do: false
2972+
2973+
defp non_empty_map_or([head | tail], fun) do
2974+
Enum.reduce(tail, fun.(head), &{:or, [], [&2, fun.(&1)]})
2975+
end
29742976
end

0 commit comments

Comments
 (0)