-
Notifications
You must be signed in to change notification settings - Fork 9
Optimize LazyHTML.Tree.to_html with large trees #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize LazyHTML.Tree.to_html with large trees #18
Conversation
|
In theory the binary should be faster and use fewer memory since we are reusing the match context: Can you please share the benchmark script you are using? You can also try this diff and see if it changes anything. |
|
Also, if you are using iodata, you don't need Enum.reverse, you can write operations like this |
|
Benchee script is in the PR description, html files are the ones used by Floki benchmark. On main branch - Same output with the patch |
|
This is actually crazy. When adding the original implementation I benchmarked a few versions and the current one was clearly superior, with runtime being close or better and 5 times lower memory usage. However, this is no longer the case on main (as indicated by @ypconstante results). I tracked it down and it's a regression from #14. Now, if I make this change, it restores the previous behaviour: - def append_escaped(text, html) do
+ defp append_escaped(text, html) do |
Just to clarify, it's not about match context (which has to do with recursive traversal, and that's the same in both implementations), it's about the runtime optimising binary appends. I don't think |
In my mind, "runtime optimizing binary appends" is the match context handling. If the match context was not optimized, then we would not see this optimization. But I found it weird nothing was warned about no match context being created and anothing about the append_attrs or to_html functions. |
|
Nah, you are right, I am getting two different optimizations confused. Apologies. Ship it. |
|
Closing in favour of #19. @ypconstante thank you very much for the PR, otherwise we wouldn't spot the regression! |
Today
LazyHTML.Tree.to_htmlis really slow on large trees. Replacing the binary accumulator with a list makes calls with large tree significantly faster, and speeds up a little calls with smaller trees.This does increase a little the memory usage, but considering the performance improvement seems to be worth.