Skip to content

Instantly share code, notes, and snippets.

@zachdaniel
Last active October 4, 2026 06:38
Show Gist options
  • Select an option

  • Save zachdaniel/44b49113bf00bb6da71e22d7bea771f1 to your computer and use it in GitHub Desktop.

Select an option

Save zachdaniel/44b49113bf00bb6da71e22d7bea771f1 to your computer and use it in GitHub Desktop.
Elixir 1.20: inferred signatures for functions returning large literals bloat ExCk (repro)

Inferred signatures for functions returning large literals

Run with elixir repro.exs [modules] [entries]. It has no dependencies.

The script generates modules shaped like the ones DSL libraries (Spark/Ash, etc.) compile configuration into:

  • functions that return large literals (lists of structs, a map with ~100 keys)
  • small wrapper functions derived from them (fetch_persisted/1, config/0)

It compiles them with Kernel.ParallelCompiler, with infer_signatures on and then off.

Output on Elixir 1.20.4 / OTP 29:

== infer_on (infer_signatures: true)
compile time:     187ms for 41 files
total .beam size: 15861KB (396KB per module)
Repro.M1 chunks:  ExCk=325KB LitT=61KB Code=0KB

ExCk bytes per export (Repro.M1):
  config/0             132865
  fetch_persisted/1    67403
  persisted/0          67373
  entities/1           65668
  persisted/2          71

== infer_off (infer_signatures: false)
compile time:     154ms for 41 files
total .beam size: 2841KB (71KB per module)
Repro.M1 chunks:  ExCk=0KB LitT=61KB Code=0KB

Observations:

  1. The ExCk chunk is about 5x the size of the literal table (LitT) it describes. Inference makes the .beam files about 5.6x larger.
  2. Wrappers re-store the literal's type:
    • fetch_persisted/1 (Map.fetch(persisted(), key)) stores roughly the same amount as persisted/0.
    • config/0 stores the sum of the functions it calls.
  3. In real code these functions are almost always called dynamically (module.persisted() with module a variable), so the precise signatures are never used.

In a real Ash app (40 resources), a typical resource .beam is ~965KB, and ExCk is 685KB of that. Turning off inference there shrinks total beams from 39MB to 12.5MB and cuts a clean compile from ~6.9s to ~4.6s.

# Reproduction: inferred signatures for functions returning large literals.
#
# elixir repro.exs [modules] [entries]
#
# Generates `modules` modules, each holding a DSL-style data structure (lists of
# structs, a map of ~`entries` keys) as literals, the way libraries that compile
# configuration into modules do. Each module also has small wrapper functions
# derived from the literal-returning ones. The modules are compiled with
# Kernel.ParallelCompiler with signature inference on and off, and the script
# reports compile time, .beam size, and the ExCk chunk size per export.
{modules, entries} =
case System.argv() do
[m, e] -> {String.to_integer(m), String.to_integer(e)}
[m] -> {String.to_integer(m), 100}
[] -> {40, 100}
end
root = Path.join(System.tmp_dir!(), "literal_signature_repro")
File.rm_rf!(root)
src = Path.join(root, "src")
File.mkdir_p!(src)
# A struct shaped like a typical DSL entity: many fields, mixed value types.
File.write!(Path.join(src, "entity.ex"), """
defmodule Repro.Entity do
defstruct [
:name, :type, :kind, :description, :default, :constraints, :source,
allow_nil?: true, public?: false, primary_key?: false, writable?: true,
generated?: false, sensitive?: false, filterable?: true, sortable?: true,
tags: [], metadata: %{}, filters: [], arguments: []
]
end
""")
types = [:string, :integer, :boolean, :decimal, :date, :map, :uuid, :atom]
entity = fn mod, i ->
%{
__struct__: Repro.Entity,
name: :"field_#{i}",
type: Enum.at(types, rem(i, length(types))),
kind: Enum.at([:attribute, :argument, :calculation], rem(i, 3)),
description: if(rem(i, 2) == 0, do: "Field #{i} of #{inspect(mod)}"),
default: if(rem(i, 3) == 0, do: i),
constraints: [max_length: 100 + i, trim?: rem(i, 2) == 0, one_of: [:"a#{i}", :"b#{i}"]],
source: :"field_#{i}",
allow_nil?: rem(i, 2) == 0,
public?: rem(i, 3) != 0,
primary_key?: i == 0,
writable?: true,
generated?: false,
sensitive?: rem(i, 7) == 0,
filterable?: true,
sortable?: rem(i, 5) != 0,
tags: [:"tag_#{rem(i, 4)}", :"group_#{rem(i, 3)}"],
metadata: %{index: i, label: "Field #{i}", order: {i, rem(i, 10)}},
filters: [{:eq, :"field_#{i}", i}, {:in, :"field_#{i}", [i, i + 1]}],
arguments: []
}
end
for m <- 1..modules do
mod = Module.concat(Repro, "M#{m}")
sections = %{
[:fields] => Enum.map(0..19, &entity.(mod, &1)),
[:arguments] => Enum.map(20..29, &entity.(mod, &1)),
[:calculations] => Enum.map(30..37, &entity.(mod, &1))
}
all = sections |> Map.values() |> List.flatten()
# A "persisted" cache map, like the derived lookup tables DSL libraries build:
# by-name lookups, per-(action, input) flags, MapSets, plain lists.
persisted =
Map.new(all, &{{:by_name, &1.name}, &1})
|> Map.merge(Map.new(all, &{{:by_name, to_string(&1.name)}, &1}))
|> Map.merge(Map.new(1..max(entries - 2 * length(all), 0), &{{:input, :"action_#{rem(&1, 6)}", :"input_#{&1}"}, true}))
|> Map.merge(%{
names: Enum.map(all, & &1.name),
required: all |> Enum.reject(& &1.allow_nil?) |> Enum.map(& &1.name),
inputs: MapSet.new(Enum.map(all, & &1.name)),
module: mod
})
entity_clauses =
for {path, list} <- sections do
" def entities(#{inspect(path)}), do: #{inspect(list, limit: :infinity, structs: false)}"
end
File.write!(Path.join(src, "m#{m}.ex"), """
defmodule #{inspect(mod)} do
# Literal-returning accessors.
#{Enum.join(entity_clauses, "\n")}
def entities(_), do: []
def persisted, do: #{inspect(persisted, limit: :infinity, structs: false)}
# Small wrappers derived from the accessors above. Each re-stores the
# literal's type in its own inferred signature.
def fetch_persisted(key), do: Map.fetch(persisted(), key)
def persisted(key, default), do: Map.get(persisted(), key, default)
def config do
%{
persist: persisted(),
fields: entities([:fields]),
arguments: entities([:arguments]),
calculations: entities([:calculations])
}
end
end
""")
end
files = Path.wildcard(Path.join(src, "*.ex"))
compile = fn label, infer? ->
out = Path.join(root, label)
File.mkdir_p!(out)
Code.put_compiler_option(:infer_signatures, infer?)
{us, {:ok, _, _}} =
:timer.tc(fn -> Kernel.ParallelCompiler.compile_to_path(files, out, return_diagnostics: true) end)
beams = Path.wildcard(Path.join(out, "Elixir.Repro.M*.beam"))
total = beams |> Enum.map(&File.stat!(&1).size) |> Enum.sum()
{:ok, _, chunks} = :beam_lib.all_chunks(String.to_charlist(Path.join(out, "Elixir.Repro.M1.beam")))
chunk_sizes = Map.new(chunks, fn {k, v} -> {List.to_string(k), byte_size(v)} end)
IO.puts("""
== #{label} (infer_signatures: #{infer?})
compile time: #{div(us, 1000)}ms for #{length(files)} files
total .beam size: #{div(total, 1024)}KB (#{div(total, 1024 * length(beams))}KB per module)
Repro.M1 chunks: ExCk=#{div(chunk_sizes["ExCk"], 1024)}KB LitT=#{div(chunk_sizes["LitT"], 1024)}KB Code=#{div(chunk_sizes["Code"], 1024)}KB
""")
if infer? do
{_, %{exports: exports}} = :erlang.binary_to_term(Map.fetch!(Map.new(chunks), ~c"ExCk"))
IO.puts("ExCk bytes per export (Repro.M1):")
exports
|> Enum.map(fn {{f, a}, info} -> {"#{f}/#{a}", byte_size(:erlang.term_to_binary(info))} end)
|> Enum.sort_by(&elem(&1, 1), :desc)
|> Enum.each(fn {fa, size} -> IO.puts(" #{String.pad_trailing(fa, 20)} #{size}") end)
IO.puts("")
end
end
IO.puts("Elixir #{System.version()} / OTP #{System.otp_release()}, #{modules} modules\n")
# Warm up so the first measured run doesn't pay for loading the compiler.
Code.put_compiler_option(:ignore_module_conflict, true)
Kernel.ParallelCompiler.compile_to_path(files, Path.join(root, "warmup"), return_diagnostics: true)
compile.("infer_on", true)
compile.("infer_off", false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment