beam icon indicating copy to clipboard operation
beam copied to clipboard

Don't re-encode byte[] values in SortValues transform

Open clairemcginty opened this issue 1 year ago • 8 comments

A small optimization for SortValues transform to avoid doubly roundtrip encoding values that are already byte[]s -- they can be passed directly to the Sorter.


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • [ ] Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • [ ] Update CHANGES.md with noteworthy changes.
  • [ ] If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels Python tests Java tests Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

clairemcginty avatar Apr 17 '24 20:04 clairemcginty

Assigning reviewers. If you would like to opt out of this review, comment assign to next reviewer:

R: @Abacn for label java.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

github-actions[bot] avatar Apr 17 '24 21:04 github-actions[bot]

Reminder, please take a look at this pr: @Abacn

github-actions[bot] avatar Apr 25 '24 12:04 github-actions[bot]

thanks, will take a look

Abacn avatar Apr 25 '24 15:04 Abacn

Reminder, please take a look at this pr: @Abacn

github-actions[bot] avatar May 03 '24 12:05 github-actions[bot]

Assigning new set of reviewers because Pr has gone too long without review. If you would like to opt out of this review, comment assign to next reviewer:

R: @damondouglas for label java.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

github-actions[bot] avatar May 07 '24 12:05 github-actions[bot]

Reminder, please take a look at this pr: @damondouglas

github-actions[bot] avatar May 15 '24 12:05 github-actions[bot]

Thank you for contributing this! The following three proposals go together and made more sense to provide in one comment, rather than separately. May we consider instead of the new custom class:

  1. two private static methods as shown below:
private static <T> T elementOf(Coder<T> coder, byte[] bytes) throws CoderException {
    if (coder instanceof ByteArrayCoder) {
      return (T) bytes;
    }
    return CoderUtils.decodeFromByteArray(coder, bytes);
  }

  private static <T> byte[] bytesOf(Coder<T> coder, T element) throws CoderException {
    if (element instanceof byte[]) {
      return (byte[]) element;
    }
    return CoderUtils.encodeToByteArray(coder, element);
  }
  1. Refactoring the SortValuesDoFn's processElement method with:
for (KV<SecondaryKeyT, ValueT> record : records) {
    sorter.add(KV.of(
        bytesOf(keyCoder, record.getKey()),
        bytesOf(valueCoder, record.getValue()))
    );
}
  1. Refactoring DecodingIterator's next with:
SecondaryKeyT secondaryKey = elementOf(keyCoder, next.getKey());
ValueT value = elementOf(valueCoder, next.getValue());
return KV.of(secondaryKey, value);

Thanks for the review @damondouglas ! Sorry for the late reply, I've been out of office, but will apply these suggestions today or tomorrow 👍

clairemcginty avatar May 22 '24 16:05 clairemcginty

Thanks for the review @damondouglas ! Sorry for the late reply, I've been out of office, but will apply these suggestions today or tomorrow 👍

Updated! your suggestions are much clearer -- my original approach constructed the custom functions up-front so that we wouldn't have to do the type-checking on every individual element, but I think the readability/clarity was just not worth it.

clairemcginty avatar May 22 '24 18:05 clairemcginty

Reminder, please take a look at this pr: @damondouglas

github-actions[bot] avatar May 30 '24 12:05 github-actions[bot]

@clairemcginty

Updated! your suggestions are much clearer -- my original approach constructed the custom functions up-front so that we wouldn't have to do the type-checking on every individual element, but I think the readability/clarity was just not worth it.

Thank you so much for listening! Apologies as I was taking a week off last week and will review now.

damondouglas avatar May 30 '24 16:05 damondouglas