MurrellGroup / MolecularEvolution.jl

A Julia framework for developing phylogenetic models
MIT License
9 stars 4 forks source link

optimize combine! for DiscretePartition #8

Closed bicycle1885 closed 1 year ago

bicycle1885 commented 1 year ago

This is a minor optimization of the combine! method of DiscretePartition, which avoids allocations entirely when scaling the likelihoods and shaves ~30% of allocations and ~10% of runtime in the felsenstein_down! case you (@murrellb) sent me via email.

main:

BenchmarkTools.Trial: 4236 samples with 1 evaluation.
 Range (min … max):  1.098 ms …   3.726 ms  ┊ GC (min … max): 0.00% … 66.00%
 Time  (median):     1.122 ms               ┊ GC (median):    0.00%
 Time  (mean ± σ):   1.179 ms ± 347.161 μs  ┊ GC (mean ± σ):  4.27% ±  9.64%

  █▄▁▂                                                         
  ████▆▅▁▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▄▇█ █
  1.1 ms       Histogram: log(frequency) by time       3.5 ms <

 Memory estimate: 985.77 KiB, allocs estimate: 19971.

PR:

BenchmarkTools.Trial: 4833 samples with 1 evaluation.
 Range (min … max):  969.458 μs …   3.808 ms  ┊ GC (min … max): 0.00% … 69.61%
 Time  (median):     987.583 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):     1.034 ms ± 312.037 μs  ┊ GC (mean ± σ):  3.59% ±  8.47%

  █▄▂▁                                                          ▁
  ████▆▆▄▁▁▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▆▇ █
  969 μs        Histogram: log(frequency) by time       3.53 ms <

 Memory estimate: 673.89 KiB, allocs estimate: 14981.