|
# 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) |