Skip to content

Commit df3ee53

Browse files
authored
Optimize RowBinary map input encoding (#399)
* Optimize RowBinary map encoding * Use direct map fold for RowBinary encoding * Inline map fold callback * simpler * Test empty map iodata output * Add RowBinary encode test helper
1 parent e19815a commit df3ee53

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

lib/ch/row_binary.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,13 @@ defmodule Ch.RowBinary do
316316
[encode(:varint, length(m)) | encode_many_kv(m, k, v)]
317317
end
318318

319-
def encode({:map, _k, _v} = t, m) when is_map(m), do: encode(t, Map.to_list(m))
319+
def encode({:map, k, v}, m) when is_map(m) do
320+
[
321+
encode(:varint, map_size(m))
322+
| :maps.fold(fn key, value, acc -> [encode(k, key), encode(v, value) | acc] end, [], m)
323+
]
324+
end
325+
320326
def encode({:map, _k, _v}, []), do: 0
321327
def encode({:map, _k, _v}, nil), do: 0
322328

test/ch/row_binary_test.exs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ defmodule Ch.RowBinaryTest do
44
import Ch.RowBinary
55
import Bitwise
66

7+
defp encode_to_binary(type, value) do
8+
type |> encode(value) |> IO.iodata_to_binary()
9+
end
10+
711
test "encode -> decode" do
812
spec = [
913
{:string, ""},
@@ -167,7 +171,8 @@ defmodule Ch.RowBinaryTest do
167171

168172
test "map" do
169173
assert encode({:map, :string, :string}, []) == 0
170-
assert encode({:map, :string, :string}, %{}) == 0
174+
175+
assert encode_to_binary({:map, :string, :string}, %{}) == <<0>>
171176

172177
assert encode({:map, :string, :string}, %{"hello" => "world"}) ==
173178
encode({:map, :string, :string}, [{"hello", "world"}])
@@ -237,7 +242,7 @@ defmodule Ch.RowBinaryTest do
237242

238243
test "strings preserve raw bytes" do
239244
value = "\x61\xF0\x80\x80\x80b"
240-
str = IO.iodata_to_binary(encode(:string, value))
245+
str = encode_to_binary(:string, value)
241246

242247
assert decode_rows(str, [:string]) == [[value]]
243248

0 commit comments

Comments
 (0)