From dc388a71b4d7d8d743a45e02e19f3117e17ecd3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Thu, 7 Aug 2025 09:03:49 +0200 Subject: [PATCH 1/3] Fix regression in LazyHTML.Tree.to_html/1 memory usage --- lib/lazy_html.ex | 2 +- lib/lazy_html/tree.ex | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/lazy_html.ex b/lib/lazy_html.ex index 358b37e..d814697 100644 --- a/lib/lazy_html.ex +++ b/lib/lazy_html.ex @@ -498,7 +498,7 @@ defmodule LazyHTML do """ @spec html_escape(String.t()) :: String.t() def html_escape(string) when is_binary(string) do - LazyHTML.Tree.append_escaped(string, "") + LazyHTML.Tree.html_escape(string) end # Access diff --git a/lib/lazy_html/tree.ex b/lib/lazy_html/tree.ex index 29edc32..cfa38b1 100644 --- a/lib/lazy_html/tree.ex +++ b/lib/lazy_html/tree.ex @@ -133,8 +133,9 @@ defmodule LazyHTML.Tree do # # [1]: https://github.com/phoenixframework/phoenix_html/blob/v4.2.1/lib/phoenix_html/engine.ex#L29-L35 - @doc false - def append_escaped(text, html) do + # Note: it is important for this function to be private to maximise + # optimisation, see https://github.com/dashbitco/lazy_html/pull/18#issuecomment-3162730539. + defp append_escaped(text, html) do append_escaped(text, text, 0, 0, html) end @@ -275,4 +276,7 @@ defmodule LazyHTML.Tree do tree end + + @doc false + def html_escape(string), do: append_escaped(string, "") end From 8bbf384274042d4ac90738984be67f581ff99182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Thu, 7 Aug 2025 09:39:28 +0200 Subject: [PATCH 2/3] Update comment --- lib/lazy_html/tree.ex | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/lazy_html/tree.ex b/lib/lazy_html/tree.ex index cfa38b1..25350d2 100644 --- a/lib/lazy_html/tree.ex +++ b/lib/lazy_html/tree.ex @@ -133,8 +133,11 @@ defmodule LazyHTML.Tree do # # [1]: https://github.com/phoenixframework/phoenix_html/blob/v4.2.1/lib/phoenix_html/engine.ex#L29-L35 - # Note: it is important for this function to be private to maximise - # optimisation, see https://github.com/dashbitco/lazy_html/pull/18#issuecomment-3162730539. + # Note: it is important for this function to be private, so that the + # Erlang compiler can infer that it is safe to use mutating appends + # on the underlying binary and maximise optimisations. + # + # See https://github.com/dashbitco/lazy_html/pull/18#issuecomment-3162730539. defp append_escaped(text, html) do append_escaped(text, text, 0, 0, html) end From 952055268279c7b56aa8ecbe00f1ae68ce37e12b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Thu, 7 Aug 2025 09:42:21 +0200 Subject: [PATCH 3/3] Up --- lib/lazy_html/tree.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/lazy_html/tree.ex b/lib/lazy_html/tree.ex index 25350d2..ee30891 100644 --- a/lib/lazy_html/tree.ex +++ b/lib/lazy_html/tree.ex @@ -135,9 +135,9 @@ defmodule LazyHTML.Tree do # Note: it is important for this function to be private, so that the # Erlang compiler can infer that it is safe to use mutating appends - # on the underlying binary and maximise optimisations. + # on the underlying binary and maximise optimisations [1]. # - # See https://github.com/dashbitco/lazy_html/pull/18#issuecomment-3162730539. + # [1]: https://github.com/dashbitco/lazy_html/pull/19 defp append_escaped(text, html) do append_escaped(text, text, 0, 0, html) end