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

Find using descendant selector without html tree #537

Merged

Conversation

ypconstante
Copy link
Contributor

This PR applies the new optimized find when the selector contains only descendant selector. The performance improvement on this one is even higher than in the previous PR, and is pretty close to a find using a selector without descendant.

For descendant selector the html tree traversal ends up traversing the same element multiple times. In this traverse we'll check each element only once.

This happens because this traverse doesn't add all element upfront with the initial selector, and instead add each children as we navigate the stack. On a match, we can add the children items with the combinator selector directly, which avoids traversing this element twice and avoids duplicating the element in the result, which we are not able to dedup on this find.

##### With input big #####
Name                                 ips        average  deviation         median         99th %
descendant selector (pr)          215.44        4.64 ms     ±6.15%        4.59 ms        6.28 ms
tag name (type) (main)            211.81        4.72 ms     ±6.52%        4.67 ms        6.37 ms
tag name (type) (pr)              209.38        4.78 ms     ±5.93%        4.73 ms        6.42 ms
descendant selector (main)         12.26       81.53 ms    ±14.32%       75.86 ms      139.62 ms

Comparison: 
descendant selector (pr)          215.44
tag name (type) (main)            211.81 - 1.02x slower +0.0796 ms
tag name (type) (pr)              209.38 - 1.03x slower +0.134 ms
descendant selector (main)         12.26 - 17.57x slower +76.89 ms

Memory usage statistics:

Name                          Memory usage
descendant selector (pr)           2.15 MB
tag name (type) (main)             2.13 MB - 0.99x memory usage -0.01691 MB
tag name (type) (pr)               2.13 MB - 0.99x memory usage -0.01691 MB
descendant selector (main)        37.71 MB - 17.57x memory usage +35.56 MB

**All measurements for memory usage were the same**

##### With input medium #####
Name                                 ips        average  deviation         median         99th %
tag name (type) (pr)              631.51        1.58 ms     ±4.89%        1.57 ms        1.93 ms
tag name (type) (main)            613.71        1.63 ms     ±4.27%        1.61 ms        1.90 ms
descendant selector (pr)          593.79        1.68 ms     ±6.78%        1.66 ms        2.08 ms
descendant selector (main)         40.00       25.00 ms     ±7.08%       24.25 ms       29.80 ms

Comparison: 
tag name (type) (pr)              631.51
tag name (type) (main)            613.71 - 1.03x slower +0.0459 ms
descendant selector (pr)          593.79 - 1.06x slower +0.101 ms
descendant selector (main)         40.00 - 15.79x slower +23.41 ms

Memory usage statistics:

Name                          Memory usage
tag name (type) (pr)             719.06 KB
tag name (type) (main)           719.06 KB - 1.00x memory usage +0 KB
descendant selector (pr)         757.27 KB - 1.05x memory usage +38.20 KB
descendant selector (main)     13525.02 KB - 18.81x memory usage +12805.95 KB

**All measurements for memory usage were the same**

##### With input small #####
Name                                 ips        average  deviation         median         99th %
tag name (type) (main)            3.02 K      331.48 μs     ±8.45%      313.67 μs      432.52 μs
tag name (type) (pr)              2.96 K      338.14 μs     ±9.32%      323.77 μs      435.99 μs
descendant selector (pr)          2.95 K      338.70 μs     ±9.56%      325.16 μs      448.01 μs
descendant selector (main)        0.25 K     4029.49 μs    ±11.35%     3837.64 μs     5322.13 μs

Comparison: 
tag name (type) (main)            3.02 K
tag name (type) (pr)              2.96 K - 1.02x slower +6.66 μs
descendant selector (pr)          2.95 K - 1.02x slower +7.22 μs
descendant selector (main)        0.25 K - 12.16x slower +3698.01 μs

Memory usage statistics:

Name                          Memory usage
tag name (type) (main)           148.43 KB
tag name (type) (pr)             148.43 KB - 1.00x memory usage +0 KB
descendant selector (pr)         149.43 KB - 1.01x memory usage +1 KB
descendant selector (main)      2492.95 KB - 16.80x memory usage +2344.52 KB

@philss
Copy link
Owner

philss commented Feb 16, 2024

@ypconstante I didn't quite get this. The diff only includes a change in the "bench" file. Is this correct?

@ypconstante ypconstante force-pushed the find-descendant-selector-without-html-tree branch from 8deb26e to 0fe0670 Compare February 17, 2024 13:54
@ypconstante
Copy link
Contributor Author

ypconstante commented Feb 17, 2024

I swapped the content to be commited with the content to be stashed 🤦‍♂️ .
Now it's correct

@philss
Copy link
Owner

philss commented Feb 17, 2024

@ypconstante awesome! :D

@philss philss merged commit 0840a05 into philss:main Feb 17, 2024
9 checks passed
@ypconstante ypconstante deleted the find-descendant-selector-without-html-tree branch February 17, 2024 16:42
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