construct
construct copied to clipboard
poc: fasten hook
proof of concept to compile module make/2 function into something like this:
def make(params, opts) do
with {:ok, a} <- cast_a(params, opts),
{:ok, b} <- cast_b(params, opts) do
{:ok, struct(ExampleFast, a: a, b: b)}
end
end
def cast_a(%{"a" => term}, opts) do
Construct.Type.cast(:string, term, opts)
end
def cast_a(%{a: term}, opts) do
Construct.Type.cast(:string, term, opts)
end
def cast_a(_, _) do
{:error, %{a: :missing}}
end
def cast_b(%{"b" => term}, opts) do
Construct.Type.cast(:float, term, opts)
end
def cast_b(%{b: term}, opts) do
Construct.Type.cast(:float, term, opts)
end
def cast_b(_, _) do
{:error, %{b: :missing}}
end
this prevents types traversal to increase performance by almost 3 times. results from bench/struct_bench.exs
Operating System: macOS
CPU Information: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
Number of Available Cores: 16
Available memory: 64 GB
Elixir 1.14.0
Erlang 25.0.4
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 3 s
memory time: 3 s
reduction time: 3 s
parallel: 1
inputs: none specified
Estimated total run time: 22 s
Benchmarking make ...
Benchmarking make optimized ...
Name ips average deviation median 99th %
make optimized 799.50 K 1.25 μs ±2251.41% 1.08 μs 1.69 μs
make 300.32 K 3.33 μs ±419.27% 2.99 μs 5.04 μs
Comparison:
make optimized 799.50 K
make 300.32 K - 2.66x slower +2.08 μs
Memory usage statistics:
Name Memory usage
make optimized 1.12 KB
make 3.43 KB - 3.07x memory usage +2.31 KB
**All measurements for memory usage were the same**
Reduction count statistics:
Name Reduction count
make optimized 109
make 362 - 3.32x reduction count +253
Pull Request Test Coverage Report for Build 260d0bc00fda028425e2d03d4d37b6eb8c3aff79-PR-45
- 1 of 31 (3.23%) changed or added relevant lines in 2 files are covered.
- No unchanged relevant lines lost coverage.
- Overall coverage decreased (-7.0%) to 84.518%
Changes Missing Coverage | Covered Lines | Changed/Added Lines | % |
---|---|---|---|
lib/construct/hooks/fasten.ex | 0 | 30 | 0.0% |
<!-- | Total: | 1 | 31 |
Totals | |
---|---|
Change from base Build 38acc7c1eb0c6cdaa11568f7e2393dd93eb8841a: | -7.0% |
Covered Lines: | 333 |
Relevant Lines: | 394 |
💛 - Coveralls
Operating System: macOS
CPU Information: Apple M3 Pro
Number of Available Cores: 12
Available memory: 36 GB
Elixir 1.17.3
Erlang 27.1
Benchmark suite executing with the following configuration:
warmup: 2 s
time: 3 s
memory time: 3 s
reduction time: 3 s
parallel: 1
inputs: none specified
Estimated total run time: 22 s
Benchmarking make ...
Benchmarking make optimized ...
Name ips average deviation median 99th %
make optimized 2.85 M 350.96 ns ±2847.14% 333 ns 459 ns
make 1.14 M 880.07 ns ±1296.72% 833 ns 1125 ns
Comparison:
make optimized 2.85 M
make 1.14 M - 2.51x slower +529.10 ns
Memory usage statistics:
Name Memory usage
make optimized 1.01 KB
make 3.15 KB - 3.12x memory usage +2.14 KB
**All measurements for memory usage were the same**
Reduction count statistics:
Name Reduction count
make optimized 109
make 362 - 3.32x reduction count +253
**All measurements for reduction count were the same**