Просмотр исходного кода

Merge pull request #177 from jlouis/jl/microoptimizations

Jl/microoptimizations
Sergey Prokhorov 6 лет назад
Родитель
Сommit
334d68e05d
1 измененных файлов с 14 добавлено и 9 удалено
  1. 14 9
      src/epgsql_wire.erl

+ 14 - 9
src/epgsql_wire.erl

@@ -45,8 +45,13 @@ decode_string(Bin) ->
 %% @doc decode multiple null-terminated string
 -spec decode_strings(binary()) -> [binary(), ...].
 decode_strings(Bin) ->
-    [<<>> | T] = lists:reverse(binary:split(Bin, <<0>>, [global])),
-    lists:reverse(T).
+    %% Assert the last byte is what we want it to be
+    %% Remove that byte from the Binary, so the zero
+    %% terminators are separators. Then apply
+    %% binary:split/3 directly on the remaining Subj
+    Sz = byte_size(Bin) - 1,
+    <<Subj:Sz/binary, 0>> = Bin,
+    binary:split(Subj, <<0>>, [global]).
 
 %% @doc decode error's field
 -spec decode_fields(binary()) -> [{byte(), binary()}].
@@ -133,15 +138,15 @@ build_decoder(Columns, Codec) ->
 
 %% @doc decode row data
 -spec decode_data(binary(), row_decoder()) -> tuple().
-decode_data(Bin, {Decoders, Columns, Codec}) ->
-    list_to_tuple(decode_data(Bin, Decoders, Columns, Codec)).
+decode_data(Bin, {Decoders, _Columns, Codec}) ->
+    list_to_tuple(decode_data(Bin, Decoders, Codec)).
 
-decode_data(_, [], [], _) -> [];
-decode_data(<<-1:?int32, Rest/binary>>, [_Dec | Decs], [_Col | Cols], Codec) ->
-    [null | decode_data(Rest, Decs, Cols, Codec)];
-decode_data(<<Len:?int32, Value:Len/binary, Rest/binary>>, [Decoder | Decs], [_Col | Cols], Codec) ->
+decode_data(_, [], _) -> [];
+decode_data(<<-1:?int32, Rest/binary>>, [_Dec | Decs], Codec) ->
+    [null | decode_data(Rest, Decs, Codec)];
+decode_data(<<Len:?int32, Value:Len/binary, Rest/binary>>, [Decoder | Decs], Codec) ->
     [epgsql_binary:decode(Value, Decoder)
-     | decode_data(Rest, Decs, Cols, Codec)].
+     | decode_data(Rest, Decs, Codec)].
 
 %% @doc decode column information
 -spec decode_columns(non_neg_integer(), binary(), epgsql_binary:codec()) -> [epgsql:column()].