Skip to content

Commit 6da90be

Browse files
committed
Optimize more traversals
1 parent 954a8c6 commit 6da90be

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

lib/elixir/lib/module/types/expr.ex

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,35 @@ defmodule Module.Types.Expr do
7878
{prefix, suffix} = unpack_list(list, [])
7979
{prefix, context} = Enum.map_reduce(prefix, context, &of_expr(&1, stack, &2))
8080
{suffix, context} = of_expr(suffix, stack, context)
81-
{non_empty_list(Enum.reduce(prefix, &union/2), suffix), context}
81+
82+
if stack.mode == :traversal do
83+
{dynamic(), context}
84+
else
85+
{non_empty_list(Enum.reduce(prefix, &union/2), suffix), context}
86+
end
8287
end
8388

8489
# {left, right}
8590
def of_expr({left, right}, stack, context) do
8691
{left, context} = of_expr(left, stack, context)
8792
{right, context} = of_expr(right, stack, context)
88-
{tuple([left, right]), context}
93+
94+
if stack.mode == :traversal do
95+
{dynamic(), context}
96+
else
97+
{tuple([left, right]), context}
98+
end
99+
end
100+
101+
# {...}
102+
def of_expr({:{}, _meta, exprs}, stack, context) do
103+
{types, context} = Enum.map_reduce(exprs, context, &of_expr(&1, stack, &2))
104+
105+
if stack.mode == :traversal do
106+
{dynamic(), context}
107+
else
108+
{tuple(types), context}
109+
end
89110
end
90111

91112
# <<...>>>
@@ -103,12 +124,6 @@ defmodule Module.Types.Expr do
103124
{@stacktrace, context}
104125
end
105126

106-
# {...}
107-
def of_expr({:{}, _meta, exprs}, stack, context) do
108-
{types, context} = Enum.map_reduce(exprs, context, &of_expr(&1, stack, &2))
109-
{tuple(types), context}
110-
end
111-
112127
# left = right
113128
def of_expr({:=, _, [left_expr, right_expr]} = expr, stack, context) do
114129
{left_expr, right_expr} = repack_match(left_expr, right_expr)

lib/elixir/lib/module/types/of.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ defmodule Module.Types.Of do
115115
@doc """
116116
Builds permutation of maps according to the given keys.
117117
"""
118+
def permutate_map(pairs, %{mode: :traversal} = stack, context, of_fun, _of_map) do
119+
context =
120+
Enum.reduce(pairs, context, fn {key, value}, context ->
121+
{_, context} = of_fun.(key, stack, context)
122+
{_, context} = of_fun.(value, stack, context)
123+
context
124+
end)
125+
126+
{dynamic(), context}
127+
end
128+
118129
def permutate_map(pairs, stack, context, of_fun, of_map) do
119130
{dynamic?, fallback, single, multiple, assert, context} =
120131
Enum.reduce(pairs, {false, none(), [], [], [], context}, fn

0 commit comments

Comments
 (0)