Skip to content

Commit

Permalink
WIP (Move index limit matching to the top)
Browse files Browse the repository at this point in the history
It gives great speedup for complex structures while degrade
performance for plain long collections (though, it is still way faster than
original).
  • Loading branch information
lexmag committed Apr 11, 2017
1 parent 08549bf commit 1955c5e
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/msgpax/unpacker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,35 +83,35 @@ defmodule Msgpax.Unpacker do

import Macro, only: [pipe: 3]

defp unpack(<<buffer::bits>>, result, options, [kind, index, size | outer], count, count) do
unpack(buffer, build_collection(result, count, kind), options, outer, index + 1, size)
end

for {format, {:value, value}} <- formats do
defp unpack(<<unquote_splicing(format), rest::bits>>, result, options, outer, index, count) when index < count do
defp unpack(<<unquote_splicing(format), rest::bits>>, result, options, outer, index, count) do
unpack(rest, [unquote(value) | result], options, outer, index + 1, count)
end
end

for {format, {:call, call}} <- formats do
options = Macro.var(:options, nil)
defp unpack(<<unquote_splicing(format), rest::bits>>, result, options, outer, index, count) when index < count do
defp unpack(<<unquote_splicing(format), rest::bits>>, result, options, outer, index, count) do
unpack(rest, [unquote(pipe(options, call, 0)) | result], options, outer, index + 1, count)
end
end

for {format, {:collection, :list = kind}} <- formats do
defp unpack(<<unquote_splicing(format), rest::bits>>, result, options, [kind | outer], index, count) when index < count do
unpack(rest, result, options, [unquote(kind), {kind, index, count} | outer], 0, unquote(quote(do: len)))
defp unpack(<<unquote_splicing(format), rest::bits>>, result, options, outer, index, count) do
unpack(rest, result, options, [unquote(kind), index, count | outer], 0, unquote(quote(do: len)))
end
end

for {format, {:collection, :map = kind}} <- formats do
defp unpack(<<unquote_splicing(format), rest::bits>>, result, options, [kind | outer], index, count) when index < count do
unpack(rest, result, options, [unquote(kind), {kind, index, count} | outer], 0, unquote(quote(do: len)) * 2)
defp unpack(<<unquote_splicing(format), rest::bits>>, result, options, outer, index, count) do
unpack(rest, result, options, [unquote(kind), index, count | outer], 0, unquote(quote(do: len)) * 2)
end
end

defp unpack(<<buffer::bits>>, result, options, [kind, {next, index, size} | outer], count, count) do
unpack(buffer, build_collection(result, count, kind), options, [next | outer], index + 1, size)
end

defp unpack(<<byte, _::bits>>, [], _options, _outer, _index, _count) do
throw {:bad_format, byte}
end
Expand Down

0 comments on commit 1955c5e

Please sign in to comment.