1
0
Fork 0
mirror of https://github.com/processone/ejabberd synced 2025-10-06 03:50:15 +02:00

Faster string_to_jid/1 implementation

This version is about 10-15% faster than old one
This commit is contained in:
Paweł Chmielowski 2015-11-03 14:40:52 +01:00
parent ff46738218
commit e54e543a66
2 changed files with 49 additions and 32 deletions

View file

@ -43,6 +43,7 @@
start(normal, _Args) -> start(normal, _Args) ->
ejabberd_logger:start(), ejabberd_logger:start(),
write_pid_file(), write_pid_file(),
jlib:start(),
start_apps(), start_apps(),
ejabberd:check_app(ejabberd), ejabberd:check_app(ejabberd),
randoms:start(), randoms:start(),

View file

@ -58,11 +58,19 @@
atom_to_binary/1, binary_to_atom/1, tuple_to_binary/1, atom_to_binary/1, binary_to_atom/1, tuple_to_binary/1,
l2i/1, i2l/1, i2l/2, queue_drop_while/2]). l2i/1, i2l/1, i2l/2, queue_drop_while/2]).
-export([start/0]).
-include("ejabberd.hrl"). -include("ejabberd.hrl").
-include("jlib.hrl"). -include("jlib.hrl").
-export_type([jid/0]). -export_type([jid/0]).
start() ->
SplitPattern = binary:compile_pattern([<<"@">>, <<"/">>]),
catch ets:new(jlib, [named_table, protected, set, {keypos, 1}]),
ets:insert(jlib, {string_to_jid_pattern, SplitPattern}),
ok.
%send_iq(From, To, ID, SubTags) -> %send_iq(From, To, ID, SubTags) ->
% ok. % ok.
@ -215,7 +223,7 @@ make_jid({User, Server, Resource}) ->
make_jid(User, Server, Resource). make_jid(User, Server, Resource).
%% This is the reverse of make_jid/1 %% This is the reverse of make_jid/1
-spec split_jid(jid()) -> {binary(), binary(), binary()} | error. -spec split_jid(jid()) -> {binary(), binary(), binary()} | error.
split_jid(#jid{user = U, server = S, resource = R}) -> split_jid(#jid{user = U, server = S, resource = R}) ->
{U, S, R}; {U, S, R};
split_jid(_) -> split_jid(_) ->
@ -224,36 +232,44 @@ split_jid(_) ->
-spec string_to_jid(binary()) -> jid() | error. -spec string_to_jid(binary()) -> jid() | error.
string_to_jid(S) -> string_to_jid(S) ->
string_to_jid1(binary_to_list(S), ""). SplitPattern = ets:lookup_element(jlib, string_to_jid_pattern, 2),
Size = size(S),
string_to_jid1([$@ | _J], "") -> error; End = Size-1,
string_to_jid1([$@ | J], N) -> case binary:match(S, SplitPattern) of
string_to_jid2(J, lists:reverse(N), ""); {0, _} ->
string_to_jid1([$/ | _J], "") -> error; error;
string_to_jid1([$/ | J], N) -> {End, _} ->
string_to_jid3(J, "", lists:reverse(N), ""); error;
string_to_jid1([C | J], N) -> {Pos1, _} ->
string_to_jid1(J, [C | N]); case binary:at(S, Pos1) of
string_to_jid1([], "") -> error; $/ ->
string_to_jid1([], N) -> make_jid(<<>>,
make_jid(<<"">>, list_to_binary(lists:reverse(N)), <<"">>). binary:part(S, 0, Pos1),
binary:part(S, Pos1+1, Size-Pos1-1));
%% Only one "@" is admitted per JID _ ->
string_to_jid2([$@ | _J], _N, _S) -> error; Pos1N = Pos1+1,
string_to_jid2([$/ | _J], _N, "") -> error; case binary:match(S, SplitPattern, [{scope, {Pos1+1, Size-Pos1-1}}]) of
string_to_jid2([$/ | J], N, S) -> {End, _} ->
string_to_jid3(J, N, lists:reverse(S), ""); error;
string_to_jid2([C | J], N, S) -> {Pos1N, _} ->
string_to_jid2(J, N, [C | S]); error;
string_to_jid2([], _N, "") -> error; {Pos2, _} ->
string_to_jid2([], N, S) -> case binary:at(S, Pos2) of
make_jid(list_to_binary(N), list_to_binary(lists:reverse(S)), <<"">>). $/ ->
make_jid(binary:part(S, 0, Pos1),
string_to_jid3([C | J], N, S, R) -> binary:part(S, Pos1+1, Pos2-Pos1-1),
string_to_jid3(J, N, S, [C | R]); binary:part(S, Pos2+1, Size-Pos2-1));
string_to_jid3([], N, S, R) -> _ -> error
make_jid(list_to_binary(N), list_to_binary(S), end;
list_to_binary(lists:reverse(R))). _ ->
make_jid(binary:part(S, 0, Pos1),
binary:part(S, Pos1+1, Size-Pos1-1),
<<>>)
end
end;
_ ->
make_jid(<<>>, S, <<>>)
end.
-spec jid_to_string(jid() | ljid()) -> binary(). -spec jid_to_string(jid() | ljid()) -> binary().
@ -856,7 +872,7 @@ decode_base64(S) ->
decode_base64_bin(S, <<>>) decode_base64_bin(S, <<>>)
end. end.
take_without_spaces(Bin, Count) -> take_without_spaces(Bin, Count) ->
take_without_spaces(Bin, Count, <<>>). take_without_spaces(Bin, Count, <<>>).
take_without_spaces(Bin, 0, Acc) -> take_without_spaces(Bin, 0, Acc) ->