Skip to content

Commit

Permalink
refactor: don't check if wire_type is correct by looking into a list
Browse files Browse the repository at this point in the history
It can provides a 1.10 speedup factor on some payloads, as well as a small decrease of 1.03 for other payloads.
  • Loading branch information
ahamez committed Jan 27, 2025
1 parent 08aba80 commit 0fccf4d
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/protox/decode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ defmodule Protox.Decode do
@spec parse_key(binary()) :: {non_neg_integer(), non_neg_integer(), binary()}
def parse_key(bytes) do
{key, rest} = Varint.decode(bytes)
wire_type = key &&& 0b0000_0111

if wire_type in [@wire_32bits, @wire_64bits, @wire_delimited, @wire_varint] do
{key >>> 3, wire_type, rest}
else
raise Protox.DecodingError.new(bytes, "invalid wire type #{wire_type}")
case _wire_type = key &&& 0b0000_0111 do
@wire_32bits -> {key >>> 3, @wire_32bits, rest}
@wire_64bits -> {key >>> 3, @wire_64bits, rest}
@wire_delimited -> {key >>> 3, @wire_delimited, rest}
@wire_varint -> {key >>> 3, @wire_varint, rest}
wire_type -> raise Protox.DecodingError.new(bytes, "invalid wire type #{wire_type}")
end
end

Expand Down

0 comments on commit 0fccf4d

Please sign in to comment.