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 stack on Finder #518

Merged
merged 8 commits into from
Jan 3, 2024
Merged

Use stack on Finder #518

merged 8 commits into from
Jan 3, 2024

Conversation

ypconstante
Copy link
Contributor

Today Finder uses Enum.reduce and nested calls to traverse_with to do it's filtering.

This works fine when using HTMLTree, but this structure makes it harder to optimize some cases in which the HTMLTree isn't necessary (#515).

This PRs updates Finder to use a stack, similar to what we have on HTMLTree.build_tree. This makes it easier to implement the change for the ticket above, or other future changes to operations that don't need the HTMLTree.build step.

I also did some other clean ups in this module, I've split each change in a separated commit.

Performance is basically the same, but when profiling the timing is a lot better and more readable because of the removal of Enum.reduce

Before - selector span a * i

%                                                           CNT           ACC           OWN        
[{ "<0.99.0>",                                           278908,    undefined,      442.743}].   %%

{  {'Elixir.Enum','-reduce/3-lists^foldl/2-0-',3},        29692,      402.120,       50.704}.      
{  {'Elixir.Floki.Finder',traverse_with,4},               18660,      309.861,       50.072}.      
{  {'Elixir.Floki.Selector','match?',3},                  12986,      160.655,       42.984}.      
{  {'Elixir.Floki.HTMLTree',build_tree,5},                15867,       39.761,       28.100}.      
{  {'Elixir.String',split,3},                              6522,       73.182,       26.562}.      

After

%                                                           CNT           ACC           OWN        
[{ "<0.99.0>",                                           231183,    undefined,      363.851}].   %%

{  {'Elixir.Floki.Finder',traverse_with,4},               38959,      322.261,       66.273}.      
{  {'Elixir.Floki.Selector','match?',3},                  12986,      158.041,       42.423}.      
{  {'Elixir.Floki.HTMLTree',build_tree,5},                15867,       40.198,       28.321}.      
{  {'Elixir.String',split,3},                              6522,       71.817,       26.114}.       

Copy link
Owner

@philss philss left a comment

Choose a reason for hiding this comment

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

Makes totally sense! Really cool! :shipit:

@philss philss merged commit d0be551 into philss:main Jan 3, 2024
9 checks passed
@ypconstante ypconstante deleted the use-stack-on-finder branch January 4, 2024 17:15
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