Skip to content

Message Packers

Chathura Widanage edited this page Aug 19, 2019 · 1 revision

Packers in twister2 are responsible for converting a message to it’s byte representation at the sender's end and for converting a byte representation to a message(to the original structure) at the receiver’s end.

Twister2 has 6 variants of message packers built into the core.

Packer Description Implementations
Primitive Packers Converts a single primitive value to it’s byte representation and writes the byte sequence to the buffer. BytePacker, CharPacker, DoublePacker, FloatPacker, IntegerPacker,LongPacker
Primitive Array Packers Converts values in a primitive array one by one to it’s bytes representation and writes to the buffer as a sequence. ByteArrayPacker, CharArrayPacker, DoubleArrayPacker, FloatArrayPacker, IntegerArrayPacker,LongArrayPacker
Object Packer Converts any object to it’s bytes representation including the metadata of the object type. ObjectPacker
String Packer Uses CharArrayPacker underneath to write the byte representation of a String to the buffer StringPacker
Empty Packer Handles empty messages EmptyPacker

In addition to above 6 packers, twister2 provides the ability to define custom packers in order to efficiently convert custom structures or objects to it’s byte representation. While Object Packer works with any message type, it is not capable of producing the most compact byte representation of a message.

For example, below message could result in a byte representation of length ~30 with Object Packer while it can be compressed down to an 8 bytes representation by defining a custom packer which will convert each integer to it’s 4 byte representation and write them to the buffer as two consecutive byte chunks.

class RandomStructure{ int x = 12; int y = 10; }

Output with Object Packer : [1, 0, 99, 111, 109, 46, 99, 119, 105, 100, 97, 110, 97, 103, 101, 46, 75, 114, 121, 111, 84, 101, 115, 116, 36, 49, -63, 1, 24, 20]

Output with CustomPacker : [0, 0, 0, 12, 0, 0, 0, 10]

Another advantage of using custom packers is the ability to give an output of a definite length. For instance, if the values of x and y are changed to the maximum possible value of an integer, ObjectPacker outputs a byte representation of length 38 while CustomPacker will still output an 8 byte representation. When this property of packers is coupled with the headerless messaging capabilities of twister2, it could drastically boost the performance of the parallel communication operations.