Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

poc: fasten hook #45

Merged
merged 1 commit into from
Jan 14, 2025
Merged

poc: fasten hook #45

merged 1 commit into from
Jan 14, 2025

Conversation

artemeff
Copy link
Member

@artemeff artemeff commented Nov 19, 2022

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, c: c, d: d, e: e)}
  end
end

defp __cast_a__(%{"a" => term}, opts) do
  Construct.Type.cast(:string, term, opts)
end

defp __cast_a__(%{a: term}, opts) do
  Construct.Type.cast(:string, term, opts)
end

defp __cast_a__(_, _) do
  {:error, %{a: :missing}}
end

defp __cast_b__(%{"b" => term}, opts) do
  Construct.Type.cast(:float, term, opts)
end

defp __cast_b__(%{b: term}, opts) do
  Construct.Type.cast(:float, term, opts)
end

defp __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

@coveralls
Copy link

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%
Totals Coverage Status
Change from base Build 38acc7c1eb0c6cdaa11568f7e2393dd93eb8841a: -7.0%
Covered Lines: 333
Relevant Lines: 394

💛 - Coveralls

@artemeff
Copy link
Member Author

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**

@artemeff artemeff force-pushed the fasten branch 2 times, most recently from 4b39a60 to 75b3fd0 Compare January 14, 2025 05:28
@artemeff artemeff merged commit f32d9ce into ExpressApp:master Jan 14, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants