wmwv / cosmo

Go cosmology library
MIT License
7 stars 2 forks source link

Explore using type conversion for special cases #23

Closed wmwv closed 6 years ago

wmwv commented 6 years ago

See if it's worth casting, e.g., a flat WCDM into FlatLCDM to reduce the redundancy between function calculations.

  1. [x] Is it possible?
  2. [x] Is it efficient?
  3. [x] Does it reduce code complexity?
wmwv commented 6 years ago

Clarification: "type assertion" is for getting the concrete object stored in an interface variable or test-casing an interface value to a concrete value.

In contrast, to call FlatLCDM from WCDM, one would create a FlatLCDM object and call the method on that.

wmwv commented 6 years ago

Takes ~30-60ns longer to fall through to the simpler case. LambdaCDM->Flat. This is about 5% of the runtime of the analytic cases. For the non-analytic cases, which take ~200µs to run, these extra ns are insignificant.

[Calm cosmo] benchstat issues23_before_benchmark.txt issues23_benchmark.txt
name                                    old time/op  new time/op  delta
FlatLCDMComovingDistance-4               799ns ± 6%   800ns ± 2%    ~     (p=0.937 n=5+5)
FlatLCDMNComovingDistance-4             8.21ms ± 4%  8.11ms ± 4%    ~     (p=0.548 n=5+5)
LambdaCDMComovingDistance-4              138µs ± 9%   137µs ±14%    ~     (p=0.548 n=5+5)
LambdaCDMNComovingDistance-4             1.34s ± 5%   1.36s ± 2%    ~     (p=0.548 n=5+5)
LambdaCDMComovingDistanceOM-4            538ns ± 5%   564ns ±14%    ~     (p=0.421 n=5+5)
LambdaCDMComovingDistanceFlat-4          795ns ± 4%   834ns ± 5%    ~     (p=0.095 n=5+5)
LambdaCDMComovingDistancePositiveOk0-4   133µs ± 4%   135µs ± 4%    ~     (p=0.548 n=5+5)
WACDMComovingDistance-4                  241µs ± 6%   241µs ± 4%    ~     (p=0.690 n=5+5)
WACDMNComovingDistance-4                 2.38s ± 6%   2.43s ± 4%    ~     (p=0.310 n=5+5)
WACDMComovingDistanceOM-4                523ns ± 5%   543ns ± 5%    ~     (p=0.135 n=5+5)
WACDMComovingDistanceFlat-4              786ns ± 4%   858ns ± 7%  +9.14%  (p=0.016 n=5+5)
WACDMComovingDistancePositiveOk0-4       207µs ± 3%   210µs ± 3%    ~     (p=0.310 n=5+5)
WCDMComovingDistance-4                   237µs ± 6%   238µs ± 4%    ~     (p=1.000 n=5+5)
WCDMNComovingDistance-4                  2.36s ± 3%   2.43s ±10%    ~     (p=0.421 n=5+5)
WCDMComovingDistanceOM-4                 528ns ± 5%   543ns ± 4%    ~     (p=0.143 n=5+5)
WCDMComovingDistanceFlat-4               791ns ± 3%   830ns ± 8%  +4.93%  (p=0.032 n=5+5)
WCDMComovingDistancePositiveOk0-4        138µs ± 4%   136µs ± 4%    ~     (p=0.421 n=5+5)
wmwv commented 6 years ago

Performance penalty acceptable. And use case seems reasonble. If you know that a cosmology is FlatLCDM, then directly create a struct of that type instead of a WCDM. If you want to generalize and call all things a WCDM, e.g., you can still do so with a very small performance penalty.