Skip to content

Commit 0d8b7b7

Browse files
authored
Merge pull request #436 from miniway/develop
Improve readPayload performance
2 parents 53a24d7 + a45ce43 commit 0d8b7b7

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
@@ -1516,6 +1516,37 @@ public void readPayload(ByteBuffer dst)
15161516
}
15171517
}
15181518

1519+
/**
1520+
* Reads payload bytes of binary, extension, or raw string types.
1521+
*
1522+
* <p>
1523+
* This consumes bytes, copies them to the specified buffer
1524+
* This is usually faster than readPayload(ByteBuffer) by using unsafe.copyMemory
1525+
*
1526+
* @param dst the Message buffer into which the data is read
1527+
* @param off the offset in the Message buffer
1528+
* @param len the number of bytes to read
1529+
* @throws IOException when underlying input throws IOException
1530+
*/
1531+
public void readPayload(MessageBuffer dst, int off, int len)
1532+
throws IOException
1533+
{
1534+
while (true) {
1535+
int bufferRemaining = buffer.size() - position;
1536+
if (bufferRemaining >= len) {
1537+
dst.putMessageBuffer(off, buffer, position, len);
1538+
position += len;
1539+
return;
1540+
}
1541+
dst.putMessageBuffer(off, buffer, position, bufferRemaining);
1542+
off += bufferRemaining;
1543+
len -= bufferRemaining;
1544+
position += bufferRemaining;
1545+
1546+
nextBuffer();
1547+
}
1548+
}
1549+
15191550
/**
15201551
* Reads payload bytes of binary, extension, or raw string types.
15211552
*
@@ -1566,8 +1597,7 @@ public byte[] readPayload(int length)
15661597
public void readPayload(byte[] dst, int off, int len)
15671598
throws IOException
15681599
{
1569-
// TODO optimize
1570-
readPayload(ByteBuffer.wrap(dst, off, len));
1600+
readPayload(MessageBuffer.wrap(dst), off, len);
15711601
}
15721602

15731603
/**
@@ -1591,7 +1621,7 @@ public MessageBuffer readPayloadAsReference(int length)
15911621
return slice;
15921622
}
15931623
MessageBuffer dst = MessageBuffer.allocate(length);
1594-
readPayload(dst.sliceAsByteBuffer());
1624+
readPayload(dst, 0, length);
15951625
return dst;
15961626
}
15971627

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)