From 10611e9e540dc6a2fd3a8261e0bbedc7a350f4c9 Mon Sep 17 00:00:00 2001 From: bagarey Date: Tue, 10 Sep 2019 14:19:19 +0200 Subject: [PATCH 1/2] Improve encoding performance https://stackoverflow.com/questions/46095870/whats-the-best-way-to-build-long-strings-in-elixir This has been modified to append short binaries to longer ones. --- lib/exoml/encoder.ex | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/exoml/encoder.ex b/lib/exoml/encoder.ex index fc660d6..578afce 100644 --- a/lib/exoml/encoder.ex +++ b/lib/exoml/encoder.ex @@ -5,43 +5,47 @@ defmodule Exoml.Encoder do end defp encode(bin, acc) when is_binary(bin) do - bin <> acc + acc <> bin end defp encode([node | tl], acc) do - encode(node, encode(tl, acc)) + encode(tl, encode(node, acc)) end defp encode([], acc), do: acc defp encode({:prolog, attrs, nil}, acc) do - " encode_attrs(attrs) <> " ?>" <> acc + acc <> " encode_attrs(attrs) <> " ?>" end defp encode({:doctype, attrs, nil}, acc) do - " encode_attrs(attrs) <> " !>" <> acc + acc <> " encode_attrs(attrs) <> " !>" end defp encode({tag, [], nil}, acc) do - "<#{tag}/>" <> acc + acc <> "<#{tag}/>" end defp encode({tag, attrs, nil}, acc) do - "<#{tag} #{encode_attrs(attrs)} />" <> acc + acc <> "<#{tag} #{encode_attrs(attrs)} />" end defp encode({tag, [], children}, acc) do - "<#{tag}>" <> encode(children, "") <> "" <> acc + acc <> "<#{tag}>" <> encode(children, "") <> "" end defp encode({tag, attrs, children}, acc) do - "<" <> tag <> " " <> encode_attrs(attrs) <> ">" <> encode(children, "") <> "" <> acc + acc <> "<" <> tag <> " " <> encode_attrs(attrs) <> ">" <> encode(children, "") <> "" end defp encode_attrs(attrs) do encode_attrs(attrs, "") end + defp encode_attrs([attr | tl], "") do + encode_attr(attr) <> encode_attrs(tl) + end + defp encode_attrs([attr | tl], acc) do trail = encode_attr(attr) if acc == "" do From 2e8af15902d8d4b20e6edb1d1dcfd57243211988 Mon Sep 17 00:00:00 2001 From: bagarey Date: Tue, 10 Sep 2019 14:23:16 +0200 Subject: [PATCH 2/2] Align with the fix https://github.com/Overbryd/exoml/commit/588b75346d787bf8e3dacbf446c6b2c5b6d33725#diff-5e2f4f549b64731d8e261939505fe53a --- lib/exoml/encoder.ex | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/exoml/encoder.ex b/lib/exoml/encoder.ex index 578afce..151add4 100644 --- a/lib/exoml/encoder.ex +++ b/lib/exoml/encoder.ex @@ -42,10 +42,6 @@ defmodule Exoml.Encoder do encode_attrs(attrs, "") end - defp encode_attrs([attr | tl], "") do - encode_attr(attr) <> encode_attrs(tl) - end - defp encode_attrs([attr | tl], acc) do trail = encode_attr(attr) if acc == "" do