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

Optimize Floki.children #533

Merged
merged 1 commit into from
Feb 9, 2024
Merged

Conversation

ypconstante
Copy link
Contributor

Floki.children is called a lot by Phoenix LiveView when doing tests. On the project I'm working on, this functions gets called more than 8 million times when running all of our tests.

This PR adds some pattern matching to this function to avoid using Access.get if possible.

Name                      ips        average  deviation         median         99th %
children (pr)         11.50 K       86.95 μs   ±239.29%       53.26 μs     1559.21 μs
children (main)        7.19 K      139.07 μs   ±145.06%      104.25 μs     1507.63 μs

Comparison: 
children (pr)         11.50 K
children (main)        7.19 K - 1.60x slower +52.12 μs

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

doc = read_file.("small.html")
html_nodes = Floki.find(doc, "*")

Benchee.run(
  %{
    "children" => fn ->
      for html_node <- html_nodes do
        Floki.children(html_node)
        Floki.children(html_node, include_text: false)
        Floki.children(html_node, include_text: true)
      end
    end
  },
  time: 10,
  save: [path: "benchs/results/finder-#{tag}.benchee", tag: tag],
  memory_time: 2
)


assert Floki.children(html_node) == expected
assert Floki.children(html_node, include_text: true) == expected
assert Floki.children(html_node, include_text: true, unknown_option: true) == expected
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why "unknown_option" is needed here?

test/floki_test.exs Show resolved Hide resolved
@philss philss merged commit 712c08a into philss:main Feb 9, 2024
9 checks passed
@ypconstante ypconstante deleted the optimize-floki-children branch February 17, 2024 16:43
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