比Java NIO的ByteBuffer性能更快的Chronicle-Bytes!


如图,红色的Bytes比蓝色的ByteBuffer在序列化等方面性能要高得多!

使用Bytes设置具有远程过程调用(RPC)和API的整个框架很容易,它支持写入和重放事件。这是一个简短的例子,其中MyPerson是一个实现接口的POJO BytesMarshable。我们不必实现任何方法,BytesMarshallable因为它带有默认实现。

public final class MyPerson implements BytesMarshallable {

    private String name;
    private byte type;
    private double balance;

    public MyPerson(){}

  // Getters and setters not shown for brevity

}

interface MyApi {
    @MethodId(0x81L)
    void myPerson(MyPerson byteable);
}

static void serialize() {
    MyPerson myPerson = new MyPerson();
    myPerson.setName(
"John");
    yPerson.setType((byte) 7);
    myPerson.setBalance(123.5);

    HexDumpBytes bytes = new HexDumpBytes();
    MyApi myApi = bytes.bytesMethodWriter(MyApi.class);

    myApi.myPerson(myPerson);

    System.out.println(bytes.toHexString());

}

序列化产生的输出:

81 01                                    # myPerson
   04 4a 6f 68 6e                           # name
   07                                   # type
   00 00 00 00 00 e0 5e 40                     # balance

很容易看出消息的组成方式。

创建文件映射字节非常简单,随着更多数据的附加而增长,如下所示:

try {
    MappedBytes mb = MappedBytes.mappedBytes(new File("mapped_file"), 1024);
    mb.appendUtf8(
"John")
    .append(4.3f);
} catch (FileNotFoundException fnfe) {
    fnfe.printStackTrace();
}

这将创建一个名为“mapped_file”的内存映射文件。

$ hexdump mapped_file 
0000000 4a 6f 68 6e 34 2e 33 00 00 00 00 00 00 00 00 00
0000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
*
0001400