Skip to content

Commit 1947e63

Browse files
committed
Simplify same_method?/2
1 parent 1dd3601 commit 1947e63

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

lib/mint/http2.ex

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ defmodule Mint.HTTP2 do
13651365
end
13661366

13671367
defp add_pseudo_headers(headers, conn, method, path) do
1368-
if is_method?(method, ~c"CONNECT") do
1368+
if same_method?(method, "CONNECT") do
13691369
[
13701370
{":method", method},
13711371
{":authority", conn.authority}
@@ -1382,19 +1382,20 @@ defmodule Mint.HTTP2 do
13821382
end
13831383
end
13841384

1385-
@spec is_method?(proposed :: binary(), method :: charlist()) :: boolean()
1386-
defp is_method?(<<>>, []), do: true
1385+
# same_method?/2 is pretty optimized, so bench before changing.
13871386

1388-
defp is_method?(<<char, rest_bin::binary>>, [char | rest_list]) do
1389-
is_method?(rest_bin, rest_list)
1390-
end
1387+
# Same binary, which is a common case.
1388+
defp same_method?(bin, bin), do: true
13911389

1392-
defp is_method?(<<lower_char, rest_bin::binary>>, [char | rest_list])
1393-
when lower_char >= ?a and lower_char <= ?z and lower_char - 32 == char do
1394-
is_method?(rest_bin, rest_list)
1395-
end
1390+
# Get out early if the size is different, these can't be the same.
1391+
defp same_method?(bin1, bin2) when byte_size(bin1) != byte_size(bin2), do: false
1392+
1393+
defp same_method?(<<ch, rest1::binary>>, <<ch, rest2::binary>>), do: same_method?(rest1, rest2)
1394+
1395+
defp same_method?(<<lower, rest1::binary>>, <<char, rest2::binary>>) when lower - 32 == char,
1396+
do: same_method?(rest1, rest2)
13961397

1397-
defp is_method?(_proposed, _method), do: false
1398+
defp same_method?(_method1, _method2), do: false
13981399

13991400
defp sort_pseudo_headers_to_front(headers) do
14001401
Enum.sort_by(headers, fn {key, _value} ->

0 commit comments

Comments
 (0)