Skip to content

Commit a45ce43

Browse files
committed
Improve readPayload performance
1 parent 81d540d commit a45ce43

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,37 @@ public void readPayload(ByteBuffer dst)
14911491
}
14921492
}
14931493

1494+
/**
1495+
* Reads payload bytes of binary, extension, or raw string types.
1496+
*
1497+
* <p>
1498+
* This consumes bytes, copies them to the specified buffer
1499+
* This is usually faster than readPayload(ByteBuffer) by using unsafe.copyMemory
1500+
*
1501+
* @param dst the Message buffer into which the data is read
1502+
* @param off the offset in the Message buffer
1503+
* @param len the number of bytes to read
1504+
* @throws IOException when underlying input throws IOException
1505+
*/
1506+
public void readPayload(MessageBuffer dst, int off, int len)
1507+
throws IOException
1508+
{
1509+
while (true) {
1510+
int bufferRemaining = buffer.size() - position;
1511+
if (bufferRemaining >= len) {
1512+
dst.putMessageBuffer(off, buffer, position, len);
1513+
position += len;
1514+
return;
1515+
}
1516+
dst.putMessageBuffer(off, buffer, position, bufferRemaining);
1517+
off += bufferRemaining;
1518+
len -= bufferRemaining;
1519+
position += bufferRemaining;
1520+
1521+
nextBuffer();
1522+
}
1523+
}
1524+
14941525
/**
14951526
* Reads payload bytes of binary, extension, or raw string types.
14961527
*
@@ -1541,8 +1572,7 @@ public byte[] readPayload(int length)
15411572
public void readPayload(byte[] dst, int off, int len)
15421573
throws IOException
15431574
{
1544-
// TODO optimize
1545-
readPayload(ByteBuffer.wrap(dst, off, len));
1575+
readPayload(MessageBuffer.wrap(dst), off, len);
15461576
}
15471577

15481578
/**
@@ -1566,7 +1596,7 @@ public MessageBuffer readPayloadAsReference(int length)
15661596
return slice;
15671597
}
15681598
MessageBuffer dst = MessageBuffer.allocate(length);
1569-
readPayload(dst.sliceAsByteBuffer());
1599+
readPayload(dst, 0, length);
15701600
return dst;
15711601
}
15721602

msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBufferU.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public void copyTo(int index, MessageBuffer dst, int offset, int length)
248248
@Override
249249
public void putMessageBuffer(int index, MessageBuffer src, int srcOffset, int len)
250250
{
251-
putBytes(index, src.toByteArray(), srcOffset, len);
251+
putByteBuffer(index, src.sliceAsByteBuffer(srcOffset, len), len);
252252
}
253253

254254
@Override

msgpack-core/src/test/scala/org/msgpack/core/MessageUnpackerTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ class MessageUnpackerTest extends MessagePackSpec {
406406
}
407407
}
408408

409-
"be faster then msgpack-v6 skip" taggedAs ("cmp-skip") in {
409+
"be faster than msgpack-v6 skip" taggedAs ("cmp-skip") in {
410410

411411
trait Fixture {
412412
val unpacker: MessageUnpacker

0 commit comments

Comments
 (0)