Skip to content

Commit b86fcba

Browse files
committed
Avoid creating list of props
1 parent 478c172 commit b86fcba

File tree

1 file changed

+23
-44
lines changed

1 file changed

+23
-44
lines changed

lib/protobuf/encoder.ex

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ defmodule Protobuf.Encoder do
3434

3535
# We encode the fields in order since some recommended conformance tests expect us to do so.
3636
encoded =
37-
ordered_tags
38-
|> Enum.map(fn fnum -> Map.fetch!(field_props, fnum) end)
39-
|> encode_fields(syntax, struct, oneofs, _acc = [])
37+
for fnum <- ordered_tags,
38+
prop = Map.fetch!(field_props, fnum),
39+
encoded = encode_field(prop, struct, oneofs, syntax),
40+
encoded != :skip do
41+
encoded
42+
end
4043

4144
encoded = [encoded | encode_unknown_fields(struct)]
4245

@@ -47,26 +50,6 @@ defmodule Protobuf.Encoder do
4750
end
4851
end
4952

50-
defp encode_fields(_fields = [], _syntax, _struct, _oneofs, acc) do
51-
acc
52-
end
53-
54-
defp encode_fields(
55-
[prop | rest],
56-
syntax,
57-
struct,
58-
oneofs,
59-
acc
60-
) do
61-
acc =
62-
case encode_field(prop, struct, oneofs, syntax) do
63-
{:ok, iodata} -> [acc | iodata]
64-
:skip -> acc
65-
end
66-
67-
encode_fields(rest, syntax, struct, oneofs, acc)
68-
end
69-
7053
defp encode_field(%FieldProps{name_atom: name, oneof: oneof} = prop, struct, oneofs, syntax) do
7154
val =
7255
if oneof do
@@ -109,8 +92,7 @@ defmodule Protobuf.Encoder do
10992
if skip_field?(syntax, val, prop) or skip_enum?(syntax, val, prop) do
11093
:skip
11194
else
112-
iodata = apply_or_map(val, repeated?, &[fnum | Wire.encode(type, &1)])
113-
{:ok, iodata}
95+
apply_or_map(val, repeated?, &[fnum | Wire.encode(type, &1)])
11496
end
11597
end
11698

@@ -124,23 +106,20 @@ defmodule Protobuf.Encoder do
124106
syntax,
125107
%FieldProps{encoded_fnum: fnum, repeated?: repeated?, map?: map?, type: type} = prop
126108
) do
127-
result =
128-
apply_or_map(val, repeated? || map?, fn val ->
129-
val = transform_module(val, type)
130-
131-
if skip_field?(syntax, val, prop) do
132-
""
133-
else
134-
val = if map?, do: struct(type, %{key: elem(val, 0), value: elem(val, 1)}), else: val
135-
136-
# so that oneof {:atom, val} can be encoded
137-
encoded = encode_from_type(type, val)
138-
byte_size = IO.iodata_length(encoded)
139-
[fnum | Varint.encode(byte_size)] ++ encoded
140-
end
141-
end)
142-
143-
{:ok, result}
109+
apply_or_map(val, repeated? || map?, fn val ->
110+
val = transform_module(val, type)
111+
112+
if skip_field?(syntax, val, prop) do
113+
""
114+
else
115+
val = if map?, do: struct(type, %{key: elem(val, 0), value: elem(val, 1)}), else: val
116+
117+
# so that oneof {:atom, val} can be encoded
118+
encoded = encode_from_type(type, val)
119+
byte_size = IO.iodata_length(encoded)
120+
[fnum | Varint.encode(byte_size)] ++ encoded
121+
end
122+
end)
144123
end
145124

146125
defp do_encode_field(:packed, val, syntax, %FieldProps{type: type, encoded_fnum: fnum} = prop) do
@@ -149,7 +128,7 @@ defmodule Protobuf.Encoder do
149128
else
150129
encoded = Enum.map(val, &Wire.encode(type, &1))
151130
byte_size = IO.iodata_length(encoded)
152-
{:ok, [fnum | Varint.encode(byte_size)] ++ encoded}
131+
[fnum | Varint.encode(byte_size)] ++ encoded
153132
end
154133
end
155134

@@ -264,8 +243,8 @@ defmodule Protobuf.Encoder do
264243
case Protobuf.Extension.get_extension_props(mod, ext_mod, key) do
265244
%{field_props: prop} ->
266245
case do_encode_field(class_field(prop), val, :proto2, prop) do
267-
{:ok, iodata} -> [acc | iodata]
268246
:skip -> acc
247+
iodata -> [acc | iodata]
269248
end
270249

271250
_ ->

0 commit comments

Comments
 (0)