You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
REXML::Node#each_recursive is a fundamental operation to traverse XML nodes. In particular, CSS selector uses this method heavily.
Unfortunately, this method is extremely slow. The following Gist contains a tiny (but mostly equivalent) each_recursive implementation and their benchmarks.
And, the below is a result of this script on my laptop (Apple M1 Pro, 14 inch, 32 GB) and Ruby 3.3.2.
$ ruby bench.rb user system total real each_recursive 2.421999 0.008742 2.430741 ( 2.453438)my_each_recursive 0.057647 0.000049 0.057696 ( 0.058718)
$ ruby --yjit bench.rb user system total real each_recursive 1.613353 0.008227 1.621580 ( 1.625374)my_each_recursive 0.025493 0.000124 0.025617 ( 0.025748)
Yes, REXML's each_recursive is ~50x (or ~80x with YJIT) slower than my tiny implementation.
I believe REXML does a lot of extra work, and we can make it faster.
Activity
kou commentedon May 31, 2024
Let's improve performance of it:
BTW, are you interesting in merging the CSS selector feature to REXML itself?
makenowjust commentedon Jun 3, 2024
@kou Thank you. I will try to improve its performance.
I am also interested in merging the CSS selector feature to REXML.
But, there are some problems and concerns:
rexml-css_selectorimplementation uses the latest Ruby features. We need to downgrade the source (or use a transpiler much likeruby-next.)I will create a separate issue on this matter.
Improve `Node#each_recursive` performance (ruby#139)