-
Notifications
You must be signed in to change notification settings - Fork 69
Improve the performance of Map's algebras. #157
Conversation
We noticed an issue in cats-kernel where operations over Maps were incredibly slow. This commit ports those fixes over here, where our additive/multiplicative monoids experienced the same issues.
else if (n == 0) zero | ||
else throw new IllegalArgumentException("Illegal negative exponent to sumN: %s" format n) | ||
|
||
override def sum(as: TraversableOnce[Map[K, V]]): Map[K, V] = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't have trySum
here:
can we do the same override on AdditiveMonoid or here so that trySum
calls sum
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure.
minor comment about |
This mirrors a similar change in cats-kernel. The basic idea is that if someone goes to the trouble of writing an optimized .sum method we'd like for .trySum to use it as well.
if (xs.size <= ys.size) { | ||
timesMap(xs, ys)((x, y) => V.times(x, y)) | ||
xs.foldLeft(Map.empty[K, V]) { case (m, (k, x)) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if Map.empty
makes sense here or if we should use the mutable approach? Like, we could allocate an empty MMap
about the size of the smallest entry (an upperbound).
I think this will be faster, if we care, but we can come back to it later if you prefer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do this now, and then make that change with a benchmark later.
👍 |
We noticed an issue in cats-kernel where operations over Maps were
incredibly slow. This commit ports those fixes over here, where our
additive/multiplicative monoids experienced the same issues.
Review by @johnynek