-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Optimize the Count CombineFn #88
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
|
|
@@ -16,10 +16,23 @@ | |
|
|
||
| package com.google.cloud.dataflow.sdk.transforms; | ||
|
|
||
| import com.google.cloud.dataflow.sdk.coders.Coder; | ||
| import com.google.cloud.dataflow.sdk.coders.CoderException; | ||
| import com.google.cloud.dataflow.sdk.coders.CoderRegistry; | ||
| import com.google.cloud.dataflow.sdk.coders.CustomCoder; | ||
| import com.google.cloud.dataflow.sdk.transforms.Combine.CombineFn; | ||
| import com.google.cloud.dataflow.sdk.util.VarInt; | ||
| import com.google.cloud.dataflow.sdk.values.KV; | ||
| import com.google.cloud.dataflow.sdk.values.PCollection; | ||
|
|
||
| import java.io.EOFException; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.io.UTFDataFormatException; | ||
| import java.util.Iterator; | ||
|
|
||
|
|
||
| /** | ||
| * {@code PTransorm}s to count the elements in a {@link PCollection}. | ||
| * | ||
|
|
@@ -106,30 +119,74 @@ public void processElement(ProcessContext c) { | |
| /** | ||
| * A {@link CombineFn} that counts elements. | ||
| */ | ||
| private static class CountFn<T> extends CombineFn<T, Long, Long> { | ||
| private static class CountFn<T> extends CombineFn<T, long[], Long> { | ||
| // Note that the long[] accumulator always has size 1, used as | ||
| // a box for a mutable long. | ||
|
|
||
| @Override | ||
| public Long createAccumulator() { | ||
| return 0L; | ||
| public long[] createAccumulator() { | ||
| return new long[] {0}; | ||
| } | ||
|
|
||
| @Override | ||
| public Long addInput(Long accumulator, T input) { | ||
| return accumulator + 1; | ||
| public long[] addInput(long[] accumulator, T input) { | ||
| accumulator[0] += 1; | ||
| return accumulator; | ||
| } | ||
|
|
||
| @Override | ||
| public Long mergeAccumulators(Iterable<Long> accumulators) { | ||
| long result = 0L; | ||
| for (Long accum : accumulators) { | ||
| result += accum; | ||
| public long[] mergeAccumulators(Iterable<long[]> accumulators) { | ||
| Iterator<long[]> iter = accumulators.iterator(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Forgot to import Iterator.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops. Done. |
||
| if (!iter.hasNext()) { | ||
| return createAccumulator(); | ||
| } | ||
| return result; | ||
| long[] running = iter.next(); | ||
| while (iter.hasNext()) { | ||
| running[0] += iter.next()[0]; | ||
| } | ||
| return running; | ||
| } | ||
|
|
||
| @Override | ||
| public Long extractOutput(Long accumulator) { | ||
| return accumulator; | ||
| public Long extractOutput(long[] accumulator) { | ||
| return accumulator[0]; | ||
| } | ||
|
|
||
| @Override | ||
| public Coder<long[]> getAccumulatorCoder(CoderRegistry registry, | ||
| Coder<T> inputCoder) { | ||
| return new CustomCoder<long[]>() { | ||
| @Override | ||
| public void encode(long[] value, OutputStream outStream, Context context) | ||
| throws IOException { | ||
| VarInt.encode(value[0], outStream); | ||
| } | ||
|
|
||
| @Override | ||
| public long[] decode(InputStream inStream, Context context) | ||
| throws IOException, CoderException { | ||
| try { | ||
| return new long[] {VarInt.decodeLong(inStream)}; | ||
| } catch (EOFException | UTFDataFormatException exn) { | ||
| throw new CoderException(exn); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isRegisterByteSizeObserverCheap(long[] value, Context context) { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| protected long getEncodedElementByteSize(long[] value, Context context) { | ||
| return VarInt.getLength(value[0]); | ||
| } | ||
|
|
||
| @Override | ||
| public String getEncodingId() { | ||
| return "VarLongSingletonArray"; | ||
| } | ||
| }; | ||
| } | ||
| } | ||
| } | ||
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.
Worth a comment explaining that the accumulator is always size 1, and is used as a mutable box for the long.
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.
Updated.