aboutsummaryrefslogtreecommitdiffstats
path: root/src/cow_http.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2014-12-12 20:14:05 +0200
committerLoïc Hoguin <[email protected]>2014-12-12 20:14:05 +0200
commit9485f21cc2efb1d013b72c1f0ba47e0589ed7ea8 (patch)
treea5113510135388d0f6f6e8fa40b81769fe8baa06 /src/cow_http.erl
parent9044538ed63075aaf44fb683b3da4202f41902c4 (diff)
downloadcowlib-9485f21cc2efb1d013b72c1f0ba47e0589ed7ea8.tar.gz
cowlib-9485f21cc2efb1d013b72c1f0ba47e0589ed7ea8.tar.bz2
cowlib-9485f21cc2efb1d013b72c1f0ba47e0589ed7ea8.zip
Optimize cow_http:parse_fullhost/1
Using two functions instead of an argument is a much better way to handle two specific pathways in the code.
Diffstat (limited to 'src/cow_http.erl')
-rw-r--r--src/cow_http.erl50
1 files changed, 39 insertions, 11 deletions
diff --git a/src/cow_http.erl b/src/cow_http.erl
index f7e3cdd..8504a21 100644
--- a/src/cow_http.erl
+++ b/src/cow_http.erl
@@ -206,21 +206,27 @@ horse_parse_headers() ->
%% to lowercase.
-spec parse_fullhost(binary()) -> {binary(), undefined | non_neg_integer()}.
+parse_fullhost(<< $[, Rest/bits >>) ->
+ parse_fullhost_ipv6(Rest, << $[ >>);
parse_fullhost(Fullhost) ->
- parse_fullhost(Fullhost, false, <<>>).
+ parse_fullhost(Fullhost, <<>>).
-parse_fullhost(<< $[, Rest/bits >>, false, <<>>) ->
- parse_fullhost(Rest, true, << $[ >>);
-parse_fullhost(<<>>, false, Acc) ->
+parse_fullhost_ipv6(<< $] >>, Acc) ->
+ {<< Acc/binary, $] >>, undefined};
+parse_fullhost_ipv6(<< $], $:, Rest/bits >>, Acc) ->
+ {<< Acc/binary, $] >>, binary_to_integer(Rest)};
+parse_fullhost_ipv6(<< C, Rest/bits >>, Acc) ->
+ case C of
+ ?INLINE_LOWERCASE(parse_fullhost_ipv6, Rest, Acc)
+ end.
+
+parse_fullhost(<<>>, Acc) ->
{Acc, undefined};
-%% @todo Optimize.
-parse_fullhost(<< $:, Rest/bits >>, false, Acc) ->
- {Acc, list_to_integer(binary_to_list(Rest))};
-parse_fullhost(<< $], Rest/bits >>, true, Acc) ->
- parse_fullhost(Rest, false, << Acc/binary, $] >>);
-parse_fullhost(<< C, Rest/bits >>, E, Acc) ->
+parse_fullhost(<< $:, Rest/bits >>, Acc) ->
+ {Acc, binary_to_integer(Rest)};
+parse_fullhost(<< C, Rest/bits >>, Acc) ->
case C of
- ?INLINE_LOWERCASE(parse_fullhost, Rest, E, Acc)
+ ?INLINE_LOWERCASE(parse_fullhost, Rest, Acc)
end.
-ifdef(TEST).
@@ -238,6 +244,28 @@ parse_fullhost_test() ->
ok.
-endif.
+-ifdef(PERF).
+horse_parse_fullhost_blue_example_org() ->
+ horse:repeat(200000,
+ parse_fullhost(<<"blue.example.org:8080">>)
+ ).
+
+horse_parse_fullhost_ipv4() ->
+ horse:repeat(200000,
+ parse_fullhost(<<"192.0.2.1:8080">>)
+ ).
+
+horse_parse_fullhost_ipv6() ->
+ horse:repeat(200000,
+ parse_fullhost(<<"[2001:db8::1]:8080">>)
+ ).
+
+horse_parse_fullhost_ipv6_v4() ->
+ horse:repeat(200000,
+ parse_fullhost(<<"[::ffff:192.0.2.1]:8080">>)
+ ).
+-endif.
+
%% @doc Extract path and query string from a binary.
-spec parse_fullpath(binary()) -> {binary(), binary()}.