Basically, my situation is this:
- Server streams data from the client connection to a
ByteBufferobject called inQueue. This contains whatever the most recent stream of data is - Server must process the data in each of these streams and expect a packet of data in a specific format
- The payload of data is to be read into a
byte[]object then processed separately
Now my question boils down to this: is copying the remaining buffer data (the payload) to a byte[] array bad for performance?
Here’s what it would look like:
// pretend we're reading the packet ID and length
// int len = LENGTH OF PACKET PAYLOAD
/*
* Mark the starting position of the packet's payload.
*/
int pos = inQueue.position();
byte[] payload = new byte[len];
inQueue.get(payload);
// Process the packet's payload here
/*
* Set the inQueue buffer to the length added to the previous position
* so as to move onto the next packet to process.
*/
inQueue.position(pos + len);
As you can see, I’m essentially doing this:
- Mark the position of the complete buffer as it were just before the payload
- Copy the contents of inQueue as far as the payload goes to a separate
byte[]object - Set the complete buffer’s position to after the payload we just read so we can read more packets
My concern is that, in doing this, I’m wasting memory by copying the buffer. Keep in mind the packets used will never exceed 500 bytes and are often under 100 bytes.
Is my concern valid, or am I being performance-paranoid? :p
Not only is this unnecessary but, to answer your question, no you won’t notice a performance change even when scaling up.