Optimize REXML::XPathParser#sort with depth-first search#314
Open
naitoh wants to merge 1 commit into
Open
Conversation
Replace the O(n x depth) algorithm that walked the parent chain for every node calling parent.index(self) at each level, with a single O(N) depth-first search (DFS) that assigns an integer document-order position to every node in the tree, then sorts by those integers with sort_by. New helpers: - sort_anchor: maps attribute nodes to their owner element so attributes sort by their element document position (matching the previous behavior). - document_order_positions: iterative DFS from each unique root using an explicit stack, assigning monotonically increasing counters in document order. Multiple disjoint subtrees are handled by tracking visited roots.
There was a problem hiding this comment.
Pull request overview
This PR optimizes REXML::XPathParser#sort by replacing repeated parent-chain/index lookups with a single DFS pass that assigns document-order positions and then sorts nodes by those positions, improving XPath evaluation performance for large nodesets.
Changes:
- Reimplemented
XPathParser#sortusing a DFS-based document-order position map (document_order_positions). - Added
sort_anchorto preserve prior attribute ordering semantics by sorting attributes by their owning element’s position. - Added regression tests to ensure correct document ordering for attribute axes across elements and mixed text/element child nodes.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
lib/rexml/xpath_parser.rb |
Replaces sort implementation with DFS-based document-order position assignment and sorting. |
test/xpath/test_base.rb |
Adds tests covering attribute-axis ordering across elements and mixed child-node ordering. |
benchmark/xpath.yaml |
Increases benchmark depth/width parameters to better stress ordering and traversal performance. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+678
to
+685
| nodes.each do |node| | ||
| anchor = sort_anchor(node) | ||
| root = anchor | ||
| while (parent = root.parent) | ||
| root = parent | ||
| end | ||
| new_arry << [ node_idx.reverse, node ] | ||
| } | ||
| ordered = new_arry.sort_by do |index, node| | ||
| if order == :forward | ||
| index | ||
| else | ||
| index.map(&:-@) | ||
| next if visited_roots.key?(root) | ||
| visited_roots[root] = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace the O(n x depth) algorithm that walked the parent chain for every node calling parent.index(self) at each level, with a single O(N) depth-first search (DFS) that assigns an integer document-order position to every node in the tree, then sorts by those integers with sort_by.
New helpers:
Benchmark