refactor: improve the speed of hexstr and binary conversions

This commit is contained in:
JianBo He 2021-03-23 17:13:01 +08:00 committed by Shawn
parent 5ea897f0f6
commit 277cab86d3
7 changed files with 30 additions and 45 deletions

View File

@ -273,7 +273,7 @@ clientinfo(ClientInfo =
message(#message{id = Id, qos = Qos, from = From, topic = Topic, payload = Payload, timestamp = Ts}) -> message(#message{id = Id, qos = Qos, from = From, topic = Topic, payload = Payload, timestamp = Ts}) ->
#{node => stringfy(node()), #{node => stringfy(node()),
id => hexstr(Id), id => emqx_guid:to_hexstr(Id),
qos => Qos, qos => Qos,
from => stringfy(From), from => stringfy(From),
topic => Topic, topic => Topic,
@ -304,12 +304,6 @@ stringfy(Term) when is_atom(Term) ->
stringfy(Term) -> stringfy(Term) ->
unicode:characters_to_binary((io_lib:format("~0p", [Term]))). unicode:characters_to_binary((io_lib:format("~0p", [Term]))).
hexstr(B) ->
<< <<(hexchar(H)), (hexchar(L))>> || <<H:4, L:4>> <= B>>.
hexchar(I) when I >= 0 andalso I < 10 -> I + $0;
hexchar(I) -> I - 10 + $A.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Acc funcs %% Acc funcs

View File

@ -205,7 +205,7 @@ handle_deliver(Delivers, Channel = #channel{clientinfo = ClientInfo}) ->
[ClientInfo], Msg), [ClientInfo], Msg),
NMsg = emqx_mountpoint:unmount(Mountpoint, Msg1), NMsg = emqx_mountpoint:unmount(Mountpoint, Msg1),
#{node => NodeStr, #{node => NodeStr,
id => hexstr(emqx_message:id(NMsg)), id => emqx_guid:to_hexstr(emqx_message:id(NMsg)),
qos => emqx_message:qos(NMsg), qos => emqx_message:qos(NMsg),
from => fmt_from(emqx_message:from(NMsg)), from => fmt_from(emqx_message:from(NMsg)),
topic => emqx_message:topic(NMsg), topic => emqx_message:topic(NMsg),
@ -591,9 +591,6 @@ default_clientinfo(#{peername := {PeerHost, _},
stringfy(Reason) -> stringfy(Reason) ->
unicode:characters_to_binary((io_lib:format("~0p", [Reason]))). unicode:characters_to_binary((io_lib:format("~0p", [Reason]))).
hexstr(Bin) ->
[io_lib:format("~2.16.0B",[X]) || <<X:8>> <= Bin].
fmt_from(undefined) -> <<>>; fmt_from(undefined) -> <<>>;
fmt_from(Bin) when is_binary(Bin) -> Bin; fmt_from(Bin) when is_binary(Bin) -> Bin;
fmt_from(T) -> stringfy(T). fmt_from(T) -> stringfy(T).

View File

@ -303,18 +303,7 @@ bin(Float) when is_float(Float) -> float_to_binary(Float).
decoding(Datas, <<"hex">>) -> decoding(Datas, <<"hex">>) ->
lists:map(fun(Data = #{<<"value">> := Value}) -> lists:map(fun(Data = #{<<"value">> := Value}) ->
Data#{<<"value">> => hexstr_to_bin(binary_to_list(Value))} Data#{<<"value">> => emqx_misc:hexstr2bin(Value)}
end, Datas); end, Datas);
decoding(Datas, _) -> decoding(Datas, _) ->
Datas. Datas.
hexstr_to_bin(S) ->
hexstr_to_bin(S, []).
hexstr_to_bin([], Acc) ->
list_to_binary(lists:reverse(Acc));
hexstr_to_bin([X,Y|T], Acc) ->
{ok, [V], []} = io_lib:fread("~16u", [X,Y]),
hexstr_to_bin(T, [V | Acc]);
hexstr_to_bin([X|T], Acc) ->
{ok, [V], []} = io_lib:fread("~16u", lists:flatten([X,"0"])),
hexstr_to_bin(T, [V | Acc]).

View File

@ -512,12 +512,10 @@ map(Data) ->
emqx_rule_utils:map(Data). emqx_rule_utils:map(Data).
bin2hexstr(Bin) when is_binary(Bin) -> bin2hexstr(Bin) when is_binary(Bin) ->
IntL = binary_to_list(Bin), emqx_misc:bin2hexstr(Bin).
list_to_binary([io_lib:format("~2.16.0B", [Int]) || Int <- IntL]).
hexstr2bin(Str) when is_binary(Str) -> hexstr2bin(Str) when is_binary(Str) ->
list_to_binary([binary_to_integer(W, 16) || <<W:2/binary>> <= Str]). emqx_misc:hexstr2bin(Str).
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% NULL Funcs %% NULL Funcs
@ -776,14 +774,7 @@ sha256(S) when is_binary(S) ->
hash(sha256, S). hash(sha256, S).
hash(Type, Data) -> hash(Type, Data) ->
hexstring(crypto:hash(Type, Data)). emqx_misc:bin2hexstr(crypto:hash(Type, Data)).
hexstring(<<X:128/big-unsigned-integer>>) ->
iolist_to_binary(io_lib:format("~32.16.0b", [X]));
hexstring(<<X:160/big-unsigned-integer>>) ->
iolist_to_binary(io_lib:format("~40.16.0b", [X]));
hexstring(<<X:256/big-unsigned-integer>>) ->
iolist_to_binary(io_lib:format("~64.16.0b", [X])).
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% Data encode and decode Funcs %% Data encode and decode Funcs

View File

@ -136,11 +136,11 @@ npid() ->
PidByte3:8, PidByte4:8>>, PidByte3:8, PidByte4:8>>,
NPid. NPid.
to_hexstr(<<I:128>>) -> to_hexstr(I) when byte_size(I) =:= 16 ->
list_to_binary(integer_to_list(I, 16)). emqx_misc:bin2hexstr(I).
from_hexstr(S) -> from_hexstr(S) when byte_size(S) =:= 32 ->
I = list_to_integer(binary_to_list(S), 16), <<I:128>>. emqx_misc:hexstr2bin(S).
to_base62(<<I:128>>) -> to_base62(<<I:128>>) ->
emqx_base62:encode(I). emqx_base62:encode(I).

View File

@ -45,6 +45,10 @@
, index_of/2 , index_of/2
]). ]).
-export([ bin2hexstr/1
, hexstr2bin/1
]).
%% @doc Merge options %% @doc Merge options
-spec(merge_opts(Opts, Opts) -> Opts when Opts :: proplists:proplist()). -spec(merge_opts(Opts, Opts) -> Opts when Opts :: proplists:proplist()).
merge_opts(Defaults, Options) -> merge_opts(Defaults, Options) ->
@ -233,3 +237,18 @@ index_of(E, I, [E|_]) ->
index_of(E, I, [_|L]) -> index_of(E, I, [_|L]) ->
index_of(E, I+1, L). index_of(E, I+1, L).
-spec(bin2hexstr(binary()) -> binary()).
bin2hexstr(B) when is_binary(B) ->
<< <<(int2hexchar(H)), (int2hexchar(L))>> || <<H:4, L:4>> <= B>>.
int2hexchar(I) when I >= 0 andalso I < 10 -> I + $0;
int2hexchar(I) -> I - 10 + $A.
-spec(hexstr2bin(binary()) -> binary()).
hexstr2bin(B) when is_binary(B) ->
<< <<(hexchar2int(H)*16 + hexchar2int(L))>> || <<H:8, L:8>> <= B>>.
hexchar2int(I) when I >= $0 andalso I =< $9 -> I - $0;
hexchar2int(I) when I >= $A andalso I =< $F -> I - $A + 10;
hexchar2int(I) when I >= $a andalso I =< $f -> I - $a + 10.

View File

@ -161,9 +161,4 @@ handler_name(Bin) ->
end. end.
hashstr(Bin) -> hashstr(Bin) ->
hexstr(crypto:hash(sha, Bin)). binary_to_list(emqx_misc:bin2hexstr(Bin)).
hexstr(Bin) ->
lists:flatten(
[io_lib:format("~2.16.0B", [Int])
|| Int <- binary_to_list(Bin)]).