Browse Source

Improve paddings encoding performance

Takeru Ohta 8 years ago
parent
commit
c363e98f48
1 changed files with 10 additions and 2 deletions
  1. 10 2
      src/jsone_encode.erl

+ 10 - 2
src/jsone_encode.erl

@@ -268,7 +268,7 @@ pp_newline(Buf, Level, Opt) -> pp_newline(Buf, Level, 0, Opt).
 
 -spec pp_newline(binary(), list(), non_neg_integer(), opt()) -> binary().
 pp_newline(Buf, _, _,     ?OPT{indent = 0}) -> Buf;
-pp_newline(Buf, L, Extra, ?OPT{indent = N}) -> lists:foldl(fun (_, B) -> padding(B, N) end, padding(<<Buf/binary, $\n>>, Extra * N), L).
+pp_newline(Buf, L, Extra, ?OPT{indent = N}) -> padding(<<Buf/binary, $\n>>, Extra * N + length(L) * N).
 
 -spec pp_newline_or_space(binary(), list(), opt()) -> binary().
 pp_newline_or_space(Buf, _, Opt = ?OPT{indent = 0}) -> pp_space(Buf, Opt);
@@ -276,7 +276,15 @@ pp_newline_or_space(Buf, L, Opt)                    -> pp_newline(Buf, L, Opt).
 
 -spec padding(binary(), non_neg_integer()) -> binary().
 padding(Buf, 0) -> Buf;
-padding(Buf, N) -> padding(<<Buf/binary, $ >>, N - 1).
+padding(Buf, 1) -> <<Buf/binary, " ">>;
+padding(Buf, 2) -> <<Buf/binary, "  ">>;
+padding(Buf, 3) -> <<Buf/binary, "   ">>;
+padding(Buf, 4) -> <<Buf/binary, "    ">>;
+padding(Buf, 5) -> <<Buf/binary, "     ">>;
+padding(Buf, 6) -> <<Buf/binary, "      ">>;
+padding(Buf, 7) -> <<Buf/binary, "       ">>;
+padding(Buf, 8) -> <<Buf/binary, "        ">>;
+padding(Buf, N) -> padding(<<Buf/binary, "         ">>, N - 9).
 
 -spec parse_options([jsone:encode_option()]) -> opt().
 parse_options(Options) ->