-
Notifications
You must be signed in to change notification settings - Fork 347
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
Implement an appendMonoid Aggregator factory which yields aggregators… #501
Merged
avibryant
merged 5 commits into
twitter:develop
from
erikerlandson:feature/append_monoid
Dec 4, 2015
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eaaa054
Implement an appendMonoid Aggregator factory which yields aggregators…
erikerlandson fd9b866
Add updateSemigroup factory function
erikerlandson c9f2e06
Optimize the AlgebirdRDD aggregateOption method to take advantage of …
erikerlandson ba9df9f
unit testing for semigroup variants
erikerlandson f0dc4f2
rename sg -> b to represent proper type
erikerlandson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
algebird-test/src/test/scala/com/twitter/algebird/AppendAggregatorTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.twitter.algebird | ||
|
||
import org.scalatest._ | ||
|
||
class AppendAggregatorTest extends WordSpec with Matchers { | ||
val data = Vector.fill(100) { scala.util.Random.nextInt(100) } | ||
val mpty = Vector.empty[Int] | ||
|
||
// test the methods that appendMonoid method defines or overrides | ||
def testMethods[E, M, P]( | ||
agg1: MonoidAggregator[E, M, P], | ||
agg2: MonoidAggregator[E, M, P], | ||
data: Seq[E], | ||
empty: Seq[E]) { | ||
|
||
val n = data.length | ||
val (half1, half2) = data.splitAt(n / 2) | ||
val lhs = agg1.appendAll(half1) | ||
|
||
data.foreach { e => | ||
agg1.prepare(e) should be(agg2.prepare(e)) | ||
} | ||
|
||
agg1.present(lhs) should be(agg2.present(lhs)) | ||
|
||
agg1(data) should be (agg2(data)) | ||
agg1(empty) should be (agg2(empty)) | ||
|
||
agg1.applyOption(data) should be(agg2.applyOption(data)) | ||
agg1.applyOption(empty) should be(agg2.applyOption(empty)) | ||
|
||
half2.foreach { e => | ||
agg1.append(lhs, e) should be(agg2.append(lhs, e)) | ||
} | ||
|
||
agg1.appendAll(lhs, half2) should be(agg2.appendAll(lhs, half2)) | ||
|
||
agg2.appendAll(data) should be(agg2.appendAll(data)) | ||
} | ||
|
||
"appendMonoid" should { | ||
"be equivalent to integer monoid aggregator" in { | ||
val agg1 = Aggregator.fromMonoid[Int] | ||
val agg2 = Aggregator.appendMonoid((m: Int, e: Int) => m + e) | ||
testMethods(agg1, agg2, data, mpty) | ||
} | ||
|
||
"be equivalent to set monoid aggregator" in { | ||
object setMonoid extends Monoid[Set[Int]] { | ||
val zero = Set.empty[Int] | ||
def plus(m1: Set[Int], m2: Set[Int]) = m1 ++ m2 | ||
} | ||
|
||
val agg1 = Aggregator.prepareMonoid((e: Int) => Set(e))(setMonoid) | ||
val agg2 = Aggregator.appendMonoid((m: Set[Int], e: Int) => m + e)(setMonoid) | ||
|
||
testMethods(agg1, agg2, data, mpty) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
so,
appnd
must satisfy the law:Otherwise
prepare
doesn't mean what we expect. If you agree, we should add this law to the comment.This is addressing one performance issue, which might be a very important one, but also the case of dealing with a bulk set of items (Monoid.sum/Semigroup.sumOption) is also an important optimization.
Since we expose the monoid/semigroup of an Aggregator, scalding and spark use that to get the sumOption speedup. For those cases, they will not get the benefit of an optimized
append
and in fact will callprepare
which may be even slower. So, I think the comment "faster aggregation" is more complex and we need to point out that this could be slower than the standard approach depending on the Monoid and appnd function.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 may be missing some angle here, but that seems like an opportunity for Scalding/Spark to take better advantage of
traversableOnce.aggregate(...)
over individual partitions.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.
@erikerlandson I think the question is whether the
append()
optimization is more effective than whatever optimization is insumOption
. They're both about eliminating unnecessary intermediate values of the type we have aMonoid
on, but in different ways - it's sorta a question of, for eachreduce(left: T, right: T)
, whether you need the left side to actually be a T (whichsumOption
eliminates) or the right side to actually be aT
(whichappend
eliminates). You could imagine anappendAll
which requires neither to be, but that feels even more invasive for theAggregator
to eliminate vs. theAppendMonoid
like you proposed.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.
To put it another way, if you ultimately think of this like a
def foldLeft[B](z: B)(f: (B, A) => B): B
on aSeq[A]
, and you have someMonoid[T]
, the question is do we haveB == T
(a constraint thatsumOption
gets rid of) or do we haveA == T
(a constraint thatappend
gets rid of), or both, or neither, and what's the most efficient space to do this aggregation in.