Direct ByteBuffer advantage?

R

Remon van Vliet

Hello,

I was wondering, is there a big advantage over using small direct
bytebuffers for the actual SocketChannel.read() and write() methods instead
of heap-allocated bytebuffers.

I'm working on a server and having a small direct buffer for the actual IO
is a bit of a pain. So, what are the advantages and does anyone have
practical experiences with the difference between the two? I understand that
directly allocated buffers can potentially be mapped to native buffers and
whatnot, but still...it's a bit of a hassle.

Remon
 
J

John C. Bollinger

Remon said:
Hello,

I was wondering, is there a big advantage over using small direct
bytebuffers for the actual SocketChannel.read() and write() methods instead
of heap-allocated bytebuffers.

I'm working on a server and having a small direct buffer for the actual IO
is a bit of a pain. So, what are the advantages and does anyone have
practical experiences with the difference between the two? I understand that
directly allocated buffers can potentially be mapped to native buffers and
whatnot, but still...it's a bit of a hassle.

If the pain is already suffered then I don't see why you want to change
the code. On the other hand, if you are just anticipating the pain and
hassle then I would recommend instead writing straightforward code and
worrying only if it ends up not meeting your performance requirements.

Out of curiosity, however, what makes direct ByteBuffers so much more
trouble than others?
 
R

Remon van Vliet

Well, the thing is, direct buffers do not have a backing array
(byte[]). So, when i use small direct buffers for reads and writes, at
some point i have to put the contents of the direct buffer into a
larger bytebuffer (one that represents the entire packet i am trying to
send). The catch is that i may need to put the first half of the direct
buffer in one larger buffer (for example, if it's the end few bytes of
the previous packet) and put the rest in a new buffer. And there is
no(?) way to put a specific part (offset + length) of bytebuffer A in
bytebuffer B.

If there is an incorrect assumption in my reasoning let me know ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top