Skip to content

Commit 443d96a

Browse files
committed
Introduce non_term_type_to_quoted to avoid additional term_type? checks
1 parent 56e7de3 commit 443d96a

File tree

1 file changed

+45
-38
lines changed

1 file changed

+45
-38
lines changed

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

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -528,50 +528,57 @@ defmodule Module.Types.Descr do
528528
if term_type?(descr) do
529529
{:term, [], []}
530530
else
531-
{dynamic, static, extra} =
532-
case :maps.take(:dynamic, descr) do
533-
:error ->
534-
{%{}, descr, []}
531+
non_term_type_to_quoted(descr, opts)
532+
end
533+
end
535534

536-
{:term, static} ->
537-
{:term, static, []}
535+
defp non_term_type_to_quoted(descr, opts) do
536+
{dynamic, static, extra} =
537+
case :maps.take(:dynamic, descr) do
538+
:error ->
539+
{%{}, descr, []}
538540

539-
{dynamic, static} ->
540-
if term_type?(dynamic) do
541-
{:term, static, []}
542-
else
543-
# Denormalize functions before we do the difference
544-
{static, dynamic, extra} = fun_denormalize(static, dynamic, opts)
545-
{difference(dynamic, static), static, extra}
546-
end
547-
end
541+
{:term, static} ->
542+
{:term, static, []}
548543

549-
# Merge empty list and list together if they both exist
550-
{extra, static} =
551-
case static do
552-
%{list: list, bitmap: bitmap} when (bitmap &&& @bit_empty_list) != 0 ->
553-
static =
554-
static
555-
|> Map.delete(:list)
556-
|> Map.replace!(:bitmap, bitmap - @bit_empty_list)
557-
558-
{list_to_quoted(list, true, opts) ++ extra, static}
544+
{dynamic, static} ->
545+
# Computing term_type?(difference(dynamic, static)) can be
546+
# expensive, so we check for term type before hand and check
547+
# for :term exclusively in dynamic_to_quoted/2.
548+
if term_type?(dynamic) do
549+
{:term, static, []}
550+
else
551+
# Denormalize functions before we do the difference
552+
{static, dynamic, extra} = fun_denormalize(static, dynamic, opts)
553+
{difference(dynamic, static), static, extra}
554+
end
555+
end
559556

560-
%{} ->
561-
{extra, static}
562-
end
557+
# Merge empty list and list together if they both exist
558+
{extra, static} =
559+
case static do
560+
%{list: list, bitmap: bitmap} when (bitmap &&& @bit_empty_list) != 0 ->
561+
static =
562+
static
563+
|> Map.delete(:list)
564+
|> Map.replace!(:bitmap, bitmap - @bit_empty_list)
563565

564-
# Dynamic always come first for visibility
565-
unions =
566-
to_quoted(:dynamic, dynamic, opts) ++
567-
Enum.sort(
568-
extra ++ Enum.flat_map(static, fn {key, value} -> to_quoted(key, value, opts) end)
569-
)
566+
{list_to_quoted(list, true, opts) ++ extra, static}
570567

571-
case unions do
572-
[] -> {:none, [], []}
573-
unions -> Enum.reduce(unions, &{:or, [], [&2, &1]})
568+
%{} ->
569+
{extra, static}
574570
end
571+
572+
# Dynamic always come first for visibility
573+
unions =
574+
to_quoted(:dynamic, dynamic, opts) ++
575+
Enum.sort(
576+
extra ++ Enum.flat_map(static, fn {key, value} -> to_quoted(key, value, opts) end)
577+
)
578+
579+
case unions do
580+
[] -> {:none, [], []}
581+
unions -> Enum.reduce(unions, &{:or, [], [&2, &1]})
575582
end
576583
end
577584

@@ -2109,7 +2116,7 @@ defmodule Module.Types.Descr do
21092116
[single]
21102117

21112118
true ->
2112-
case to_quoted(descr, opts) do
2119+
case non_term_type_to_quoted(descr, opts) do
21132120
{:none, _meta, []} = none -> [none]
21142121
descr -> [{:dynamic, [], [descr]}]
21152122
end

0 commit comments

Comments
 (0)