Skip to content

Optimize Finder.get_descendant_ids/2 memory usage and speed#660

Merged
philss merged 2 commits intophilss:mainfrom
preciz:optimize-descendant-ids
Mar 16, 2026
Merged

Optimize Finder.get_descendant_ids/2 memory usage and speed#660
philss merged 2 commits intophilss:mainfrom
preciz:optimize-descendant-ids

Conversation

@preciz
Copy link
Contributor

@preciz preciz commented Mar 16, 2026

Optimizes the traversal of descendant nodes in Floki.Finder by rewriting get_descendant_ids/2 to use an optimal tail-recursive Depth-First Search (DFS) approach.

Also included is a new test in test/floki_test.exs specifically designed to guard against future regressions in descendant match traversal order.

  • Big Input: ~31.5% faster (47.22 ms vs 68.96 ms), memory reduced by 39.7% (9.82 MB vs 16.28 MB).
  • Medium Input: ~12.2% faster (7.97 ms vs 9.08 ms), memory reduced by 44.3% (2.65 MB vs 4.76 MB).
  • Small Input: ~15.1% faster (2.08 ms vs 2.45 ms), memory reduced by 33.4% (775 KB vs 1.14 MB).

preciz and others added 2 commits March 16, 2026 20:24
Replaced ++ and Enum.flat_map with an optimal tail-recursive DFS approach that preserves the correct document traversal order while massively reducing memory usage and increasing execution speed for descendant selectors.

Also added a test to explicitly guard against regressions in the descendant matching traversal order.
@philss philss merged commit 67c1125 into philss:main Mar 16, 2026
6 checks passed
@philss
Copy link
Owner

philss commented Mar 16, 2026

🚀 🚀 🚀 🚀

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