Improve performance and reduce memory footprint during generation of complex trees #46
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.
Since we don't dynamically add instance variables to objects during runtime, we can leverage
__slots__
to remove theLeaf
,Stem
, andTree
classes'__dict__
attribute. This reduces memory requirements and increases performance on complex trees. Really doesn't make a difference forTree
, but I added anyway for consistency.It doesn't make a difference in simple trees, but in complex trees like the Weeping Willow that have boatloads of branches and leaves it makes a significant difference. My tests show it shaves off about 11 seconds from generating a Weeping Willow while making no statistically significant difference for Black Oak.
I have apparently lost the memory benchmarks from these runs (I did them a while back), but since this removes the
__dict__
attribute from each class it's not possible for the footprint to be worse.W/O
__slots__
Tree: Black Oak
Seed: 8684998
Avg of 4 runs:
create_branches: 3.99 seconds
create_leaf_mesh: 7.77 seconds
Tree generated in 11.75 seconds
Tree: Weeping Willow
Seed: 8684998
Avg of 4 runs:
create_branches: 76.21 seconds
create_leaf_mesh: 18.00 seconds
Tree generated in 93.86 seconds
====
__slots__
Tree: Black Oak
Seed: 8684998
Avg of 4 runs:
create_branches: 4.01 seconds
create_leaf_mesh: 8.00 seconds
Tree generated in 11.78 seconds
Tree: Weeping Willow
Seed: 8684998
Avg of 4 runs:
create_branches: 65.62 seconds
create_leaf_mesh: 16.95 seconds
Tree generated in 82.26 seconds