[ https://issues.apache.org/jira/browse/BEAM-5439?focusedWorklogId=158357&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-158357 ]
ASF GitHub Bot logged work on BEAM-5439: ---------------------------------------- Author: ASF GitHub Bot Created on: 24/Oct/18 22:30 Start Date: 24/Oct/18 22:30 Worklog Time Spent: 10m Work Description: lukecwik closed pull request #6812: [BEAM-5439] fixes performance issue in StringUtf8Coder URL: https://github.com/apache/beam/pull/6812 This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/StringUtf8Coder.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/StringUtf8Coder.java index 339b8f13cf1..57107813e7d 100644 --- a/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/StringUtf8Coder.java +++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/StringUtf8Coder.java @@ -18,8 +18,7 @@ package org.apache.beam.sdk.coders; import com.google.common.base.Utf8; -import java.io.DataInputStream; -import java.io.DataOutputStream; +import com.google.common.io.ByteStreams; import java.io.EOFException; import java.io.IOException; import java.io.InputStream; @@ -46,19 +45,19 @@ public static StringUtf8Coder of() { private static final StringUtf8Coder INSTANCE = new StringUtf8Coder(); private static final TypeDescriptor<String> TYPE_DESCRIPTOR = new TypeDescriptor<String>() {}; - private static void writeString(String value, DataOutputStream dos) throws IOException { + private static void writeString(String value, OutputStream dos) throws IOException { byte[] bytes = value.getBytes(StandardCharsets.UTF_8); VarInt.encode(bytes.length, dos); dos.write(bytes); } - private static String readString(DataInputStream dis) throws IOException { + private static String readString(InputStream dis) throws IOException { int len = VarInt.decodeInt(dis); if (len < 0) { throw new CoderException("Invalid encoded string length: " + len); } byte[] bytes = new byte[len]; - dis.readFully(bytes); + ByteStreams.readFully(dis, bytes); return new String(bytes, StandardCharsets.UTF_8); } @@ -82,7 +81,7 @@ public void encode(String value, OutputStream outStream, Context context) throws outStream.write(bytes); } } else { - writeString(value, new DataOutputStream(outStream)); + writeString(value, outStream); } } @@ -98,7 +97,7 @@ public String decode(InputStream inStream, Context context) throws IOException { return new String(bytes, StandardCharsets.UTF_8); } else { try { - return readString(new DataInputStream(inStream)); + return readString(inStream); } catch (EOFException | UTFDataFormatException exn) { // These exceptions correspond to decoding problems, so change // what kind of exception they're branded as. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 158357) Time Spent: 20m (was: 10m) > StringUtf8Coder is slower than expected > --------------------------------------- > > Key: BEAM-5439 > URL: https://issues.apache.org/jira/browse/BEAM-5439 > Project: Beam > Issue Type: Bug > Components: sdk-java-core > Affects Versions: 2.6.0 > Reporter: Julien Tournay > Assignee: Julien Tournay > Priority: Major > Labels: perfomance > Time Spent: 20m > Remaining Estimate: 0h > > While working on Scio's next version, I noticed that {{StringUtf8Coder}} is > slower than expected. > I wrote a small micro-benchmark using {{jmh}} that serialises a (scala) List > of a 1000 Strings using a custom {{Coder[List[_]]}}. While profiling it, I > noticed that a lot of time is spent in > {{java.io.DataInputStream.<init>(java.io.InputStream)}}. > Looking into the code for > {{StringUtf8Coder}}, the {{readString}} method is directly reading bytes. It > therefore does not seem that a {{DataInputStream}} is necessary. > I replaced {{StringUtf8Coder}} with a {{Coder[String]}} implementation (in > Scala), that is essentially the same as {{StringUtf8Coder}} but is not using > {{DataInputStream}}. > > {code:scala} > private final object ScioStringCoder extends AtomicCoder[String] { > import org.apache.beam.sdk.util.VarInt > import java.nio.charset.StandardCharsets > import org.apache.beam.sdk.values.TypeDescriptor > import com.google.common.base.Utf8 > def decode(dis: InputStream): String = { > val len = VarInt.decodeInt(dis) > if (len < 0) { > throw new CoderException("Invalid encoded string length: " + len) > } > val bytes = new Array[Byte](len) > dis.read(bytes) > return new String(bytes, StandardCharsets.UTF_8) > } > def encode(value: String, outStream: OutputStream): Unit = { > val bytes = value.getBytes(StandardCharsets.UTF_8) > VarInt.encode(bytes.length, outStream) > outStream.write(bytes) > } > override def verifyDeterministic() = () > override def consistentWithEquals() = true > private val TYPE_DESCRIPTOR = new TypeDescriptor[String] {} > override def getEncodedTypeDescriptor() = TYPE_DESCRIPTOR > override def getEncodedElementByteSize(value: String) = { > if (value == null) { > throw new CoderException("cannot encode a null String") > } > val size = Utf8.encodedLength(value) > VarInt.getLength(size) + size > } > } > {code} > > Using that {{Coder}} is about 27% faster than {{StringUtf8Coder}}. I've added > the jmh output in "Docs Text" > Is there any particular reason to use {{DataInputStream}} ? > Do you think we can remove that to make {{StringUtf8Coder}} more efficient ? > -- This message was sent by Atlassian JIRA (v7.6.3#76005)