Skip to content

Commit ff203b2

Browse files
aries-3alamar
authored andcommitted
IGNITE-13856 Linear performance for DirectByteBufferStreamImplV2.writeString - Fixes #8577.
Signed-off-by: Ilya Kasnacheev <ilya.kasnacheev@gmail.com>
1 parent 6d02e32 commit ff203b2

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

modules/core/src/main/java/org/apache/ignite/internal/direct/stream/v2/DirectByteBufferStreamImplV2.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ public class DirectByteBufferStreamImplV2 implements DirectByteBufferStream {
301301
/** */
302302
protected boolean lastFinished;
303303

304+
/** byte-array representation of string */
305+
private byte[] curStrBackingArr;
306+
304307
/**
305308
* @param msgFactory Message factory.
306309
*/
@@ -584,7 +587,17 @@ public DirectByteBufferStreamImplV2(MessageFactory msgFactory) {
584587

585588
/** {@inheritDoc} */
586589
@Override public void writeString(String val) {
587-
writeByteArray(val != null ? val.getBytes() : null);
590+
if (val != null) {
591+
if (curStrBackingArr == null)
592+
curStrBackingArr = val.getBytes();
593+
594+
writeByteArray(curStrBackingArr);
595+
596+
if (lastFinished)
597+
curStrBackingArr = null;
598+
}
599+
else
600+
writeByteArray(null);
588601
}
589602

590603
/** {@inheritDoc} */

modules/core/src/test/java/org/apache/ignite/internal/direct/stream/v2/DirectByteBufferStreamImplV2ByteOrderSelfTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.ArrayList;
2323
import java.util.List;
2424
import java.util.Random;
25+
import org.apache.commons.lang3.StringUtils;
2526
import org.apache.ignite.IgniteException;
2627
import org.apache.ignite.internal.direct.stream.DirectByteBufferStream;
2728
import org.apache.ignite.internal.util.GridUnsafe;
@@ -339,6 +340,52 @@ public void testDoubleArrayInternal() {
339340
testWriteArrayInternalOverflow(arr, true, true, 3);
340341
}
341342

343+
/**
344+
* tests linear performance for writeString method
345+
* */
346+
@Test
347+
public void testNonSuperLinearPerformanceForWriteString() {
348+
int len = 400_000;
349+
350+
int trues = 0;
351+
for (int i = 0; i < 10; i++) {
352+
long t1 = getWriteStringExecutionTime(len);
353+
long t2 = getWriteStringExecutionTime(len * 10);
354+
long t3 = getWriteStringExecutionTime(len * 100);
355+
356+
if (t2 <= t1 * 10)
357+
trues++;
358+
359+
if (t3 <= t1 * 100)
360+
trues++;
361+
}
362+
363+
assertTrue(trues > 0);
364+
}
365+
366+
/**
367+
* execution time
368+
* @param len string length
369+
* @return writing time
370+
* */
371+
private long getWriteStringExecutionTime(int len) {
372+
String s = StringUtils.leftPad("1", len, "_");
373+
374+
buff.rewind();
375+
376+
DirectByteBufferStreamImplV2 stream = createStream(buff);
377+
378+
long d1 = System.currentTimeMillis();
379+
while (!stream.lastFinished()) {
380+
stream.writeString(s);
381+
382+
buff.rewind();
383+
}
384+
long d2 = System.currentTimeMillis();
385+
386+
return d2 - d1;
387+
}
388+
342389
/**
343390
* @param srcArr Source array.
344391
* @param writeBigEndian If {@code true}, then write in big-endian mode.

0 commit comments

Comments
 (0)