-
Notifications
You must be signed in to change notification settings - Fork 459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve performance of arrange_nodes. #261
Merged
Merged
Conversation
This file contains 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
This is marked as WIP as I'd like to test against some real world cases and possibly create some more tests. |
Fryguy
force-pushed
the
better_arrange_nodes
branch
3 times, most recently
from
February 6, 2016 03:16
3946c6c
to
2e4bb66
Compare
kbrock
changed the title
[WIP] Improve performance of arrange_nodes.
Improve performance of arrange_nodes.
Feb 6, 2016
@Fryguy looks good. let me know when you are ready for me to pull the trigger |
Fryguy
changed the title
Improve performance of arrange_nodes.
[WIP] Improve performance of arrange_nodes.
Feb 8, 2016
Fryguy
force-pushed
the
better_arrange_nodes
branch
from
February 8, 2016 22:50
2e4bb66
to
fc1fe1e
Compare
Fryguy
changed the title
[WIP] Improve performance of arrange_nodes.
Improve performance of arrange_nodes.
Feb 8, 2016
@kbrock This should be good to go now. |
Actually, I have a way to make the tests readable, so hold off until I push new specs. |
Fryguy
force-pushed
the
better_arrange_nodes
branch
2 times, most recently
from
February 9, 2016 02:00
f027dff
to
fe785d7
Compare
@kbrock Ok, I updated the specs to DRY them up a bit... |
Fryguy
force-pushed
the
better_arrange_nodes
branch
from
February 9, 2016 02:24
fe785d7
to
757273e
Compare
The old code is essentially an insertion algorithm that walks the tree from the root for each node and inserts itself in the right spot. Inserting a node is roughly O(depth); best case O(1), but worst case O(n). Thus, the tree building is roughly O(n * depth); best case O(n), but worst case O(n^2). The new code maintains an index of a node's children by node id. Each nodes is placed into the children of the parent id. Thus, inserting a node is an O(1) lookup, making the tree building O(n). The operation only has to look at the parent, and does not have to go through all of the ancestry ids, because eventually the parent node will go through the same insertion process itself. Note that the old algorithm used a pre-sorting of nodes in order to more efficiently arrange them. The new algorithm doesn't care about order, and thus that sorting can probably be removed in future PRs.
Fryguy
force-pushed
the
better_arrange_nodes
branch
from
February 9, 2016 05:00
757273e
to
8a4fb00
Compare
kbrock
added a commit
that referenced
this pull request
Feb 9, 2016
Improve performance of arrange_nodes.
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.
The old code is essentially an insertion algorithm that walks the tree
from the root for each node and inserts itself in the right spot.
Inserting a node is roughly O(depth); best case O(1), but worst case
O(n). Thus, the tree building is roughly O(n * depth); best case O(n),
but worst case O(n^2).
The new code maintains an index of a node's children by node id. Each
nodes is placed into the children of the parent id. Thus, inserting a
node is an O(1) lookup, making the tree building O(n).
The operation only has to look at the parent, and does not have to go
through all of the ancestry ids, because eventually the parent node
will go through the same insertion process itself.
Note that the old algorithm used a pre-sorting of nodes in order to
more efficiently arrange them. The new algorithm doesn't care about
order, and thus that sorting can probably be removed in future PRs.