Skip to content

Commit a9a6367

Browse files
committed
add comments
1 parent 6fde6bc commit a9a6367

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
import java.lang.invoke.MethodType;
99
import java.lang.reflect.Constructor;
1010
import java.lang.reflect.Field;
11-
import java.lang.reflect.InvocationTargetException;
1211
import java.nio.ByteBuffer;
13-
import java.nio.ByteOrder;
14-
import java.util.NoSuchElementException;
1512
import java.util.concurrent.atomic.AtomicInteger;
1613

1714
import static sun.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET;
@@ -21,6 +18,10 @@
2118
* MessageBuffer class is an abstraction of memory for reading/writing message pac data.
2219
* This MessageBuffers ensures integers (31-bit singed) are written to the memory in big-endian order.
2320
*
21+
* This class is optimized for fast memory access, so many methods are
22+
* implemented without using any interface method that produces invokeinterface call in JVM, which is generally
23+
* 30% slower than invokevirtual, because invokeinterface needs to look up a function from the function table.
24+
*
2425
*/
2526
public class MessageBuffer {
2627

@@ -69,8 +70,6 @@ public class MessageBuffer {
6970
unsafe.freeMemory(a);
7071
}
7172

72-
// We need to use reflection to create MessageBuffer instances in order to prevent TypeProfile generation for getInt method. TypeProfile will be
73-
// generated to resolve one of the method references when two or more classes overrides the method.
7473
String bufferClsName = isLittleEndian ? "org.msgpack.core.MessageBuffer" : "org.msgpack.core.MessageBufferBE";
7574
msgBufferClass = Class.forName(bufferClsName);
7675
}
@@ -79,6 +78,10 @@ public class MessageBuffer {
7978
}
8079
}
8180

81+
82+
/**
83+
* MessageBuffer class to use. If this machine is big-endian, it uses MessageBufferBE, which overrides some methods in this class that translate endians. If not, uses MessageBuffer.
84+
*/
8285
private final static Class<?> msgBufferClass;
8386

8487
/**
@@ -138,6 +141,8 @@ public static MessageBuffer wrap(ByteBuffer bb) {
138141
*/
139142
private static MessageBuffer newMessageBuffer(ByteBuffer bb) {
140143
try {
144+
// We need to use reflection to create MessageBuffer instances in order to prevent TypeProfile generation for getInt method. TypeProfile will be
145+
// generated to resolve one of the method references when two or more classes overrides the method.
141146
Constructor<?> constructor = msgBufferClass.getDeclaredConstructor(ByteBuffer.class);
142147
return (MessageBuffer) constructor.newInstance(bb);
143148
}
@@ -315,6 +320,7 @@ public void putFloat(int index, float v) {
315320
}
316321

317322
public void putLong(int index, long l) {
323+
// Reversing the endian
318324
l = (l & 0x00ff00ff00ff00ffL) << 8 | (l>>> 8) & 0x00ff00ff00ff00ffL;
319325
l = (l << 48) |
320326
((l & 0xffff0000L) << 16) |
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package org.msgpack.core;
22

3-
import java.io.IOException;
4-
import java.math.BigInteger;
5-
import java.nio.ByteBuffer;
63

74
/**
8-
* Created on 2014/05/04.
5+
*
96
*/
107
public interface MessageBufferOutput {
118

129

1310

11+
1412
}

0 commit comments

Comments
 (0)