From b1766c8ff90638f85d6993e5c2986c39792737c3 Mon Sep 17 00:00:00 2001 From: Daniel Kukula Date: Thu, 27 Aug 2026 23:03:07 +0200 Subject: [PATCH 1/2] Cache common indentation for small binaries Inline newlines at break call sites and generate function heads for common indentation widths. Assisted-by: Gemini 3.8 Flash --- lib/elixir/lib/inspect/algebra.ex | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/elixir/lib/inspect/algebra.ex b/lib/elixir/lib/inspect/algebra.ex index 34ff70e4ae..a6f3073435 100644 --- a/lib/elixir/lib/inspect/algebra.ex +++ b/lib/elixir/lib/inspect/algebra.ex @@ -1295,7 +1295,7 @@ defmodule Inspect.Algebra do do: format(w, k, t, acc) defp format(w, _, i, _, doc_line(), t, acc), - do: format(w, i, t, <>) + do: format(w, i, t, <>) defp format(w, k, i, m, doc_cons(x, y), t, acc), do: format(w, k, i, m, x, [{i, m, y} | t], acc) @@ -1325,14 +1325,14 @@ defmodule Inspect.Algebra do if w == :infinity or m == :flat or fits?(w, k, true, t) do format(w, k, t, <>) else - format(w, i, t, <>) + format(w, i, t, <>) end end # Strict breaks are conditional to the mode defp format(w, k, i, mode, doc_break(s, :strict), t, acc) do if mode == :break do - format(w, i, t, <>) + format(w, i, t, <>) else format(w, k + byte_size(s), t, <>) end @@ -1388,9 +1388,14 @@ defmodule Inspect.Algebra do end defp collapse(t, max, count, i) do - [:binary.copy("\n", min(max, count)), :binary.copy(" ", i) | t] + [newlines(min(max, count)), spaces(i) | t] end + defp newlines(0), do: "" + defp newlines(1), do: @newline + defp newlines(2), do: @newline <> @newline + defp newlines(n), do: :binary.copy(@newline, n) + defp strip_whitespace(" " <> rest), do: strip_whitespace(rest) defp strip_whitespace(rest), do: rest @@ -1398,6 +1403,11 @@ defmodule Inspect.Algebra do defp apply_nesting(_, _, :reset), do: 0 defp apply_nesting(i, _, j), do: i + j - defp indent(0), do: @newline - defp indent(i), do: @newline <> :binary.copy(" ", i) + for i <- 0..64 do + defp spaces(unquote(i)) do + unquote(:binary.copy(" ", i)) + end + end + + defp spaces(i), do: :binary.copy(" ", i) end From b54acbb79fde079c3950e47e58c4dca20f1c9735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 26 Sep 2026 22:23:19 +0200 Subject: [PATCH 2/2] Update lib/elixir/lib/inspect/algebra.ex --- lib/elixir/lib/inspect/algebra.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/elixir/lib/inspect/algebra.ex b/lib/elixir/lib/inspect/algebra.ex index a6f3073435..5afeb393a3 100644 --- a/lib/elixir/lib/inspect/algebra.ex +++ b/lib/elixir/lib/inspect/algebra.ex @@ -1403,7 +1403,7 @@ defmodule Inspect.Algebra do defp apply_nesting(_, _, :reset), do: 0 defp apply_nesting(i, _, j), do: i + j - for i <- 0..64 do + for i <- 0..16 do defp spaces(unquote(i)) do unquote(:binary.copy(" ", i)) end