Say we have several function heads
or case do
patterns to match on.
Will placing “common case” or “most likely” headers first offer any benefits?
I’m seeing timing improvements when I re-order case do
match patterns in ELIXIR, putting the most common pattern first.
I had expected the VM to optimize this away, and the order shouldn’t matter.
Example:
for {s, i} <- li do
case :binary.split(s, spliter, [:global]) do
[_, tag, val, _, _] ->
# this pattern occurs over 9/10 times.
# in files with over 100000 rows matching this pattern
# reduced processing time
{:tv, i, tag, val}
[_, "data", _] ->
Process.put(:data_tag, true)
{:tb, i, "data"}
[_, "/data", _] ->
Process.put(:data_tag, false)
{:te, i, "data"}
[_, "/" <> tag, _] ->
if Process.get(:data_tag) do
{nil, i}
else
{:te, i, tag}
end
[_, "?xml" <> _, _] ->
{nil, i}
[_, "!--" <> _, _] ->
{nil, i}
[_, "return" <> _, _] ->
{nil, i}
[_, tag, _] ->
if Process.get(:data_tag) do
{:tv, i, tag, ""}
else
{:tb, i, tag}
end
_ ->
{nil, i}
end
end