Partial recursion unrolling for PreOrderIter#278
Open
gregreich wants to merge 3 commits intoc0fec0de:mainfrom
Open
Partial recursion unrolling for PreOrderIter#278gregreich wants to merge 3 commits intoc0fec0de:mainfrom
gregreich wants to merge 3 commits intoc0fec0de:mainfrom
Conversation
avoid the setup costs for deque, while avoiding re-allocation of list memory (hence reversed)
Contributor
Author
|
any thoughts, maybe @c0fec0de ? |
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.
Occasionally, I have trees that are very degenerate in the sense that they feature an overwhelming majority of "only-child" parents; let's call those trees "sparse" in this context. In my application, the sparseness is inherent already in the physical domain that I model, and is thus costly to avoid in the code. Now, for large trees, important standard operations that build on recursion—such as the iterators—can blow up the Python recursion stack; at the same time, the latter can no longer simply be increased without tricks (see, for example, this discussion).
One way to avoid this is to "unroll" all those recursions that are degenerate in that they do not incorporate any branching. I have implemented that in
PreOrderIterexemplarily, and added some more unit tests that capture cases that haven't been covered before. This partial unrolling implementation is a compromise between loop and recursion: of course, the queue (or the stack) approach could be used even more consequently, eliminating all recursion whatsoever. However, this would particularly complicate the "maxlevel" filtering, having it to rely on (more expensive) calls to thedepthproperty. At the same time, the most obviously unnecessary recursion is effectively avoided, making the iterator significantly more efficient for sparse—yet large—trees.One final remark about this pull request: For now, I only updated
PreOrderIter, for one thing because I mostly use this iterator in my own work, and for another because it is heavily used internally. This latter use is what made me submit this PR: In my understanding, there is no way to "augment" anytree by a new iterator implementation through delegation or inheritance, which also replaces its internal use in standard properties, such asleaves,size, etc.