The example you quoted from José does include guards. Just as many as the Heads-variant.
The ‘trick’ demonstrated by my Case-variant is the absence of 9.999 x 2 guards, as only a single function head has the 2 guards and all 9.999 case clauses can go without. As José mention, the same effect can be reached by having 1 pub function with guards delegating to 10.000 unguarded defp’s.
That being said: so far I could not reproduce any speed improvement with José’s Anon-variant in combination with Macro’s. Maybe the example is too simple for it to have enough impact to overcome an initial penalty..?
To be double clear: Yes, there is pattern matching on a specific integer AND a guards checking if the args are integers; the guards are useless and only added to test performance with guards.
defmodule AnonMacro do
defmacro fast do
quote bind_quoted: [] do
for i <- 1..10000 do
def foo(x = unquote(i), y = unquote(i)) when is_integer(x) and is_integer(y), do: unquote(i)
end
end
end
end
defmodule Anon.A do
import AnonMacro
fast()
end
defmodule Anon.B do
import AnonMacro
fast()
end
defmodule Anon.C do
import AnonMacro
fast()
end
The result
test3.ex compiled in 9551.563 ms (guard heavy, missing optimization technique)
-> Modules: [Heads.A, Heads.B, Heads.C]
test1.ex compiled in 10683.215 ms (guard heavy, wrapped in anon fun)
-> Modules: [AnonMacro, Anon.A, Anon.B, Anon.C]
The Schultzer-case
I still wonder why you would need more var!
Can you give us an example of a rewritten block?


















