What's the best way to understand CompositeBuffer and BuffersBuffer?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

What's the best way to understand CompositeBuffer and BuffersBuffer?

yangjun2
1.What's the best way to understand CompositeBuffer and BuffersBuffer?  
Why not use JDK's Buffer class , and customisze it?

2.When use allocatedirect(), what's the best way to monitor the DirectByteBuffer?
Best Regards,
Young
Reply | Threaded
Open this post in threaded view
|

Re: What's the best way to understand CompositeBuffer and BuffersBuffer?

oleksiys
Administrator
On 06/22/2011 08:16 AM, yangjun2 wrote:
> 1.What's the best way to understand CompositeBuffer and BuffersBuffer?
CompositeBuffer is general interface, BuffersBuffer is the implementation.
CompositeBuffer let's us look at Buffer[] as a single Buffer, which
simplifies parsing, serializing logic.

Typical situation we're parsing custom protocol message from byte[] and
realizing our byte[] doesn't contain entire message. To leverage NIO we
have to save the current ByteBuffer (byte[]) data, terminate channel
processing in this thread and wait until more data is available. So next
time more data becomes available, we'll get another ByteBuffer (byte[]).
So now we have 2 ByteBuffers and have to parse data there (may be we'll
need 3rd, 4th ByteBuffer...). IMO it's much more difficult to implement
parsing logic for ByteBuffer[] comparing to single ByteBuffer.

You may ask why not define MAX_MESSAGE_SIZE and always preallocate
ByteBuffer of size MAX_MESSAGE_SIZE for each message we parse. So if we
don't have enough data after first read, we store the entire ByteBuffer
and next time read into the same ByteBuffer. That's also an option, but
in that case we have to define MAX_MESSAGE_SIZE, which might not be
suitable for some protocols. But the more serious problem, if the
MAX_MESSAGE_SIZE is big enough, is that all the times we have to keep in
memory huge incomplete ByteBuffer per Connection. So finally we can
easily hit OutOfMemoryError.

> Why not use JDK's Buffer class , and customisze it?
This is abstract class, which has package-private constructor, so
there's no clear way to extend it.

> 2.When use allocatedirect(), what's the best way to monitor the
> DirectByteBuffer?
Not sure I completely understand the question.
You can register MemoryProbe on MemoryManager and track MemoryManager's
allocations.

In Grizzly we use direct ByteBuffers on Transport level only to optimize
Channel read and write operations, we cache them per thread to save
memory and time spent on ByteBuffer allocation.
Otherwise we use heap buffers.

WBR,
Alexey.

>
> -----
> Best Regards.
> yangjun
> --
> View this message in context: http://grizzly.1045725.n5.nabble.com/What-s-the-best-way-to-understand-CompositeBuffer-and-BuffersBuffer-tp4513056p4513056.html
> Sent from the Grizzly - Users mailing list archive at Nabble.com.