From 1a2c87f0035849fc5a24bff5dd36796500d1f451 Mon Sep 17 00:00:00 2001 From: Anders Svensson Date: Sat, 29 Jul 2017 16:53:18 +0200 Subject: Optimize sub-binaries With ERL_COMPILER_OPTIONS=bin_opt_info, before: base/diameter_codec.erl:508: Warning: NOT OPTIMIZED: the binary matching instruction that follows in the same function have problems that prevent delayed sub binary optimization (probably indicated by INFO warnings) And after: base/diameter_codec.erl:508: Warning: OPTIMIZED: creation of sub binary delayed This has a surprisingly large impact on the performance of diameter_codec:collect_avps/2: about 15% faster in one testcase on a RAR with 7 AVPs. --- lib/diameter/src/base/diameter_codec.erl | 51 ++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 23 deletions(-) (limited to 'lib/diameter/src') diff --git a/lib/diameter/src/base/diameter_codec.erl b/lib/diameter/src/base/diameter_codec.erl index 81c21fb8f2..c171ea9dca 100644 --- a/lib/diameter/src/base/diameter_codec.erl +++ b/lib/diameter/src/base/diameter_codec.erl @@ -506,20 +506,33 @@ collect_avps(<<_:20/binary, Avps/binary>>) -> %% +-+-+-+-+-+-+-+-+ collect(<>) -> - Vid = if 1 == V -> I; 0 == V -> undefined end, - DataLen = Len - 8 - V*4, %% Might be negative, which ensures - Pad = ?PAD(Len), %% failure of the match below. - MB = 1 == M, - PB = 1 == P, - - case Rest of - <> -> + collect(Rest, + Code, + if 1 == V -> I; 0 == V -> undefined end, + Len - 8 - V*4, %% Might be negative, which ensures + ?PAD(Len), %% failure of the match below. + 1 == M, + 1 == P); + +collect(<<>>) -> + []; + +%% Header is truncated. pack_avp/1 will pad this at encode if sent in +%% a Failed-AVP. +collect(Bin) -> + [#diameter_avp{data = {5014, Bin}}]. + +%% collect/7 + +collect(Bin, Code, Vid, DataLen, Pad, M, P) -> + case Bin of + <> -> Avp = #diameter_avp{code = Code, vendor_id = Vid, - is_mandatory = MB, - need_encryption = PB, + is_mandatory = M, + need_encryption = P, data = Data}, - [Avp | collect(T)]; + [Avp | collect(Rest)]; _ -> %% Length in header points past the end of the message, or %% doesn't span the header. Note that an length error can @@ -529,18 +542,10 @@ collect(<>) -> %% know the types of the AVPs being extracted. [#diameter_avp{code = Code, vendor_id = Vid, - is_mandatory = MB, - need_encryption = PB, - data = {5014, Rest}}] - end; - -collect(<<>>) -> - []; - -%% Header is truncated. pack_avp/1 will pad this at encode if sent in -%% a Failed-AVP. -collect(Bin) -> - [#diameter_avp{data = {5014, Bin}}]. + is_mandatory = M, + need_encryption = P, + data = {5014, Bin}}] + end. %% 3588: %% -- cgit v1.2.3