Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ahamez committed Feb 3, 2025
1 parent cfba9a7 commit 0d2d5df
Show file tree
Hide file tree
Showing 17 changed files with 409 additions and 292 deletions.
4 changes: 3 additions & 1 deletion benchmark/mix/tasks/protox/benchmark/generate/payloads.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ defmodule Mix.Tasks.Protox.Benchmark.Generate.Payloads do
end

Stream.repeatedly(fn -> :proper_gen.pick(gen, 5) end)
|> Stream.map(fn {:ok, msg} -> {msg, msg |> Protox.encode!() |> IO.iodata_to_binary()} end)
|> Stream.map(fn {:ok, msg} ->
{msg, msg |> Protox.encode!() |> elem(0) |> IO.iodata_to_binary()}
end)
|> Stream.reject(fn {_msg, bytes} -> byte_size(bytes) == 0 end)
|> Stream.reject(fn {_msg, bytes} -> byte_size(bytes) > 16_384 * 16 end)
|> Stream.map(fn {msg, bytes} -> {msg, byte_size(bytes), bytes} end)
Expand Down
5 changes: 3 additions & 2 deletions benchmark/mix/tasks/protox/benchmark/run.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ defmodule Mix.Tasks.Protox.Benchmark.Run do
:encode ->
%{
encode: fn input ->
Enum.map(input, fn {msg, _size, _bytes} -> msg.__struct__.encode!(msg) end)
Enum.each(input, fn {msg, _size, _bytes} -> msg.__struct__.encode!(msg) end)
end
}

:decode ->
%{
decode: fn input ->
Enum.map(input, fn {msg, _size, bytes} -> msg.__struct__.decode!(bytes) end)
Enum.each(input, fn {msg, _size, bytes} -> msg.__struct__.decode!(bytes) end)
end
}
end
Expand Down Expand Up @@ -82,5 +82,6 @@ defmodule Mix.Tasks.Protox.Benchmark.Run do
path
|> File.read!()
|> :erlang.binary_to_term()
|> Map.drop([ProtobufTestMessages.Proto3.TestAllTypesProto3])
end
end
4 changes: 2 additions & 2 deletions conformance/protox/conformance/escript.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ defmodule Protox.Conformance.Escript do
IO.binwrite(log_file, "Message: #{inspect(msg, limit: :infinity)}\n")

try do
encoded_payload = msg |> Protox.encode!() |> :binary.list_to_bin()
encoded_payload = msg |> Protox.encode!() |> elem(0) |> :binary.list_to_bin()

IO.binwrite(
log_file,
Expand Down Expand Up @@ -155,7 +155,7 @@ defmodule Protox.Conformance.Escript do
end

defp make_message_bytes(%Conformance.ConformanceResponse{} = msg) do
data = msg |> Protox.encode!() |> :binary.list_to_bin()
data = msg |> Protox.encode!() |> elem(0) |> :binary.list_to_bin()

<<byte_size(data)::unsigned-little-32, data::binary>>
end
Expand Down
6 changes: 3 additions & 3 deletions lib/protox.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ defmodule Protox do
Throwing version of `encode/1`.
"""
@doc since: "1.6.0"
@spec encode!(struct()) :: iodata() | no_return()
@spec encode!(struct()) :: {iodata(), non_neg_integer()} | no_return()
def encode!(msg) do
msg.__struct__.encode!(msg)
end
Expand All @@ -137,7 +137,7 @@ defmodule Protox do
## Examples
iex> msg = %ProtoxExample{a: 3, b: %{1 => "some string"}}
iex> {:ok, iodata} = Protox.encode(msg)
iex> {:ok, iodata, _iodata_size} = Protox.encode(msg)
iex> :binary.list_to_bin(iodata)
<<8, 3, 18, 15, 8, 1, 18, 11, 115, 111, 109, 101, 32, 115, 116, 114, 105, 110, 103>>
Expand All @@ -148,7 +148,7 @@ defmodule Protox do
"""
@doc since: "1.6.0"
@spec encode(struct()) :: {:ok, iodata()} | {:error, any()}
@spec encode(struct()) :: {:ok, iodata(), non_neg_integer()} | {:error, any()}
def encode(msg) do
msg.__struct__.encode(msg)
end
Expand Down
5 changes: 2 additions & 3 deletions lib/protox/define_decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,7 @@ defmodule Protox.DefineDecoder do
end
end

# Compute at compile time the varint representation of a field
# tag and wire type.
# Compute at compile time the varint representation of a field tag and wire type.
defp make_key_bytes(%Field{} = field) do
# We need to convert the type to something recognized
# by Protox.Encode.make_key_bytes/2.
Expand All @@ -673,6 +672,6 @@ defmodule Protox.DefineDecoder do
_ -> field.type
end

Protox.Encode.make_key_bytes(field.tag, ty) |> IO.iodata_to_binary()
Protox.Encode.make_key_bytes(field.tag, ty) |> elem(0) |> IO.iodata_to_binary()
end
end
Loading

0 comments on commit 0d2d5df

Please sign in to comment.