Skip to content
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 1 commit into from
Feb 9, 2016

Conversation

Fryguy
Copy link
Contributor

@Fryguy Fryguy commented Feb 6, 2016

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
Copy link
Contributor Author

Fryguy commented Feb 6, 2016

This is marked as WIP as I'd like to test against some real world cases and possibly create some more tests.

@Fryguy Fryguy force-pushed the better_arrange_nodes branch 3 times, most recently from 3946c6c to 2e4bb66 Compare February 6, 2016 03:16
@kbrock kbrock changed the title [WIP] Improve performance of arrange_nodes. Improve performance of arrange_nodes. Feb 6, 2016
@kbrock
Copy link
Collaborator

kbrock commented Feb 6, 2016

@Fryguy looks good. let me know when you are ready for me to pull the trigger

@Fryguy Fryguy changed the title Improve performance of arrange_nodes. [WIP] Improve performance of arrange_nodes. Feb 8, 2016
@Fryguy Fryguy changed the title [WIP] Improve performance of arrange_nodes. Improve performance of arrange_nodes. Feb 8, 2016
@Fryguy
Copy link
Contributor Author

Fryguy commented Feb 8, 2016

@kbrock This should be good to go now.

@Fryguy
Copy link
Contributor Author

Fryguy commented Feb 8, 2016

Actually, I have a way to make the tests readable, so hold off until I push new specs.

@Fryguy Fryguy force-pushed the better_arrange_nodes branch 2 times, most recently from f027dff to fe785d7 Compare February 9, 2016 02:00
@Fryguy
Copy link
Contributor Author

Fryguy commented Feb 9, 2016

@kbrock Ok, I updated the specs to DRY them up a bit...

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.
@kbrock
Copy link
Collaborator

kbrock commented Feb 9, 2016

@Fryguy thanks

Ok, I updated the specs to DRY them up a bit...
@Fryguy

a bit? looks like you got them down a lot. much nicer to read. thanks

kbrock added a commit that referenced this pull request Feb 9, 2016
Improve performance of arrange_nodes.
@kbrock kbrock merged commit e686dd1 into stefankroes:master Feb 9, 2016
@Fryguy Fryguy deleted the better_arrange_nodes branch February 9, 2016 16:13
@coveralls
Copy link

coveralls commented Jan 28, 2018

Coverage Status

Coverage increased (+0.04%) to 98.113% when pulling fc1fe1e on Fryguy:better_arrange_nodes into d99e2a4 on stefankroes:master.

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.

3 participants