This is an automated email from the ASF dual-hosted git repository. ilyak pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/ignite.git
The following commit(s) were added to refs/heads/master by this push: new ff203b2 IGNITE-13856 Linear performance for DirectByteBufferStreamImplV2.writeString - Fixes #8577. ff203b2 is described below commit ff203b2ee8e8a05f0783b440f2da8f6fbc4beb8d Author: Ilya Kazakov <kazakov.i...@gmail.com> AuthorDate: Mon Dec 28 13:06:18 2020 +0300 IGNITE-13856 Linear performance for DirectByteBufferStreamImplV2.writeString - Fixes #8577. Signed-off-by: Ilya Kasnacheev <ilya.kasnach...@gmail.com> --- .../stream/v2/DirectByteBufferStreamImplV2.java | 15 ++++++- ...ectByteBufferStreamImplV2ByteOrderSelfTest.java | 47 ++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/modules/core/src/main/java/org/apache/ignite/internal/direct/stream/v2/DirectByteBufferStreamImplV2.java b/modules/core/src/main/java/org/apache/ignite/internal/direct/stream/v2/DirectByteBufferStreamImplV2.java index ef6e01f..50380ea 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/direct/stream/v2/DirectByteBufferStreamImplV2.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/direct/stream/v2/DirectByteBufferStreamImplV2.java @@ -301,6 +301,9 @@ public class DirectByteBufferStreamImplV2 implements DirectByteBufferStream { /** */ protected boolean lastFinished; + /** byte-array representation of string */ + private byte[] curStrBackingArr; + /** * @param msgFactory Message factory. */ @@ -584,7 +587,17 @@ public class DirectByteBufferStreamImplV2 implements DirectByteBufferStream { /** {@inheritDoc} */ @Override public void writeString(String val) { - writeByteArray(val != null ? val.getBytes() : null); + if (val != null) { + if (curStrBackingArr == null) + curStrBackingArr = val.getBytes(); + + writeByteArray(curStrBackingArr); + + if (lastFinished) + curStrBackingArr = null; + } + else + writeByteArray(null); } /** {@inheritDoc} */ diff --git a/modules/core/src/test/java/org/apache/ignite/internal/direct/stream/v2/DirectByteBufferStreamImplV2ByteOrderSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/direct/stream/v2/DirectByteBufferStreamImplV2ByteOrderSelfTest.java index 795fc63..a2c6daa 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/direct/stream/v2/DirectByteBufferStreamImplV2ByteOrderSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/direct/stream/v2/DirectByteBufferStreamImplV2ByteOrderSelfTest.java @@ -22,6 +22,7 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; import java.util.Random; +import org.apache.commons.lang3.StringUtils; import org.apache.ignite.IgniteException; import org.apache.ignite.internal.direct.stream.DirectByteBufferStream; import org.apache.ignite.internal.util.GridUnsafe; @@ -340,6 +341,52 @@ public class DirectByteBufferStreamImplV2ByteOrderSelfTest { } /** + * tests linear performance for writeString method + * */ + @Test + public void testNonSuperLinearPerformanceForWriteString() { + int len = 400_000; + + int trues = 0; + for (int i = 0; i < 10; i++) { + long t1 = getWriteStringExecutionTime(len); + long t2 = getWriteStringExecutionTime(len * 10); + long t3 = getWriteStringExecutionTime(len * 100); + + if (t2 <= t1 * 10) + trues++; + + if (t3 <= t1 * 100) + trues++; + } + + assertTrue(trues > 0); + } + + /** + * execution time + * @param len string length + * @return writing time + * */ + private long getWriteStringExecutionTime(int len) { + String s = StringUtils.leftPad("1", len, "_"); + + buff.rewind(); + + DirectByteBufferStreamImplV2 stream = createStream(buff); + + long d1 = System.currentTimeMillis(); + while (!stream.lastFinished()) { + stream.writeString(s); + + buff.rewind(); + } + long d2 = System.currentTimeMillis(); + + return d2 - d1; + } + + /** * @param srcArr Source array. * @param writeBigEndian If {@code true}, then write in big-endian mode. * @param readBigEndian If {@code true}, then read in big-endian mode.