Skip to content
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

Use recursion instead of Enum.flat_map on find_selectors #508

Merged
merged 2 commits into from
Dec 27, 2023

Conversation

ypconstante
Copy link
Contributor

@ypconstante ypconstante commented Dec 21, 2023

Today find_selectors and it's child functions use Enum.flat_map to interact through the nodes to find the matching ones.

This PR replaces the Enum.flat_map and Enum.map usages with recursion, removing most of the intermediate lists this methods had, and also simplifying traverse_with implementation.

This also provides a really small performance improvement. When checking this benchmark with fprof, operations count went from 65867 to 57726, reducing the time to run with fprof from 115ms to 95ms.

Name            ips        average  deviation         median         99th %
pr           555.12        1.80 ms    ±13.53%        1.72 ms        2.75 ms
today        544.31        1.84 ms    ±12.83%        1.75 ms        2.79 ms

Comparison: 
pr           555.12
today        544.31 - 1.02x slower +0.0358 ms

Memory usage statistics:

Name     Memory usage
pr            2.05 MB
today         2.09 MB - 1.02x memory usage +0.0319 MB

read_file = fn name ->
  __ENV__.file
  |> Path.dirname()
  |> Path.join(name)
  |> File.read!()
end

html_input = read_file.("small.html")

[{"html", _, _} = html | _] = Floki.parse_document!(html_input)

Benchee.run(
  %{
    "bench" => fn -> Floki.Finder.find(html, ".mw-parser-output > p")  end
  },
  time: 5,
  memory_time: 2
)

@ypconstante ypconstante changed the title Use Enum.reduce instead of Enum.flat_map on find_selectors Use recursion instead of Enum.flat_map on find_selectors Dec 22, 2023
@philss
Copy link
Owner

philss commented Dec 27, 2023

I have to say it was hard to remember this part of the code 😅
Nicely done! Thank you! 💜

@philss philss merged commit ff1d247 into philss:main Dec 27, 2023
9 checks passed
@ypconstante ypconstante deleted the simplify-find-selectors branch December 28, 2023 01:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants