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

More efficient computation in MomentsMonoid #1049

Merged
merged 1 commit into from
Jan 12, 2022

Conversation

sritchie
Copy link
Collaborator

@sritchie sritchie commented Jan 11, 2022

I was staring at this code and noticed that we could make it faster by avoiding a few divisions. So here we go!

Also, I wasn't sure where to put this, but this - and the mutable equivalent - are going to be faster than the provided Aggregator in the case of a scan. Any interest?

  def plusDouble(a: Moments, b: Double): Moments = {
    val countCombined = a.m0D + 1
    val delta = b - a.mean
    val delta_n = delta / countCombined
    val delta_n2 = delta_n * delta_n
    val term1 = delta * delta_n * a.m0D
    val meanCombined = Moments.getCombinedMeanDouble(a.m0D, a.mean, 1.0, b)

    // writing them in reverse order like this will be useful in the mutable case, but doesn't matter here.
    val m4 = a.m4 + term1 * delta_n2 * (n * n - 3 * n + 3) + 6 * delta_n2 * a.m2 - 4 * delta_n * a.m3
    val m3 = a.m3 + term1 * delta_n * (n - 2) - 3 * delta_n * a.m2
    val m2 = a.m2 + term1

    new Moments(countCombined, meanCombined, m2, m3, m4)
  }

Copy link
Collaborator

@johnynek johnynek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great!

Thanks for taking the time to do this.

One follow up if you are interested:

replace math.pow(x, 2) with final def square(x: Double): Double = x * x which should be inlined, but the pow(x, 2) looks dynamic I fear to the JIT and it may not be able to inline it as one multiplication.

@johnynek johnynek merged commit d96093b into develop Jan 12, 2022
@johnynek johnynek deleted the sritchie/efficient_moments branch January 12, 2022 19:31
@johnynek
Copy link
Collaborator

As for the plusDouble I think a mutable Fold is the right way to do that since you can can allocate some mutable state to hold your variables class MutMoments { var count: Double , ... } then implement your plusMoments function there.

that way, you wouldn't allocate on each call, but you would wind up boxing the doubles (since these types are generic) so I don't know if that is going to kill the win (since boxing into a Moments is maybe almost as cheap).

But you could also add plusDouble to Moments and just add a law that m.plusDouble(x) == m.plus(Moments(x)).

That said, you would have to allocate a new Moments on each return anyway... Seems to me maybe the real win would be maybe making a MomentsState class that is mutable, and you could add a Moments into it, or a Double, and the only thing you can do with a MomentsState is convert to Moments (or from Moments).

so, something like:

final class MomentsState {
  // has private vars that keep track of the moments
  // we can then use this state in sumOption or in a mutable Fold.
  def +(m: Moments): this.type
  def +(d: Double): this.type
  
  def toMoments: Moments
  def resetFromMoments(m: Moments): this.type
}

object MomentsState {
  def newEmpty: MomentsState
}

@sritchie
Copy link
Collaborator Author

Done in #1050

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.

2 participants