Skip to content

Commit e047308

Browse files
committed
twice faster than DataOutputStream(BAOS); can be used as StringBuilder
1 parent 71c3a9e commit e047308

File tree

2 files changed

+63
-5
lines changed
  • src
    • main/java/com/trivago/fastutilconcurrentwrapper/io
    • test/java/com/trivago/fastutilconcurrentwrapper/io

2 files changed

+63
-5
lines changed

src/main/java/com/trivago/fastutilconcurrentwrapper/io/BAOS.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,15 +337,15 @@ public void writeObject(Object obj) throws IOException {
337337
}
338338

339339
@Override
340-
public Appendable append (CharSequence csq) {
340+
public BAOS append (CharSequence csq) {
341341
append(csq, 0, csq.length());
342342
return this;
343343
}
344344

345345
/// @see #write(byte[], int, int)
346346
/// @see #writeBytes(byte[])
347347
@Override
348-
public Appendable append (CharSequence csq, int start, int end) {
348+
public BAOS append (CharSequence csq, int start, int end) {
349349
final int len = end - start;
350350
Objects.checkFromIndexSize(start, len, csq.length());
351351
final int len2 = len << 1;
@@ -361,7 +361,7 @@ public Appendable append (CharSequence csq, int start, int end) {
361361

362362
/// @see #writeChar(int)
363363
@Override
364-
public Appendable append (char c) {
364+
public BAOS append (char c) {
365365
grow(2);
366366
JBytes.DirectByteArrayAccess.setChar(buf, position, c);
367367
position += 2;

src/test/java/com/trivago/fastutilconcurrentwrapper/io/BAOSTest.java

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,11 +1044,54 @@ void _benchmark () {
10441044
});
10451045
}
10461046

1047+
@Test
1048+
void _benchmarkJDK () {
1049+
IntStream.range(0, 2).forEach(__->{
1050+
long t = System.nanoTime();
1051+
for (int loop = 1; loop < 500_000; loop++){
1052+
val baos = new BAOS();
1053+
var os = new DataOutputStream(baos);
1054+
val is = new BAIS(baos.array());
1055+
1056+
try {
1057+
for (int i = 0; i < 133; i++){
1058+
os.write(0xAB);
1059+
os.writeShort(0xCD_EF);
1060+
os.writeInt(0x1234_5678);
1061+
os.writeLong(0x91929394_95969798L);
1062+
os.writeDouble(Math.PI);
1063+
os.writeLong(uuid.getMostSignificantBits());
1064+
os.writeLong(uuid.getLeastSignificantBits());
1065+
os.writeBytes("Answer42!");
1066+
}
1067+
} catch (IOException e){
1068+
throw new AssertionError(e);
1069+
}
1070+
assertEquals(6384, os.size());
1071+
1072+
is.array(baos.array(), 0, os.size());
1073+
1074+
for (int i = 0; i < 133; i++){
1075+
assertEquals(0xAB_CD_EF, is.readMedium());
1076+
assertEquals(0x1234_5678, is.readInt());
1077+
assertEquals(0x91929394_95969798L, is.readLong());
1078+
assertEquals(Math.PI, is.readDouble());
1079+
assertEquals(uuid, is.readUUID());
1080+
assertEquals("Answer42!", is.readLatin1String(9));
1081+
}
1082+
assertEquals(9222, is.array().length);
1083+
assertEquals(6384, is.length());
1084+
assertEquals(6384, is.limit());
1085+
assertEquals(6384, is.position());
1086+
assertEquals(6384, is.readerIndex());
1087+
}//f
1088+
System.out.println(benchToStr(t, System.nanoTime(), 500_000));
1089+
});
1090+
}
1091+
10471092
public static String benchToStr (long start, long end, long totalOperations) {
10481093
assert start <= end : "start ≤ end, but " + start + " > " + end;
1049-
10501094
long elapsed = end - start;
1051-
10521095
return String.format(Locale.ENGLISH, "%.3f, op/s=%.2f", elapsed/1000/1000.0, totalOperations * 1_000_000_000.0 / elapsed);
10531096
}
10541097

@@ -1126,4 +1169,19 @@ void _appendable () {
11261169
assertEquals(" test !", is.readUTF16String(7));
11271170
assertEquals(s3, is.readUTF16String(s3.length()));
11281171
}
1172+
1173+
@Test
1174+
void _baos_as_StringBuilder () {
1175+
val w = new BAOS(5000*2);
1176+
val b = new StringBuilder(5000);
1177+
1178+
for (int i = 0; i<=255; i++){
1179+
w.append(Integer.toString(i)).append(Integer.toHexString(i)).append('\n');
1180+
b.append(i).append(Integer.toHexString(i)).append('\n');
1181+
}
1182+
1183+
assertEquals(w.toString(UTF_16BE), b.toString());
1184+
assertArrayEquals(w.toByteArray(), b.toString().getBytes(UTF_16BE));
1185+
assertEquals(w.length()/2, b.length());
1186+
}
11291187
}

0 commit comments

Comments
 (0)