AbstractTrees.jl
AbstractTrees.jl copied to clipboard
Prevent isempty(children(node)) from calling iterate (20% improvement in iteration speed)
During some profileing I noticed the isempty test on children were causing a call to next then iterate
This remedies that buy replacing any call to isempty(children(node)) with effectively ischildrenempty(children(node)) which for the most part is just checking for the interface definition of empty which is an empty typle.
The major culprit of the problem was isempty(children(TreeCursor))
Benchmark code:
using AbstractTrees
using BenchmarkTools
function randomArrayBinaryTreeNode(leaf_probabilty=0.5; max_depth=10, inds_range=1:2^10, inds_count_range=2:10)
if rand() <= leaf_probabilty || max_depth == 0
return rand(inds_range, rand(inds_count_range))
else
l = randomArrayBinaryTreeNode(leaf_probabilty; max_depth=max_depth - 1, inds_range, inds_count_range)
r = randomArrayBinaryTreeNode(leaf_probabilty; max_depth=max_depth - 1, inds_range, inds_count_range)
return [l,r]
end
end
# This makes sure that the random tree is not ranom in size
# random_tree = randomIndicesBinaryTreeNode(0.0, max_depth=10, inds_count_range=2:2)
random_tree = randomArrayBinaryTreeNode(0.0, max_depth=10, inds_count_range=2:2)
function test_function(tree, num=1)
n = 0
for m = 1:num
n = 0
for obj in PostOrderDFS(tree)
n += 1
end
end
return n
end
##
@benchmark treebreadth($random_tree)
##
@benchmark treeheight($random_tree)
##
@benchmark test_function($random_tree,2)
##
@benchmark count(_->true, Leaves($random_tree))
##
@benchmark count(_->true, PreOrderDFS($random_tree))
master benchmarks:
BenchmarkTools.Trial: 10000 samples with 5 evaluations.
Range (min … max): 6.730 μs … 44.979 μs ┊ GC (min … max): 0.00% … 0.00%
Time (median): 7.029 μs ┊ GC (median): 0.00%
Time (mean ± σ): 7.343 μs ± 1.078 μs ┊ GC (mean ± σ): 0.00% ± 0.00%
▅▇▃█▆▄▃▂▂▂ ▁ ▃▁ ▂▅▂ ▁▃ ▂
█████████████▇▅███████████▆▆▆▄▅▄▃▄▆▅▆▆▇▆▅▆▅▅▅▅▃▄▁▄▅▄▅▄▅▄▅▄ █
6.73 μs Histogram: log(frequency) by time 11.3 μs <
Memory estimate: 0 bytes, allocs estimate: 0.
BenchmarkTools.Trial: 10000 samples with 4 evaluations.
Range (min … max): 7.375 μs … 54.647 μs ┊ GC (min … max): 0.00% … 0.00%
Time (median): 7.550 μs ┊ GC (median): 0.00%
Time (mean ± σ): 8.150 μs ± 1.868 μs ┊ GC (mean ± σ): 0.00% ± 0.00%
█▅▄▃▃▂▃▂ ▅▂▅▅▁▃▃▁ ▂
██████████████████▇▆▄▅▅▅▅▅▅▅▄▄▅▃▅▅▃▆▅▄▄▅▅▄▃▃▃▄▅▃▄▄▄▄▁▃▄▅▄▃ █
7.38 μs Histogram: log(frequency) by time 14.4 μs <
Memory estimate: 0 bytes, allocs estimate: 0.
BenchmarkTools.Trial: 844 samples with 1 evaluation.
Range (min … max): 4.707 ms … 14.161 ms ┊ GC (min … max): 0.00% … 40.54%
Time (median): 5.379 ms ┊ GC (median): 0.00%
Time (mean ± σ): 5.916 ms ± 1.376 ms ┊ GC (mean ± σ): 1.48% ± 5.94%
▇█▅
███▆▅▆▅▄▆▄▆▃▃▄▄▄▄▄▄▄▄▄▃▄▃▃▃▃▃▃▃▃▂▃▃▃▂▃▂▂▂▂▂▂▁▁▂▁▁▂▂▂▂▁▂▁▁▂ ▃
4.71 ms Histogram: frequency by time 11.1 ms <
Memory estimate: 1.04 MiB, allocs estimate: 45882.
BenchmarkTools.Trial: 1592 samples with 1 evaluation.
Range (min … max): 2.596 ms … 9.547 ms ┊ GC (min … max): 0.00% … 59.26%
Time (median): 3.120 ms ┊ GC (median): 0.00%
Time (mean ± σ): 3.134 ms ± 587.732 μs ┊ GC (mean ± σ): 1.02% ± 4.86%
█ ▂
▅▃█▅▄▃▄▃▃▃▄▄▆█▅▄▄▄▃▃▃▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▁▂▂▁▂▁▂▂▁▁▁▂▁▁▁▁▁▂▂ ▃
2.6 ms Histogram: frequency by time 5.34 ms <
Memory estimate: 359.48 KiB, allocs estimate: 13793.
BenchmarkTools.Trial: 1319 samples with 1 evaluation.
Range (min … max): 2.835 ms … 17.063 ms ┊ GC (min … max): 0.00% … 0.00%
Time (median): 3.660 ms ┊ GC (median): 0.00%
Time (mean ± σ): 3.779 ms ± 973.802 μs ┊ GC (mean ± σ): 1.19% ± 5.42%
▁█ ▂
██▆▅▅▅▄█▆▆▆█▇▆▇█▆▅▄▃▃▃▃▃▃▂▃▂▂▂▂▂▂▂▂▁▂▂▂▂▁▂▂▂▁▁▁▁▁▁▁▂▂▂▂▁▁▁▂ ▃
2.84 ms Histogram: frequency by time 7.88 ms <
Memory estimate: 519.31 KiB, allocs estimate: 21976.
PR benchmarks
BenchmarkTools.Trial: 10000 samples with 5 evaluations.
Range (min … max): 6.710 μs … 30.438 μs ┊ GC (min … max): 0.00% … 0.00%
Time (median): 6.792 μs ┊ GC (median): 0.00%
Time (mean ± σ): 7.218 μs ± 1.152 μs ┊ GC (mean ± σ): 0.00% ± 0.00%
█▃▆▂▁ ▁ ▂ ▅ ▃ ▁
███████▇▇▇▆███████▇▇▆▆▄▄▅▆▇▇▆▆▅▅▅▅▃▄▄▅▄▃▄▃▃▃▃▁▁▃▄▁▄▄▅▃▃▃▁▃ █
6.71 μs Histogram: log(frequency) by time 13 μs <
Memory estimate: 0 bytes, allocs estimate: 0.
BenchmarkTools.Trial: 10000 samples with 4 evaluations.
Range (min … max): 7.138 μs … 33.505 μs ┊ GC (min … max): 0.00% … 0.00%
Time (median): 7.444 μs ┊ GC (median): 0.00%
Time (mean ± σ): 7.783 μs ± 1.066 μs ┊ GC (mean ± σ): 0.00% ± 0.00%
▅█▆▃▆▆▆▂▁ ▁▄▂ ▅▅▁ ▂▄▂ ▂
██████████▇▇█▆▆▇▇▄▄████████████▇▇▆▆▆▅▅▅▅▄▁▃▃▁▄▄▃▃▃▅▆▅▄▄▄▄▄ █
7.14 μs Histogram: log(frequency) by time 11.2 μs <
Memory estimate: 0 bytes, allocs estimate: 0.
BenchmarkTools.Trial: 996 samples with 1 evaluation.
Range (min … max): 4.614 ms … 10.041 ms ┊ GC (min … max): 0.00% … 36.65%
Time (median): 4.803 ms ┊ GC (median): 0.00%
Time (mean ± σ): 5.016 ms ± 626.262 μs ┊ GC (mean ± σ): 1.53% ± 6.02%
█▄▁▄
████▇▄▄▄▄▃▃▃▃▃▃▃▃▃▃▃▂▂▂▂▂▂▂▂▂▂▁▁▁▂▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂▂▂▂▂▂ ▃
4.61 ms Histogram: frequency by time 8.14 ms <
Memory estimate: 1.05 MiB, allocs estimate: 46092.
BenchmarkTools.Trial: 1825 samples with 1 evaluation.
Range (min … max): 2.536 ms … 6.763 ms ┊ GC (min … max): 0.00% … 52.58%
Time (median): 2.646 ms ┊ GC (median): 0.00%
Time (mean ± σ): 2.735 ms ± 389.647 μs ┊ GC (mean ± σ): 1.03% ± 5.00%
▄█▅▅▇▅▃▂▂▁▂▁▁▁▁▁ ▁ ▁ ▁
████████████████████▇███▇▇▇▇▆▅▆▅▅▆▄▆▅▄▄▄▁▁▁▁▁▁▄▁▁▄▁▁▁▅▄▁▁▁▄ █
2.54 ms Histogram: log(frequency) by time 4.09 ms <
Memory estimate: 359.95 KiB, allocs estimate: 13823.
BenchmarkTools.Trial: 1600 samples with 1 evaluation.
Range (min … max): 2.853 ms … 7.947 ms ┊ GC (min … max): 0.00% … 52.53%
Time (median): 2.979 ms ┊ GC (median): 0.00%
Time (mean ± σ): 3.122 ms ± 460.504 μs ┊ GC (mean ± σ): 1.24% ± 5.59%
█▂▅
███▄▄▃▃▃▃▃▃▃▃▃▃▂▂▂▂▂▂▂▁▂▂▁▂▁▁▂▂▂▁▂▁▁▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂ ▃
2.85 ms Histogram: frequency by time 6.27 ms <
Memory estimate: 519.78 KiB, allocs estimate: 22006.
Codecov Report
:x: Patch coverage is 90.90909% with 1 line in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 70.00%. Comparing base (781a01d) to head (459b597).
:warning: Report is 47 commits behind head on master.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/cursors.jl | 66.66% | 1 Missing :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## master #122 +/- ##
==========================================
- Coverage 70.04% 70.00% -0.05%
==========================================
Files 8 8
Lines 434 440 +6
==========================================
+ Hits 304 308 +4
- Misses 130 132 +2
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.