dashbitco / mox

Mocks and explicit contracts in Elixir
1.35k stars 77 forks source link

Remove redundant ownership check #158

Closed ypconstante closed 4 months ago

ypconstante commented 4 months ago

Today Mox does a ownership check before doing an update to store the expectations. This call can be avoided because NimbleOwnership already does this same check on get_and_update, and the error allows us to do the error mapping.

Name                   ips        average  deviation         median         99th %
bench (PR)         36.47 K       27.42 μs    ±34.15%       25.69 μs       61.83 μs
bench (main)       32.33 K       30.93 μs    ±34.77%       28.91 μs       66.20 μs

Comparison: 
bench (PR)         36.47 K
bench (main)       32.33 K - 1.13x slower +3.52 μs

Memory usage statistics:

Name                 average  deviation         median         99th %
bench (PR)           7.08 KB     ±0.02%        7.08 KB        7.08 KB
bench (main)         7.53 KB     ±0.15%        7.53 KB        7.53 KB
defmodule BenchBehaviour do
  @functions Enum.map(1..50, &:"get_v#{&1}")

  for function <- @functions do
    @callback unquote(function)() :: integer()
  end
end

defmodule BenchBehaviourImpl do
  @behaviour BenchBehaviour

  @functions Enum.map(1..50, &:"get_v#{&1}")

  for function <- @functions do
    @impl BenchBehaviour
    def unquote(function)() do
      unquote(function)
    end
  end
end

Mox.defmock(BenchBehaviourMock, for: BenchBehaviour)

Benchee.run(
  %{
    "bench" => fn ->
      Mox.stub_with(BenchBehaviourMock, BenchBehaviourImpl)
      NimbleOwnership.cleanup_owner({:global, Mox.Server}, self())
    end
  },
  time: 10,
  memory_time: 2
)
josevalim commented 4 months ago

/cc @whatyouhide

josevalim commented 4 months ago

:green_heart: :blue_heart: :purple_heart: :yellow_heart: :heart: