Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exploring alt decoding strategies #4

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bench/decode.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ rows = [
]
]

header = [["a", "b", "c", "d"], ["UInt32", "String", "DateTime", "Array(String)"]]
header = [["a", "b", "d"], ["UInt32", "String", "Array(String)"]]

csv =
(header ++ CSV.encode_rows(rows)) |> NimbleCSV.RFC4180.dump_to_iodata() |> IO.iodata_to_binary()
Expand Down
75 changes: 74 additions & 1 deletion bench/dev.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,77 @@
defmodule Decoder do
@compile {:bin_opt_info, true}

def body_rec(<<i::32-little-signed, rest::bytes>>) do
[i | body_rec(rest)]
end

def body_rec(<<>>), do: []

def pivot(types, <<data::bytes>>) do
pivot__1(types, acc, data, types)
end

defp pivot_row([type | types], acc, <<bin::bytes>>, stack) do
decode_cell(types, bin, [{:row, [types, acc]}])
end

defp pivot_row([], acc, <<bin::bytes>>, types) do
pivot__1(types, acc, bin, types)
end

defp pivot_array(acc, type, count, <<bin::bytes>>, stack) when count > 0 do
decode_cell(type, bin, [{:array, acc, type, count - 1}])
end

defp pivot_array(acc, _type, _count, <<bin::bytes>>, stack) do
next(:lists.reverse(acc), bin, stack)
end

@compile inline: [decode_cell: 3]
defp decode_cell(type, <<bin::bytes>>, stack) do
case type do
:i32 ->
<<i::32-little-signed, bin::bytes>> = bin
next(i, bin, stack)

{:array, type} ->
<<0::1, count::7, bin::bytes>> = bin
pivot_array([], type, count, bin, stack)
end
end

@compile inline: [next: 3]
defp next(value, <<bin::bytes>>, [next | stack]) do
case next do
{:array, acc, type, count} -> pivot_array([value | acc], type, count, bin, stack)
{:row, acc, types} -> pivot_row(types, [value | acc], bin, stack)
types -> pivot_row(types, )
end
end
end

# TODO consider Native

ints = 0..1000

# data =
# Enum.into(ints, [])
# |> Ch.RowBinary.encode_row(List.duplicate(:i32, Enum.count(ints)))
# |> IO.iodata_to_binary()

cols = [
Enum.into(ints, []),
Enum.map(ints, &to_string/1)
]

Benchee.run(
%{},
%{
# "body_rec" => fn -> Decoder.body_rec(data) end,
# "pivot" => fn -> Decoder.pivot(data, :i32, Enum.count(ints)) end

# "apply" => fn -> apply(Kernel, :+, [1, 2]) end,
# "no-apply" => fn -> 1 + 2 end
"transpose" => fn -> Matrix.transpose(cols) end
},
memory_time: 2
)