Low-Level Communication Optimizations

There are number of optimizations you can make to the low-level communication infrastructure. These optimizations can be difficult to implement, and it is usually easier to buy these types of optimizations than to build them.

Compression

Where the distributed application is transferring large amounts of data over a network, the communications layer can be optimized to support compression of the data transfers. In order to minimize compression overhead for small data transfers, the compression mechanism should have a filter size below which compression is not used for data packets.

The JDK documentation includes an extended example of installing a compression layer in the RMI communications layer (the main documentation index page leads to RMI documentation under the “Enterprise Features” heading). The following code illustrates a simple example of adding compression into a communications layer. The bold type shows the extra code required:

void writeTransfer(byte[] transferbuffer, int offset, int len)
{
  if (len <= 0)
    return;
  int newlen = compress(transferbuffer, offset, len);
  communicationSocket.write(len);
  communicationSocket.write(newlen);
  communicationSocket.write(transferbuffer, offset, newlen);
  communicationSocket.flush( );
}

byte[] readTransfer( )
  throws IOException
{
  int len = communicationSocket.read( );
  if (len <= 0)
    throw new IOException("blah blah");
  int newlen = communicationSocket.read( );
  if (newlen <= 0)
    throw new IOException("blah ...

Get Java Performance Tuning now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.