Skip to content

Cache common indentation in Inspect.Algebra - #15947

Merged
josevalim merged 2 commits into
elixir-lang:mainfrom
dkuku:dk_inspect_algebra_spaces16
Sep 26, 2026
Merged

josevalim merged 2 commits into
elixir-lang:mainfrom
dkuku:dk_inspect_algebra_spaces16

Conversation

@dkuku

@dkuku dkuku commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Precalculate common indentation spaces (0..64) and collapsed newlines (0..2) at compile time.

Previously, indent(i) allocated two intermediate binaries per break (:binary.copy and @newline <> ...), creating immediate garbage on the process heap. Inlining @newline into the binary builder and returning compile-time literals for spaces(0..64) eliminates these allocations.

I chose 64 because :binary.copy/2 has $O(N)$ allocation and copying cost, whereas compile-time literals provide $O(1)$ lookups. Indentation can get quite deep—for example, with anonymous functions passed as the last argument—so covering up to 64 bytes yields the greatest savings while remaining within the on-heap binary limit.

Benchee for DateTime.utc_now() |> Map.from_struct()

Name                            ips        average  deviation         median         99th %
AFTER (cached spaces)      373.16 K        2.68 μs   ±361.58%        2.47 μs        5.46 μs
BEFORE (main)              334.00 K        2.99 μs   ±416.09%        2.72 μs        6.80 μs

Comparison:
AFTER (cached spaces)      373.16 K
BEFORE (main)              334.00 K - 1.12x slower (+0.31 μs)

Memory usage statistics:

Name                     Memory usage
AFTER (cached spaces)         7.87 KB
BEFORE (main)                 8.48 KB - 1.08x memory usage (+0.61 KB)

Assisted-by: Gemini 3.8 Flash

Minimizing allocation overhead in inspection paths is critical, as formatting large terms during error logging is a frequent cause of VM out-of-memory failures under heavy load.

Inline newlines at break call sites and generate function heads for common indentation widths.

Assisted-by: Gemini 3.8 Flash
Comment thread lib/elixir/lib/inspect/algebra.ex Outdated
@josevalim
josevalim merged commit 25fa668 into elixir-lang:main Sep 26, 2026
15 checks passed
@josevalim

Copy link
Copy Markdown
Member

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants