From bfd46bd90352a498a60169c32c03c75fd40b1f7c Mon Sep 17 00:00:00 2001 From: zhanglinjie Date: Sat, 18 Jul 2026 18:07:03 +0800 Subject: [PATCH 1/2] Optimize Inspect.List.keyword?/1 by using Atom.to_string/1 Atom.to_charlist/1 allocates a new list per atom key when checking if a keyword list contains module names. Atom.to_string/1 produces a binary that can be pattern matched directly, avoiding O(n) temporary allocations when inspecting large maps and keyword lists. Benchmark (n=10): keyword?/1 (10K): 0.62ms -> 0.48ms (1.3x) keyword?/1 (50K): 2.72ms -> 1.69ms (1.6x) inspect map (10K): 2.59ms -> 1.52ms (1.7x) inspect map (50K): 8.85ms -> 5.94ms (1.5x) Assisted-by: opencode:deepseek-v4-flash --- lib/elixir/lib/inspect.ex | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/elixir/lib/inspect.ex b/lib/elixir/lib/inspect.ex index 927ddb50459..9bdec69b406 100644 --- a/lib/elixir/lib/inspect.ex +++ b/lib/elixir/lib/inspect.ex @@ -416,10 +416,7 @@ defimpl Inspect, for: List do @doc false def keyword?([{key, _value} | rest]) when is_atom(key) do - case Atom.to_charlist(key) do - [?E, ?l, ?i, ?x, ?i, ?r, ?.] ++ _ -> false - _ -> keyword?(rest) - end + if match?("Elixir." <> _, Atom.to_string(key)), do: false, else: keyword?(rest) end def keyword?([]), do: true From e52a22ed1d5540d100c9771f061dcd0a5f933bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 18 Jul 2026 12:18:14 +0200 Subject: [PATCH 2/2] Apply suggestion from @josevalim --- lib/elixir/lib/inspect.ex | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/elixir/lib/inspect.ex b/lib/elixir/lib/inspect.ex index 9bdec69b406..150817a5726 100644 --- a/lib/elixir/lib/inspect.ex +++ b/lib/elixir/lib/inspect.ex @@ -416,7 +416,10 @@ defimpl Inspect, for: List do @doc false def keyword?([{key, _value} | rest]) when is_atom(key) do - if match?("Elixir." <> _, Atom.to_string(key)), do: false, else: keyword?(rest) + case Atom.to_string(key) do + "Elixir." <> _ -> false + _ -> keyword?(rest) + end end def keyword?([]), do: true