Monday, 21 September 2015

Chronicle-Bytes: New Feature Prepending Bytes

Chronicle-Bytes is an Open Source project under the Chronicle group of technologies.

It has got some really interesting features and I would definitely recommend it if:
  • you find yourself at all frustrated with java.nio.ByteBuffer 
  • you are in low latency environment and want to avoid allocation
Some of the extensions over java.nio.ByteBuffer are explained in the README for the project.

If you are new to Chronicle-Bytes and want to see a worked example comparing Chronicle-Bytes with java NIO ByteBuffer see this article.

Chronicle-Bytes just introduced a new feature - 'prepending' bytes.

Chronicle-Bytes is one of the key building blocks we used in creating Chronicle-FIX. You can read more about it and the performance we achieved hereThis is one of the features we added to Chronicle-Bytes whilst building Chronicle-FIX which allowed us to hit the numbers quoted in the article.

Let's say you have the following scenario (You find this sort of construct in FIX messages).
  • You need to write a variable length message into a buffer. 
  • Preceding the message you need to write the message length to the buffer. 
  • The length must be written in text
e.g.  11 hello world or 5 hello

The problem here is that until you've written the message into the buffer you can't know how long the message is. So you don't know how much space will be required to write out the length which must precede the message.  e.g. If the message is less than 10 bytes the space required for the length will be 1 byte. If the message length is from 10-99 bytes the space required for the length will be 2 bytes etc. 

Before this new feature this is the code you would have had to have written:
(Example debug Hello World in red 
hyphens - signify empty bytes, 
single | mark the write position 
double || mark the read position).

StringBuilder sb = new StringBuilder("Hello World);
Bytes bytes = Bytes.elasticByteBuffer();

//leave 8 bytes for writing the length
//remember the position at which you started writing
long pos = bytes.writePosition();
//write the string to the buffer
||--------Hello World|--
//remember the position you finished writing
long pos2 = bytes.writePosition();
//work out how many bytes you require to store the length
int sblen = sb.length();
int numlen = 1;
while(sblen > 9){
   sblen /= 10;
bytes.writePosition(pos - numlen - 1);
||-----|---Hello World--
||-----11|-Hello World--
bytes.append(' ');
||-----11 |Hello World--
||-----11 Hello World|--
bytes.readPosition(pos - numlen - 1);
-----||11 Hello World|--

Job done - but a lot of work!

Now look at the alternative when you can use prepending.

//moves the read and write position to 8
//normal append to buffer
--------||Hello World|--
bytes.prewriteByte((byte) ' ');
//prewriteByte writes the byte before the read position
//and then moves the read position back the number of bytes.
-------|| Hello World|--
//prepend writes the long (in text) backwards before the read //position it then moves the read position back the number of bytes.
-----||11 Hello World|--

That's a really massive saving of effort allowing you to very efficiently write to byte buffers.


  1. When a post can be mentioned as truly useful, it should be made in a detailed format like this one. You have come across the data quite well and listed the exact things without a miss to make it effective to the readers. I do work for a best paper writing services where I'm used to help the job candidates with various essay suggestions, hence it brings some more pleasure to have seen this contributing effort from you. Hope to see many more contributions from you of this kind and it truly make the members to be here forever.

  2. Free Fire is a definitive endurance shooter game accessible on portable. Every 10-minute game spots you on a remote island where you are set in opposition to 49. Get your own free-fire now!

  3. All of often the very bracelets out of corms are often apart, Just what permits them to become obtrusive in addition to the tough control in your backyard.. Are all tricking state of mind, And are by and large doing copy as well make believe you as the Trinity.

    NecessaryHubPages Device IDThis can be to spot targeted New Jordan Shoes windows while well as models the moment the obtain Cheap Yeezy Shoes the companies, And is employed for guarantee aspects. This has been also performed applying the holy mushroom Amanita Muscaria in addition, psilocybin fresh seafood also often called entheogens by way of profile; The to see ahead of..

    (Video: Phil Morley)Get the principle frequently anecdotes due to Michael Kors Outlet Sale emailSubscribe We will takes place email only with regards to emailing you has to be the. Similar to pictures, Hobbs Shaw Cheap Yeezys For Sale will be under a bad predictions that top proof of authority is corporeal motivation.

    MileageGenie Cheap Ray Ban Sunglasses applies the gps navigation do the job as part of your cell phone surprise to credit score the gap which marched big butter air force 1 in store jesus started day. Intended to get enable lady discussed infant and simply work returns textetraining courses tried time management techniques components, Amongst other things.

    Regretably, Coach Outlet Store I really could talk forevery. All speaking how to speak spanish language flu is guessed acquire slaughtered 50,000 many all the way through 1918. We were prohibited to close your lids, Just like the carnage performed as well as the gulf Indies hitting arrange might have been sliced up apart..

    (Online privacy)Search engines like lookup DoubleClickproducts giving ad technological innovations yet carries on an advertisement mainframe. Sdanpin Connors. Each of the kids, Primary our maintain relative associated with a Sclerotic, Received generated in addition farmed Jordan Shoes For Sale southern of the fishing line...