Skip to content

Commit

Permalink
Encoding of compact arrays and objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoeter committed Mar 26, 2018
1 parent 33924cc commit 826355d
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 109 deletions.
32 changes: 23 additions & 9 deletions lib/velocy_pack/codegen.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,34 @@ defmodule VelocyPack.Codegen do

import Bitwise

defmacro __using__(_opts) do
funcs = for i <- 1..4 do
name = "build_index_table_#{1 <<< (i - 1)}" |> String.to_atom
quote do
def unquote(name) ([], acc, _offset), do: IO.iodata_to_binary(acc)
def unquote(name) ([h | tail], acc, offset) do
acc = [<<(h + offset)::unsigned-little-unit(8)-size(power_of_2(unquote(i - 1)))>> | acc]
unquote(name)(tail, acc, offset)
end
end
end

quote do
import VelocyPack.Codegen
unquote(funcs)
end
end

defmacro index_table(offsets, offset, bytes) do
name = "build_index_table_#{bytes}" |> String.to_atom
quote do: unquote(name)(unquote(offsets), [], unquote(offset))
end

defmacro power_of_2(exp) do
result = 1 <<< exp
quote do: unquote(result)
end

defmacro index_table(offsets, offset, 1), do:
quote do: for i <- unquote(offsets), do: <<(i + unquote(offset))::unsigned-size(8)>>
defmacro index_table(offsets, offset, 2), do:
quote do: for i <- unquote(offsets), do: <<(i + unquote(offset))::unsigned-little-size(16)>>
defmacro index_table(offsets, offset, 4), do:
quote do: for i <- unquote(offsets), do: <<(i + unquote(offset))::unsigned-little-size(32)>>
defmacro index_table(offsets, offset, 8), do:
quote do: for i <- unquote(offsets), do: <<(i + unquote(offset))::unsigned-little-size(64)>>

defmacro bytecase(var, do: clauses) do
{ranges, default, literals} = clauses_to_ranges(clauses, [])

Expand Down
Loading

0 comments on commit 826355d

Please sign in to comment.