Optimize Finder.get_siblings/2 memory usage and speed#663
Merged
philss merged 1 commit intophilss:mainfrom Mar 17, 2026
Merged
Conversation
Replaced a multi-pass pipeline (`Enum.reverse |> Enum.drop_while |> tl |> Enum.filter`) with a single-pass tail-recursive approach. Since `children_nodes_ids` are stored in reverse document order, iterating over them and prepending to an accumulator naturally reverses the relevant subsequent siblings back into forward document order without needing to iterate the preceding siblings or create intermediate lists. Benchmark results when calling `get_siblings` on all nodes of a large document: ``` Name ips average deviation median 99th % new_get_siblings 89.90 11.12 ms ±14.39% 10.97 ms 15.05 ms old_get_siblings 41.31 24.21 ms ±10.01% 24.15 ms 31.30 ms Comparison: new_get_siblings 89.90 old_get_siblings 41.31 - 2.18x slower +13.09 ms Memory usage statistics: Name Memory usage new_get_siblings 2.97 MB old_get_siblings 10.31 MB - 3.47x memory usage +7.34 MB ```
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.
Replaced a multi-pass pipeline (
Enum.reverse |> Enum.drop_while |> tl |> Enum.filter) with a single-pass tail-recursive approach.Since
children_nodes_idsare stored in reverse document order, iterating over them and prepending to an accumulator naturally reverses the relevant subsequent siblings back into forward document order without needing to iterate the preceding siblings or create intermediate lists.Benchmark results: