You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by "이희승 (Trustin Lee)" <tr...@gmail.com> on 2008/05/20 14:45:07 UTC

Feed back appreciated - Quick preview of buffer rewrite

Hi,

I've just checked in the following three classes:

* IoQueue - a Queue (java.util) which supports event listeners
* IoQueueListener - an event listener which is notified whenever a new  
element is going to be added, added or removed
* ByteBufferQueue - an IoQueue which provides special access methods for  
primitive types

The source code is available here:

http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/queue/

I chose java.util.Queue because we can reuse existing interface  
(java.util.Queue) and people knows what Queue is as they know what  
Input/OutputStream is.

IoQueueListener is similar to IoEventQueueHandler  
(org.apache.mina.filter.executor).  It can be used to throttle IoQueue  
dynamically, just like IoEventQueueThrottle does.  The difference is that  
it works with any type of objects including ByteBuffer rather than just  
IoEvent.  This allows a user to reuse this framework even after codec  
finished decoding (i.e. SEDA friendly).

ByteBufferQueue is what a codec or low-level filter will interact with.   
It has all the sequential getters and putters including random access  
getters based on byte offset.  It supports minimal set of types, but more  
access methods will be provided as static methods of a utility class.  Our  
long term goal is to provide a codec generater so that a user doesn't need  
to spend a lot of time to implement any kind of codec, so keeping it  
minimal makes sense.

All methods have been named after the methods in Queue, so it might sound  
a little bit long, but it shouldn't be a problem in the long term with the  
codec generator.

It satisfies most needs except that it doesn't have additional random  
access operations such as mark/reset, movePosition and random putters.  I  
dropped them because we can still read the buffer randomly using  
elementAsXXX(int offset) methods.  Considering random access operations  
are not used often, it should be OK.  Especially with the codec generator,  
the number of random access operations should be minimal.

What do you think about this proposal?  Any feed back is appreciated.

Thanks,

PS: Yes, I think it's time to finish this stuff quickly and move on to the  
codec generator.
-- 
Trustin Lee - Principal Software Engineer, JBoss, Red Hat
--
what we call human nature is actually human habit
--
http://gleamynode.net/

Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by "이희승 (Trustin Lee)" <tr...@gmail.com>.
On Wed, 21 May 2008 08:32:21 +0900, Rich Dougherty <ri...@rd.gen.nz> wrote:

> On Wed, May 21, 2008 at 9:36 AM, 이희승 (Trustin Lee) <tr...@gmail.com>
> wrote:
>
>> For now, slicing might copy or might not copy depending on the specified
>> position and length.  If the requested slice belongs to one ByteBuffer,  
>> no
>> copy will be performed.  Otherwise a new ByteBuffer will be created and
>> zero-copy in not realized.  Decoder implementor needs to understand  
>> this and
>> implement the decoder properly to achieve zero-copy.  State machine  
>> based
>> decoders should not have a problem with this IMHO.  Please let me know  
>> if I
>> am missing something.
>>
>> To provide more perfect zero-copy, we need to introduce a new type which
>> wraps one or more ByteBuffers.  Its implementation could be similar to
>> ByteBufferQueue, but it's different in that its length is fixed and it  
>> looks
>> exactly like ByteBuffer.  It's because ByteBuffer cannot be extended.
>>  Should we provide this?  I think we can.  The problem is that we need  
>> to
>> write a whole bunch of JavaDoc for the new buffer class, although it's  
>> just
>> one time task.
>
> It would also be possible to provide a more general method that returns  
> an
> Iterable<ByteBuffer>, but of course this would be harder for callers to  
> work
> with. Personally I prefer the "wrapper" option.

I think Daniel's idea satisfies both.  ByteBufferQueue extends  
Iterable<ByteBuffer>, and users still have nice access methods.

>> Another possible problem in introducing a new buffer type is overhead.
>>  ByteBufferQueue is already composite, and its elements can be  
>> composite -
>> index calculation cost might neutralize the advantage of zero-copy.  On  
>> the
>> other hand, we can keep the index calculation depth to two level at  
>> maximum
>> and to one level in most cases, so it might not be a big problem.  :)
>>
>> So, I'm up for introducing a new type.  However, there was also a  
>> request
>> that we have to use ByteBuffer as first citizen.  Please let me know  
>> again
>> if there's an issue with introducing a new type that we are missing.
>
> Another advantage of a "ByteBuffer wrapper" is that it could include a
> free()/dispose() method that would allow us to encapsulate disposal
> behaviour (e.g. returning the ByteBuffer to a pool). This is especially
> useful when you're doing complicated stuff with buffers, like slicing  
> them
> into bits and using them in different places.

This leads me to an interesting issue.

ByteBufferQueue.poll() returns ByteBuffer and we can remove it from the  
internal data structure.  However, we cannot return it to the pool because  
there's no guarantee when user will dereference it.  Explicit free method  
makes sense.  However, introducing the whole buffer types just because  
this one method sounds like an overkill.  It might be just enough with a  
static method in this case?  (or do we need mixin in Java? :D)

Thanks,
-- 
Trustin Lee - Principal Software Engineer, JBoss, Red Hat
--
what we call human nature is actually human habit
--
http://gleamynode.net/

Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by Rich Dougherty <ri...@rd.gen.nz>.
On Wed, May 21, 2008 at 9:36 AM, 이희승 (Trustin Lee) <tr...@gmail.com>
wrote:

> For now, slicing might copy or might not copy depending on the specified
> position and length.  If the requested slice belongs to one ByteBuffer, no
> copy will be performed.  Otherwise a new ByteBuffer will be created and
> zero-copy in not realized.  Decoder implementor needs to understand this and
> implement the decoder properly to achieve zero-copy.  State machine based
> decoders should not have a problem with this IMHO.  Please let me know if I
> am missing something.
>
> To provide more perfect zero-copy, we need to introduce a new type which
> wraps one or more ByteBuffers.  Its implementation could be similar to
> ByteBufferQueue, but it's different in that its length is fixed and it looks
> exactly like ByteBuffer.  It's because ByteBuffer cannot be extended.
>  Should we provide this?  I think we can.  The problem is that we need to
> write a whole bunch of JavaDoc for the new buffer class, although it's just
> one time task.
>

It would also be possible to provide a more general method that returns an
Iterable<ByteBuffer>, but of course this would be harder for callers to work
with. Personally I prefer the "wrapper" option.

Another possible problem in introducing a new buffer type is overhead.
>  ByteBufferQueue is already composite, and its elements can be composite -
> index calculation cost might neutralize the advantage of zero-copy.  On the
> other hand, we can keep the index calculation depth to two level at maximum
> and to one level in most cases, so it might not be a big problem.  :)
>
> So, I'm up for introducing a new type.  However, there was also a request
> that we have to use ByteBuffer as first citizen.  Please let me know again
> if there's an issue with introducing a new type that we are missing.


Another advantage of a "ByteBuffer wrapper" is that it could include a
free()/dispose() method that would allow us to encapsulate disposal
behaviour (e.g. returning the ByteBuffer to a pool). This is especially
useful when you're doing complicated stuff with buffers, like slicing them
into bits and using them in different places.

Cheers
Rich

Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by "이희승 (Trustin Lee)" <tr...@gmail.com>.
Let me snip and comment - I agree with you entirely.  :)

On Wed, 21 May 2008 08:34:24 +0900, Daniel Wirtz <da...@virtunity.com>  
wrote:
> What about slicing ByteBufferQueues to another ByteBufferQueue by default
> while reusing (e.g. duplicate() while using the same underlying byte  
> array)
> the affected underlying buffers...with the ability to transform the  
> contents
> to a single bytebuffer for backward compatibility (queue.toByteBuffer())?
> When doing this, the default behaviour will be zero-copy and ByteBuffers
> will still be used. I think mixing ByteBufferQueues with ByteBuffers by  
> the
> default implementation is not a perfect behaviour. If I slice a Queue, I
> expect a Queue to be returned in the first place.

What an idea!  I like it.  Then we don't need to introduce a new buffer  
type.  :)

We still need to make the static methods public then.

>> My idea is to provide something similar to ANTLR.  The difference is it
>> works in a binary level (or course including text level) and it  
>> generates
>> non-blocking MINA ProtocolDecoder.  Once properly done, we could  
>> replace all
>> existing codec with this and the maintenance cost will drop down
>> dramatically.  Instead we will have to maintain the decoder generator  
>> big
>> time, but it's more entertaining.  :)
>
> I thought about such a concept a little bit today and I am not yet  
> convinced
> that this will be practical. I think the generator would be very complex
> because very different rule sets and transformations need to be  
> implemented
> depending on the task. In some cases even callbacks will be inevitable.  
> Also
> error hooks that generate some output and close the connection (or keep  
> it,
> or jump to some other state etc.) will be challenging. Ok, this may be  
> some
> fun for a while but it may also be just one step too much for a  
> networking
> framework, that already is so easy to work with. In most cases I think
> it will be much easier to implement a protocol using switch() - also the
> proposed queues will make this a pleasure ;). Ok, maybe a generator can
> create more efficient code in some cases but it may not in every case  
> (most
> protocols are different/exotic by nature), and as I said I'm not  
> convinced
> that using the generator will be easier. However, if you decide to stick
> with it, I'll of course try it out someday ;).

Who knows?  We didn't even implement a line yet.  :)

It's true that MINA should provide a convenient way to build a codec even  
if we are going to provide a good tooling in the long term.  We will see  
what flesh we will add to the current prototype while we port examples and  
filters.

Thanks!
-- 
Trustin Lee - Principal Software Engineer, JBoss, Red Hat
--
what we call human nature is actually human habit
--
http://gleamynode.net/

Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by Daniel Wirtz <da...@virtunity.com>.
>
>  I thought about adding setOrder() method and ended up with not adding it
> because it can cause thread-safety issue when the queue is used in
> consumer-producer scenario.  For example, two threads could change the order
> at the same time and then the order can be mixed up.  Moreover, byte order
> doesn't change often, so I thought it might be a good idea to let user
> specify it when the queue is constructed.


Good point, this will be ok.


> For now, slicing might copy or might not copy depending on the specified
> position and length.  If the requested slice belongs to one ByteBuffer, no
> copy will be performed.  Otherwise a new ByteBuffer will be created and
> zero-copy in not realized.  Decoder implementor needs to understand this and
> implement the decoder properly to achieve zero-copy.  State machine based
> decoders should not have a problem with this IMHO.  Please let me know if I
> am missing something.


What about slicing ByteBufferQueues to another ByteBufferQueue by default
while reusing (e.g. duplicate() while using the same underlying byte array)
the affected underlying buffers...with the ability to transform the contents
to a single bytebuffer for backward compatibility (queue.toByteBuffer())?
When doing this, the default behaviour will be zero-copy and ByteBuffers
will still be used. I think mixing ByteBufferQueues with ByteBuffers by the
default implementation is not a perfect behaviour. If I slice a Queue, I
expect a Queue to be returned in the first place.


> To provide more perfect zero-copy, we need to introduce a new type which
> wraps one or more ByteBuffers.  Its implementation could be similar to
> ByteBufferQueue, but it's different in that its length is fixed and it looks
> exactly like ByteBuffer.  It's because ByteBuffer cannot be extended.
>  Should we provide this?  I think we can.  The problem is that we need to
> write a whole bunch of JavaDoc for the new buffer class, although it's just
> one time task.


Yes, this would be the first and imho the best possibility to achive this
behaviour.

But do we really need some huge wrapper class? Afaik e.g. HeapByteBuffer is
already a wrapper for the relevant byte array. What about just duplicating
bytebuffers (as it keeps the same underlying byte array, see constructors
inside ByteBuffer sources) for every queue instance so that it gains its own
position / limit / etc. values but still using the same underlying byte
array? Isn't this basically the same? Of course, when manipulating one
bytebuffer's contents, the other's will be modified too - but I don't
see any problem that may occur because of this because the content is
modified mostly on creation and when explicitly reusing buffers only and
slicing is mostly done when reading.

However, this only makes sence when the initial position and limit of a
bytebuffer are respected by the queue implementation (I think you already
planned it this way), so that partial buffer contents are possible
when based on those values - don't think this will be a problem in a
read-once-by-implementation queue where the position of the current
bytebuffer is modified only on removeXy(...) while forgetting all the
previous contents. Even slice(int length) is perfect for this because there
is no starting offset that may lay somewhere before the current bytebuffer's
position. This also does not change anything for automatic disposal, because
bytebuffers are just garbage collected, when there is no more reference to
them (e.g. when all queues are read completelty and the references were
subsequently removed in this process).


> Another possible problem in introducing a new buffer type is overhead.
>  ByteBufferQueue is already composite, and its elements can be composite -
> index calculation cost might neutralize the advantage of zero-copy.  On the
> other hand, we can keep the index calculation depth to two level at maximum
> and to one level in most cases, so it might not be a big problem.  :)


Think so, the index calculation could be limited to ByteBuffer (of course
adding some overhead when using duplicates) and a simple
current_position inside ByteBufferQueue...right?


> So, I'm up for introducing a new type.  However, there was also a request
> that we have to use ByteBuffer as first citizen.  Please let me know again
> if there's an issue with introducing a new type that we are missing.


The duplicate-approach would respect this, as mentioned I don't see a need
for a new type because ByteBuffer already comes with everything needed ootb
because of it's random access and all-content-between-position-and-limit
nature.


> Yes, we need to reuse the utility class on both ByteBuffers and
> ByteBufferQueues.  However, if we introduce a new buffer type, we can
> provide all of them to both ByteBufferQueue and the new buffer type.  To
> avoid code duplication, we will still have to keep static methods somewhere
> and make them package private.


Ok - however I have no idea what other buffer types would be required even
in the long term. Of course, modular design is not a bad thing, though.


> I am sorry about the users.. but consistency in naming is more important
> IMHO.  We can add getters and putters which delegates all operations to
> removeXXX and offerXXX, but I guess it will make ByteBufferQueue look huge.
>  Do you have better idea?  If so, that would be fantastic.
>

Sorry I don't, I was just wondering ;). For me it's ok to stick with the
queue specific methods. The rest will be done by toByteBuffer() but noone
should be encouraged to use this.


> Very true.  Both random access and first offset access should be provided
> for all getters and putters, and then we have no problem with such a
> protocol, right?  (just to make sure I understood the problem correctly. :)
>

Absolutely right :)


> My idea is to provide something similar to ANTLR.  The difference is it
> works in a binary level (or course including text level) and it generates
> non-blocking MINA ProtocolDecoder.  Once properly done, we could replace all
> existing codec with this and the maintenance cost will drop down
> dramatically.  Instead we will have to maintain the decoder generator big
> time, but it's more entertaining.  :)
>

I thought about such a concept a little bit today and I am not yet convinced
that this will be practical. I think the generator would be very complex
because very different rule sets and transformations need to be implemented
depending on the task. In some cases even callbacks will be inevitable. Also
error hooks that generate some output and close the connection (or keep it,
or jump to some other state etc.) will be challenging. Ok, this may be some
fun for a while but it may also be just one step too much for a networking
framework, that already is so easy to work with. In most cases I think
it will be much easier to implement a protocol using switch() - also the
proposed queues will make this a pleasure ;). Ok, maybe a generator can
create more efficient code in some cases but it may not in every case (most
protocols are different/exotic by nature), and as I said I'm not convinced
that using the generator will be easier. However, if you decide to stick
with it, I'll of course try it out someday ;).

regards
Daniel

Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by Rich Dougherty <ri...@rd.gen.nz>.
On Wed, May 21, 2008 at 9:36 AM, 이희승 (Trustin Lee) <tr...@gmail.com>
wrote:

>
> On Tue, 20 May 2008 22:56:03 +0900, Daniel Wirtz <da...@virtunity.com>
> wrote:
>
> And what about slicing contents from the buffer? Does it *copy* the
>> contents to a new bytebuffer or is a composite buffer used in the
>> background
>> to achive zero-copying when slicing over multiple (including partial)
>> buffers? Or is it not meant to deal with a ByteBufferQueue this way and
>> reuse a duplicated instance instead for further reading?
>>
>
> For now, slicing might copy or might not copy depending on the specified
> position and length.  If the requested slice belongs to one ByteBuffer, no
> copy will be performed.  Otherwise a new ByteBuffer will be created and
> zero-copy in not realized.  Decoder implementor needs to understand this and
> implement the decoder properly to achieve zero-copy.  State machine based
> decoders should not have a problem with this IMHO.  Please let me know if I
> am missing something.
>
> To provide more perfect zero-copy, we need to introduce a new type which
> wraps one or more ByteBuffers.  Its implementation could be similar to
> ByteBufferQueue, but it's different in that its length is fixed and it looks
> exactly like ByteBuffer.  It's because ByteBuffer cannot be extended.
>  Should we provide this?  I think we can.  The problem is that we need to
> write a whole bunch of JavaDoc for the new buffer class, although it's just
> one time task.
>

Hi

Just a note about the CompositeByteArray implementation that I've attached
to DIRMINA-489. If it was used as the underlying implementation for the
ByteBufferQueue then any of these behaviours could be easily provided.

*Always provide a single ByteBuffer* (even if this involves a copy)

composite.slice(index, length).getSingleByteBuffer()

*Provide an Iterable<ByteBuffer>* (thereby avoiding a copy)

composite.slice(index, length).getByteBuffers()

*Provide a wrapped ByteBuffer* (which may or may not be composite, and again
avoiding a copy)

composite.slice(index, length)

Simple! Of course, I'm possibly biased. :-)

Cheers
Rich

Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by "이희승 (Trustin Lee)" <tr...@gmail.com>.
Hello Daniel,

First off, thanks for the quick feed back.

On Tue, 20 May 2008 22:56:03 +0900, Daniel Wirtz <da...@virtunity.com>  
wrote:

> Hello Trustin,
>
>> I chose java.util.Queue because we can reuse existing interface
>> (java.util.Queue) and people knows what Queue is as they know what
>> Input/OutputStream is.
>
> Naming it a queue with removeXy getters instead of some non-standard
> stream sounds good so far, so the non blocking and automatic dispose  
> ideas
> become clear directly and the concept is basically the same like the
> proposed streams. However, I am still missing some setOrder(ByteOrder
> order). And what about slicing contents from the buffer? Does it *copy*  
> the
> contents to a new bytebuffer or is a composite buffer used in the  
> background
> to achive zero-copying when slicing over multiple (including partial)
> buffers? Or is it not meant to deal with a ByteBufferQueue this way and
> reuse a duplicated instance instead for further reading?

I thought about adding setOrder() method and ended up with not adding it  
because it can cause thread-safety issue when the queue is used in  
consumer-producer scenario.  For example, two threads could change the  
order at the same time and then the order can be mixed up.  Moreover, byte  
order doesn't change often, so I thought it might be a good idea to let  
user specify it when the queue is constructed.

For now, slicing might copy or might not copy depending on the specified  
position and length.  If the requested slice belongs to one ByteBuffer, no  
copy will be performed.  Otherwise a new ByteBuffer will be created and  
zero-copy in not realized.  Decoder implementor needs to understand this  
and implement the decoder properly to achieve zero-copy.  State machine  
based decoders should not have a problem with this IMHO.  Please let me  
know if I am missing something.

To provide more perfect zero-copy, we need to introduce a new type which  
wraps one or more ByteBuffers.  Its implementation could be similar to  
ByteBufferQueue, but it's different in that its length is fixed and it  
looks exactly like ByteBuffer.  It's because ByteBuffer cannot be  
extended.  Should we provide this?  I think we can.  The problem is that  
we need to write a whole bunch of JavaDoc for the new buffer class,  
although it's just one time task.

Another possible problem in introducing a new buffer type is overhead.   
ByteBufferQueue is already composite, and its elements can be composite -  
index calculation cost might neutralize the advantage of zero-copy.  On  
the other hand, we can keep the index calculation depth to two level at  
maximum and to one level in most cases, so it might not be a big problem.   
:)

So, I'm up for introducing a new type.  However, there was also a request  
that we have to use ByteBuffer as first citizen.  Please let me know again  
if there's an issue with introducing a new type that we are missing.


>> ByteBufferQueue is what a codec or low-level filter will interact with.  
>> It
>> has all the sequential getters and putters including random access  
>> getters
>> based on byte offset.  It supports minimal set of types, but more access
>> methods will be provided as static methods of a utility class.  Our long
>> term goal is to provide a codec generater so that a user doesn't need to
>> spend a lot of time to implement any kind of codec, so keeping it  
>> minimal
>> makes sense.
>
> I don't really understand why it is neccessary to create an utility class
> for reading e.g. Strings instead of implementing it directly into the
> ByteBufferQueue. Or is it planned to reuse the utility class on  
> ByteBuffers
> *and* ByteBufferQueues however the getters and setters are named
> differently? Or is there a plan to provide other ByteBufferQueue
> implementations other than using...ByteBuffers? ;). If not, then I don't  
> get
> it.

Yes, we need to reuse the utility class on both ByteBuffers and  
ByteBufferQueues.  However, if we introduce a new buffer type, we can  
provide all of them to both ByteBufferQueue and the new buffer type.  To  
avoid code duplication, we will still have to keep static methods  
somewhere and make them package private.

>> All methods have been named after the methods in Queue, so it might  
>> sound a
>> little bit long, but it shouldn't be a problem in the long term with the
>> codec generator.
>
> For me personally this is absolutely ok, but didn't you mention yourself,
> that this will make it more complicated changing from IoBuffer upwards  
> for
> users?

I am sorry about the users.. but consistency in naming is more important  
IMHO.  We can add getters and putters which delegates all operations to  
removeXXX and offerXXX, but I guess it will make ByteBufferQueue look  
huge.  Do you have better idea?  If so, that would be fantastic.

>> It satisfies most needs except that it doesn't have additional random
>> access operations such as mark/reset, movePosition and random putters.   
>> I
>> dropped them because we can still read the buffer randomly using
>> elementAsXXX(int offset) methods.  Considering random access operations  
>> are
>> not used often, it should be OK.  Especially with the codec generator,  
>> the
>> number of random access operations should be minimal.
>
> I aggree. The random access getters can do the job somehow. However,  
> let's
> say an immaginary protocol uses NUL terminated strings and somewhere  
> between
> them offsets to those strings as e.g. short values and there is a need to
> randomly jump to a specific NUL terminated string inside the buffer to  
> read
> it. As far as I understood, there will be an utility class for reading
> strings. So then, will every utility method have an optional offset
> parameter? Will every random utility method return the real number of  
> bytes
> read (e.g. when reading UTF-8 Strings with multibyte characters, the real
> length cannot be determined perfectly afterwards). Without, this kind of
> protocol will be hard to decode because the utility class could not be  
> used
> at all.

Very true.  Both random access and first offset access should be provided  
for all getters and putters, and then we have no problem with such a  
protocol, right?  (just to make sure I understood the problem correctly. :)

>> PS: Yes, I think it's time to finish this stuff quickly and move on to  
>> the
>> codec generator.
>
> Yes, I am already curious to use the generator :).

My idea is to provide something similar to ANTLR.  The difference is it  
works in a binary level (or course including text level) and it generates  
non-blocking MINA ProtocolDecoder.  Once properly done, we could replace  
all existing codec with this and the maintenance cost will drop down  
dramatically.  Instead we will have to maintain the decoder generator big  
time, but it's more entertaining.  :)

Thanks,
-- 
Trustin Lee - Principal Software Engineer, JBoss, Red Hat
--
what we call human nature is actually human habit
--
http://gleamynode.net/

Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by Daniel Wirtz <da...@virtunity.com>.
Hello Trustin,


> I chose java.util.Queue because we can reuse existing interface
> (java.util.Queue) and people knows what Queue is as they know what
> Input/OutputStream is.


Naming it a queue with removeXy getters instead of some non-standard
stream sounds good so far, so the non blocking and automatic dispose ideas
become clear directly and the concept is basically the same like the
proposed streams. However, I am still missing some setOrder(ByteOrder
order). And what about slicing contents from the buffer? Does it *copy* the
contents to a new bytebuffer or is a composite buffer used in the background
to achive zero-copying when slicing over multiple (including partial)
buffers? Or is it not meant to deal with a ByteBufferQueue this way and
reuse a duplicated instance instead for further reading?


> ByteBufferQueue is what a codec or low-level filter will interact with. It
> has all the sequential getters and putters including random access getters
> based on byte offset.  It supports minimal set of types, but more access
> methods will be provided as static methods of a utility class.  Our long
> term goal is to provide a codec generater so that a user doesn't need to
> spend a lot of time to implement any kind of codec, so keeping it minimal
> makes sense.


I don't really understand why it is neccessary to create an utility class
for reading e.g. Strings instead of implementing it directly into the
ByteBufferQueue. Or is it planned to reuse the utility class on ByteBuffers
*and* ByteBufferQueues however the getters and setters are named
differently? Or is there a plan to provide other ByteBufferQueue
implementations other than using...ByteBuffers? ;). If not, then I don't get
it.


> All methods have been named after the methods in Queue, so it might sound a
> little bit long, but it shouldn't be a problem in the long term with the
> codec generator.


For me personally this is absolutely ok, but didn't you mention yourself,
that this will make it more complicated changing from IoBuffer upwards for
users?


> It satisfies most needs except that it doesn't have additional random
> access operations such as mark/reset, movePosition and random putters.  I
> dropped them because we can still read the buffer randomly using
> elementAsXXX(int offset) methods.  Considering random access operations are
> not used often, it should be OK.  Especially with the codec generator, the
> number of random access operations should be minimal.


I aggree. The random access getters can do the job somehow. However, let's
say an immaginary protocol uses NUL terminated strings and somewhere between
them offsets to those strings as e.g. short values and there is a need to
randomly jump to a specific NUL terminated string inside the buffer to read
it. As far as I understood, there will be an utility class for reading
strings. So then, will every utility method have an optional offset
parameter? Will every random utility method return the real number of bytes
read (e.g. when reading UTF-8 Strings with multibyte characters, the real
length cannot be determined perfectly afterwards). Without, this kind of
protocol will be hard to decode because the utility class could not be used
at all.


> PS: Yes, I think it's time to finish this stuff quickly and move on to the
> codec generator.


Yes, I am already curious to use the generator :).

regards
Daniel

Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by Daniel Wirtz <da...@virtunity.com>.
On Wed, May 21, 2008 at 11:10 PM, Pauls, Karl <kp...@trigeo.com> wrote:

> If I understand you correctly, then apart from the kernel-level
> implications of zero-copy reads, such a facility to bring bytes into the JVM
> without a copy has existed in the JNI spec since Java 1.4.
>
> http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/functions.html#nio_support
>
> Note that this is an optional part of the JNI specification. So some
> vendors may not support it.
>
> Isn't this how org.apache.tomcat.jni.Pool is implemented?
>

I'm not familar with tomcat, but if e.g. the APR connector supports this
feature, it is free to inject its own kind of bytebuffers to the JVM for
Mina via JNI as far as I understood. The specific implementation (e.g.
Mina's APR transport) should cope with correctly obtaining direct buffers
from JNI then. Standard Nio SocketChannels do not seem to support a
channel.getBuffer() method or similar instead of channel.read(buffer),
though, so that using heap buffers with standard Nio is still the best
choice.

Injecting some sort of direct buffers to Mina is a part of the
particular JNI transport in the first place and will not result in any code
changes to the framework logic behind the transport as pointed out earlier -
which is especially the proposed queue implementation.

In this case, if the JNI transport can also get back a direct buffer (not
just inject it) it may make sence to allocate direct buffers within mina
that can be dealed with in this way to pass it pack to the transport - but
only in this perticular case of using JNI. For this, there is already a
possibility to achive it afaik: Using a custom IoBufferAllocator. I'm not
sure about the possible performance benefits, though. If there is one, it
may be worth thinking about implementing APR etc. perfectly to achive
zero-copy even on read/write from/to JNI. However, the currently planned
changes are not directly connected to this type of optimization because it's
more about the internal use of bytebuffers inside Mina.

But for the JNI transports it is a very good point!

regards
Daniel

RE: Feed back appreciated - Quick preview of buffer rewrite

Posted by "Pauls, Karl" <kp...@trigeo.com>.

> -----Original Message-----
> From: Daniel Wirtz [mailto:daniel@virtunity.com]
>
> Yes they have, but only if there is a way to pass some memory contents
> from
> one to another application (or the OS). To take advantage of this, we may
> need to hack the JVM and maybe the OS's network implementation I think.

If I understand you correctly, then apart from the kernel-level implications of zero-copy reads, such a facility to bring bytes into the JVM without a copy has existed in the JNI spec since Java 1.4.
http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/functions.html#nio_support

Note that this is an optional part of the JNI specification. So some vendors may not support it.

Isn't this how org.apache.tomcat.jni.Pool is implemented?

-karl

Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by Daniel Wirtz <da...@virtunity.com>.
 Hello karl,


> If we have interest in the hypothesis that copy-avoidance/zero-copy will
> realize broad server throughput advantages, then it may be more efficient
> overall to allocate direct buffers, process our bytes, and explicitly free
> the direct buffers. This removes a possibly heavy burden from the JVM at the
> cost of risking exhaustion of the memory available to your direct buffers.


However I think that this burden was taken into account when the JVM was
designed. When removing this burden, we should use C instead. All we can do
is move the memory allocation from the JVM to pure Java. The performance
impact may be hypotetical comparably that of imlementing the TCP/IP stack in
pure Java.


> Direct ByteBuffers have important practical and theoretical considerations
> for zero-copy reads, gathering writes, and hopefully (tho I haven't looked
> yet) zero-copy access to MINA APR bindings.


Yes they have, but only if there is a way to pass some memory contents from
one to another application (or the OS). To take advantage of this, we may
need to hack the JVM and maybe the OS's network implementation I think.

Let's say we socket.read(buffer) something. The contents will be inserted
into a buffer that we allocated before so it is absolutely indifferent what
type this buffer is of because it was not passed untouched by the network
interface (or the OS) to the JVM. The same happens for writing buffers - the
buffer must exist as some sort of memory and is then written through the JVM
to the OS's network implementation. Of course it may be copied by one
network implementation but not by another one - we don't know and we cannot
affect it anyhow after the contents have been written out.

I think the discussion spaces out a little. The zero-copy approach is mostly
about elimination the overhead of ByteBuffer.setAutoExpand(true) that copies
the whole buffer to resize it - and this is bad because it may happen very
often depending on the particular implementation of a protocol or a whole
application. Another advantage is that a queue may be reused to eliminate
the need to buffer the contents (as a copy) e.g. inside the protocol decoder
- with queues mina can do this for the protocol zero-copy. By extending the
previous bytebuffer approach, there are some nice performance benefits but
we should not lose the target so early ;)

regards
Daniel

Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by Daniel Wirtz <da...@virtunity.com>.
Hello Rich,

> Although MINA probably *uses* buffers for short periods of time, wouldn't
> it
> be possible to make the underlying buffers "long-lived" by pooling them
> between usages? Of course, if we pooled buffers then we'd need to support
> some mechanism of returning the buffers to the pool (probably an explicit *
> free* or *dispose* operation).


Memory is long-lived as is and the OS operates it "good" when the JVM
requests some. I don't see a reason to move memory allocation from the OS to
the application. This will only increase complexity (a need for free(),
accidentally disposed buffers that are still needed, maybe high memory
consumption of our single mina process in an environment that may need every
free byte of memory urgently etc.) and I'm not convinced that there will be
a noticable performance gain at all.


> To me, the question of whether or not its worth the bother of doing this
> comes down to the following questions:
> - is there any performance benefit of using direct buffers?


All I know about direct buffers is from the docs. Logically thinking, the
bytes in direct buffers must reside somewhere in non-JVM memory and can be
mapped to the JVM to avoid copying - and this makes it good e.g. for
explicitly mapping file contents to memory for fast access before saving it
back to disk (avoid IO operations). When working with network interfaces, I
don't see a possibility to improve performance. I think all the buzz about
direct buffers is from its naming as being "direct" which sounds good
somehow ;).

As always, I may be wrong...so feel free to correct me.

regards
Daniel

Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by Emmanuel Lecharny <el...@apache.org>.
이희승 (Trustin Lee) wrote:
> On Thu, 22 May 2008 05:14:24 +0900, Emmanuel Lecharny 
> <el...@apache.org> wrote:
>
>> Rich Dougherty wrote:
>>> Hi Daniel
>>>
>>> Although MINA probably *uses* buffers for short periods of time, 
>>> wouldn't it
>>> be possible to make the underlying buffers "long-lived" by pooling them
>>> between usages? Of course, if we pooled buffers then we'd need to 
>>> support
>>> some mechanism of returning the buffers to the pool (probably an 
>>> explicit *
>>> free* or *dispose* operation).
>>>
>>
>> Pooling objects instead of simple let the GC dispose them is not a 
>> good strategy with modern JVM. With a pool, you introduce a 
>> synchronized structure which will cost more to manage than letting 
>> the JVM creating new objects.
>>
>> I have played this game years ago, and when switching to Java 5, and 
>> even Java 1.4 to a certain extent, found it a waste of time and 
>> energy ...
>
> Allocating a big direct buffer (say 16MB) once and slicing it into 
> many pieces for allocation showed very nice performance.  
Very nice performance ? In the long term? On complex applications? This 
is simply rewriting a GC on your own. Again, I did it once upon a time, 
and this is a pretty complex jobs, not worth the effort, IMHO. JVM GCs 
are simply too good to spend time to try beating them in this area, 
unless you have very specific needs. Alas, MINA is a generic framework :)
> The problem is that the current JVM implementation is not good at 
> handling many big direct buffer allocations - they are not freed when 
> we need it to be freed.  Tomcat native provides a way to explicitly 
> free direct buffers, so it might resolve the issue.
>
> Anyway, I think we can implement ByteBufferQueue work with both heap 
> and direct buffers and then think about the side effect of using 
> direct buffers and discuss what would be the solution?
Yes, that's the best approach : first make it works, then if someone 
complains about the performance and exhibit better performance with 
Direct buffer, then start to implement another solution.

>
> Seems like we all like the proposed interface anyway.  Cool!
Yes, this is quite ok.
>
> Thanks,


-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org



Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by "이희승 (Trustin Lee)" <tr...@gmail.com>.
On Thu, 22 May 2008 05:14:24 +0900, Emmanuel Lecharny  
<el...@apache.org> wrote:

> Rich Dougherty wrote:
>> Hi Daniel
>>
>> Although MINA probably *uses* buffers for short periods of time,  
>> wouldn't it
>> be possible to make the underlying buffers "long-lived" by pooling them
>> between usages? Of course, if we pooled buffers then we'd need to  
>> support
>> some mechanism of returning the buffers to the pool (probably an  
>> explicit *
>> free* or *dispose* operation).
>>
>
> Pooling objects instead of simple let the GC dispose them is not a good  
> strategy with modern JVM. With a pool, you introduce a synchronized  
> structure which will cost more to manage than letting the JVM creating  
> new objects.
>
> I have played this game years ago, and when switching to Java 5, and  
> even Java 1.4 to a certain extent, found it a waste of time and energy  
> ...

Allocating a big direct buffer (say 16MB) once and slicing it into many  
pieces for allocation showed very nice performance.  The problem is that  
the current JVM implementation is not good at handling many big direct  
buffer allocations - they are not freed when we need it to be freed.   
Tomcat native provides a way to explicitly free direct buffers, so it  
might resolve the issue.

Anyway, I think we can implement ByteBufferQueue work with both heap and  
direct buffers and then think about the side effect of using direct  
buffers and discuss what would be the solution?

Seems like we all like the proposed interface anyway.  Cool!

Thanks,
-- 
Trustin Lee - Principal Software Engineer, JBoss, Red Hat
--
what we call human nature is actually human habit
--
http://gleamynode.net/

Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by Emmanuel Lecharny <el...@apache.org>.
Rich Dougherty wrote:
> Hi Daniel
>
> Although MINA probably *uses* buffers for short periods of time, wouldn't it
> be possible to make the underlying buffers "long-lived" by pooling them
> between usages? Of course, if we pooled buffers then we'd need to support
> some mechanism of returning the buffers to the pool (probably an explicit *
> free* or *dispose* operation).
>   
Pooling objects instead of simple let the GC dispose them is not a good 
strategy with modern JVM. With a pool, you introduce a synchronized 
structure which will cost more to manage than letting the JVM creating 
new objects.

I have played this game years ago, and when switching to Java 5, and 
even Java 1.4 to a certain extent, found it a waste of time and energy ...

-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org



Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by Rich Dougherty <ri...@rd.gen.nz>.
Hi Daniel

Although MINA probably *uses* buffers for short periods of time, wouldn't it
be possible to make the underlying buffers "long-lived" by pooling them
between usages? Of course, if we pooled buffers then we'd need to support
some mechanism of returning the buffers to the pool (probably an explicit *
free* or *dispose* operation).

To me, the question of whether or not its worth the bother of doing this
comes down to the following questions:
- is there any performance benefit of using direct buffers?
- is it feasible for users to manually dispose each buffer or slice that
they use *or* can we do this automatically for them somehow?

Cheers
Rich

On Thu, May 22, 2008 at 1:08 AM, Daniel Wirtz <da...@virtunity.com> wrote:

> From the ByteBuffer Java Docs: "It is therefore recommended that direct
> buffers be allocated primarily for *large*, *long-lived* buffers that are
> subject to the underlying system's native I/O operations." Because of
> the small and short-lived nature of buffers inside Mina, I don't see a
> reason for using direct buffers at all. Afaik direct buffers make sence
> e.g.
> for mapping large portions of files on a disk to direct memory for faster
> multiple access - I doubt that using them for network packets results in
> a performance gain (see also: DirectByteBuffer extends *MappedByteBuffer*).
>
> However, I played around a bit with direct and heap buffers, testing some
> possible cases that could lead to unexpected behaviour, but everything
> worked fine with direct buffers, too, so there should be no problem. Direct
> buffers support duplicate(), position(), limit(), all the
> setters/getters and they have their own custom dealocator that will hook
> the
> direct byte array to the wrapping directbytebuffer instance or even
> multiple
> instances for duplicates (
>
> http://dmi.ensica.fr/doc/Java/j2sdk-1_4_2-doc/docs/j2h/java/nio/DirectByteBuffer.java.html
> )
> so this should basically work the same like gcing heap buffers. The
> implementation seems to be mostly the same, just the underlying byte array
> is "directly mapped".
>
> Feel free to correct me if I'm wrong.
>
> regards
> Daniel
>

Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by Emmanuel Lecharny <el...@apache.org>.
Outside - Karl's ACM wrote:
> With respect, I would disagree with generalizations made from this excerpt in the javadoc. The important reason given in the javadoc is that allocation and freeing of direct buffers are more costly than the trivial cost of allocating a heap buffer. 
This is not eactly what the Javadoc says. The most important sentence is 
"...that are subject to the underlying system's native I/O operations". 
Ie, use direct buffer for tight integration with some OS managed memory 
(graphic memory, any device memory, etc).

Using direct buffer for something else is a kind of violation of this 
semantic.
> If we have interest in the hypothesis that copy-avoidance/zero-copy will realize broad server throughput advantages, then it may be more efficient overall to allocate direct buffers, process our bytes, and explicitly free the direct buffers. This removes a possibly heavy burden from the JVM at the cost of risking exhaustion of the memory available to your direct buffers.
>   
Not only. Allocating direct buffers involve some system calls which may 
be very costly, when heap buffers are handled by the JVM without telling 
the underlying OS. You may exhaust the system memory, which is 
defintively something you want to avoid. The direct memory is the system 
memory...

GC cost is generally largely overestimated... I'm doing some Java since 
1997, and I have always been told that GC cost a lot, blah, blah, but 
everytime I had to check and control some slow application, GC process 
was simply not costing more than a few %.

-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org



RE: Feed back appreciated - Quick preview of buffer rewrite

Posted by Outside - Karl's ACM <kp...@acm.org>.
With respect, I would disagree with generalizations made from this excerpt in the javadoc. The important reason given in the javadoc is that allocation and freeing of direct buffers are more costly than the trivial cost of allocating a heap buffer. I am confident that this is true in all cases where the garbage collector is not an issue!

> From: Daniel Wirtz [mailto:daniel@virtunity.com]
>
> From the ByteBuffer Java Docs: "It is therefore recommended that direct
> buffers be allocated primarily for *large*, *long-lived* buffers that are
> subject to the underlying system's native I/O operations." Because of
> the small and short-lived nature of buffers inside Mina, I don't see a
> reason for using direct buffers at all. Afaik direct buffers make sence
> e.g.
> for mapping large portions of files on a disk to direct memory for faster
> multiple access - I doubt that using them for network packets results in
> a performance gain (see also: DirectByteBuffer extends
> *MappedByteBuffer*).


If we have interest in the hypothesis that copy-avoidance/zero-copy will realize broad server throughput advantages, then it may be more efficient overall to allocate direct buffers, process our bytes, and explicitly free the direct buffers. This removes a possibly heavy burden from the JVM at the cost of risking exhaustion of the memory available to your direct buffers.

Direct ByteBuffers have important practical and theoretical considerations for zero-copy reads, gathering writes, and hopefully (tho I haven't looked yet) zero-copy access to MINA APR bindings.

I cannot yet confidently say that copy-avoidance/zero-copy in the JVM using heap or direct buffers will ever be a general best solution, but I am keenly interested in testing it.

-karl


Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by Daniel Wirtz <da...@virtunity.com>.
On Wed, May 21, 2008 at 11:00 AM, Eero Nevalainen <
eero.nevalainen@indagon.com> wrote:

> I think that the above use case works fine when using heap buffers, as when
> there are no more references to the message object the sliced buffers should
> be freed as well. But would I need to do something extra
> to make it work with direct buffers?


>From the ByteBuffer Java Docs: "It is therefore recommended that direct
buffers be allocated primarily for *large*, *long-lived* buffers that are
subject to the underlying system's native I/O operations." Because of
the small and short-lived nature of buffers inside Mina, I don't see a
reason for using direct buffers at all. Afaik direct buffers make sence e.g.
for mapping large portions of files on a disk to direct memory for faster
multiple access - I doubt that using them for network packets results in
a performance gain (see also: DirectByteBuffer extends *MappedByteBuffer*).

However, I played around a bit with direct and heap buffers, testing some
possible cases that could lead to unexpected behaviour, but everything
worked fine with direct buffers, too, so there should be no problem. Direct
buffers support duplicate(), position(), limit(), all the
setters/getters and they have their own custom dealocator that will hook the
direct byte array to the wrapping directbytebuffer instance or even multiple
instances for duplicates (
http://dmi.ensica.fr/doc/Java/j2sdk-1_4_2-doc/docs/j2h/java/nio/DirectByteBuffer.java.html)
so this should basically work the same like gcing heap buffers. The
implementation seems to be mostly the same, just the underlying byte array
is "directly mapped".

Feel free to correct me if I'm wrong.

regards
Daniel

Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by Eero Nevalainen <ee...@indagon.com>.
Emmanuel Lecharny wrote:
> 이희승 (Trustin Lee) wrote:
>> On Wed, 21 May 2008 10:08:27 +0900, Emmanuel Lecharny 
>> <el...@apache.org> wrote:
>>> Another things would also be to get a byte[] getBytes( in length ) 
>>> method returning as much bytes as possible, up to a length. If the 
>>> number of available bytes is below the length, the returned byte 
>>> array will only contains the available bytes, and it will be up to 
>>> the decoder to deal with it. The idea is to avoid calling the 
>>> element() method for every single byte we want.
>>
>> It sounds similar to ByteBufferQueue.pollSlice(int length), but 
>> pollSlice() returns null when there's not enough data.  
> Would be better to returns what has already been read. Sometime, you are 
> just proxying the data, and you want to send it to the next consumer as 
> soon as you get some. Otherwise, the queue will buffer potentially 
> gigantic data in memory. (This is something we experimented with 
> JpegPhoto data in Ldap, this is the reason I mention it).

Soooo.. Let's suppose I was writing a kind of proxy that receives 
messages and relays parts of those messages to several clients.

I'd get the relayable part of the message with a slice() operation.
My decoder would then make a message object with the ByteBufferQueue and 
send it for further processing.

For the clients my encoder would first write some header stuff with the 
offerXX() methods.
Then I think I'd need a method that offers a ByteBufferQueue (or 
Iterable<ByteBuffer>).


I think that the above use case works fine when using heap buffers, as 
when there are no more references to the message object the sliced 
buffers should be freed as well. But would I need to do something extra
to make it work with direct buffers?

-Eero Nevalainen




Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by Emmanuel Lecharny <el...@apache.org>.
이희승 (Trustin Lee) wrote:
>> Would be better to returns what has already been read. Sometime, you 
>> are just proxying the data, and you want to send it to the next 
>> consumer as soon as you get some. Otherwise, the queue will buffer 
>> potentially gigantic data in memory. (This is something we 
>> experimented with JpegPhoto data in Ldap, this is the reason I 
>> mention it).
>
> We already have ByteBufferQueue.poll() if it's just for consuming as 
> soon as possible?  Are you suggesting something else?
Sorry, I thought that poll() just return a byte (E -> byte) but 
suddenly, I realise that it returns a ByteBuffer ! This is perfect.

Coming back the the base idea, with you BBQueue, you can add a new 
filter to stream the big data to disk if needed, so that you won't kill 
a server if memory is limited. Incming data will be proxied to the 
stream filter, and a decoder using this stream will be able to work 
without knowing that the data is in memory or on disk. That's all good.



-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org



Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by "이희승 (Trustin Lee)" <tr...@gmail.com>.
On Wed, 21 May 2008 10:42:30 +0900, Emmanuel Lecharny  
<el...@apache.org> wrote:
> 이희승 (Trustin Lee) wrote:
>> On Wed, 21 May 2008 10:08:27 +0900, Emmanuel Lecharny  
>> <el...@apache.org> wrote:
>>> Another things would also be to get a byte[] getBytes( in length )  
>>> method returning as much bytes as possible, up to a length. If the  
>>> number of available bytes is below the length, the returned byte array  
>>> will only contains the available bytes, and it will be up to the  
>>> decoder to deal with it. The idea is to avoid calling the element()  
>>> method for every single byte we want.
>>
>> It sounds similar to ByteBufferQueue.pollSlice(int length), but  
>> pollSlice() returns null when there's not enough data.
> Would be better to returns what has already been read. Sometime, you are  
> just proxying the data, and you want to send it to the next consumer as  
> soon as you get some. Otherwise, the queue will buffer potentially  
> gigantic data in memory. (This is something we experimented with  
> JpegPhoto data in Ldap, this is the reason I mention it).

We already have ByteBufferQueue.poll() if it's just for consuming as soon  
as possible?  Are you suggesting something else?
-- 
Trustin Lee - Principal Software Engineer, JBoss, Red Hat
--
what we call human nature is actually human habit
--
http://gleamynode.net/

Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by Emmanuel Lecharny <el...@apache.org>.
이희승 (Trustin Lee) wrote:
> On Wed, 21 May 2008 10:08:27 +0900, Emmanuel Lecharny 
> <el...@apache.org> wrote:
>
>> We may need to deal with bits. ASN.1 PER decoder needs it.
>
> Yes.  Probably in the decoder?  I don't think we should have bit 
> getters/putters in our interface.  Then we should add all the bells 
> and whistles together. :)
Agreed.
>
>>> All methods have been named after the methods in Queue, so it might 
>>> sound a little bit long, but it shouldn't be a problem in the long 
>>> term with the codec generator.
>>
>> Just a question : do we need all those offerXXX() methods ? The type 
>> will be enough to select the correct method. And I also suggest that 
>> the default implementation should be for bytes, and that we have 
>> inherited classes for int, float, etc... which would make the 
>> implementation much easier (less methods).
>
> We need them like ByteBuffer provides them.  It's also important from 
> performance perspective because reading 8 bytes in a row with one call 
> often outperforms reading 8 bytes with 8 calls.  We are going to 
> provide access methods for all primitive types (except for boolean) 
> eventually so I don't think it will lead to less methods in total.
Yeah, you may be right. It makes sense to read a byte, then a long, then 
two integers...
>
>> Another things would also be to get a byte[] getBytes( in length ) 
>> method returning as much bytes as possible, up to a length. If the 
>> number of available bytes is below the length, the returned byte 
>> array will only contains the available bytes, and it will be up to 
>> the decoder to deal with it. The idea is to avoid calling the 
>> element() method for every single byte we want.
>
> It sounds similar to ByteBufferQueue.pollSlice(int length), but 
> pollSlice() returns null when there's not enough data.  
Would be better to returns what has already been read. Sometime, you are 
just proxying the data, and you want to send it to the next consumer as 
soon as you get some. Otherwise, the queue will buffer potentially 
gigantic data in memory. (This is something we experimented with 
JpegPhoto data in Ldap, this is the reason I mention it).

-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org



Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by "이희승 (Trustin Lee)" <tr...@gmail.com>.
On Wed, 21 May 2008 10:08:27 +0900, Emmanuel Lecharny  
<el...@apache.org> wrote:

> We may need to deal with bits. ASN.1 PER decoder needs it.

Yes.  Probably in the decoder?  I don't think we should have bit  
getters/putters in our interface.  Then we should add all the bells and  
whistles together. :)

>> All methods have been named after the methods in Queue, so it might  
>> sound a little bit long, but it shouldn't be a problem in the long term  
>> with the codec generator.
>
> Just a question : do we need all those offerXXX() methods ? The type  
> will be enough to select the correct method. And I also suggest that the  
> default implementation should be for bytes, and that we have inherited  
> classes for int, float, etc... which would make the implementation much  
> easier (less methods).

We need them like ByteBuffer provides them.  It's also important from  
performance perspective because reading 8 bytes in a row with one call  
often outperforms reading 8 bytes with 8 calls.  We are going to provide  
access methods for all primitive types (except for boolean) eventually so  
I don't think it will lead to less methods in total.

> Another things would also be to get a byte[] getBytes( in length )  
> method returning as much bytes as possible, up to a length. If the  
> number of available bytes is below the length, the returned byte array  
> will only contains the available bytes, and it will be up to the decoder  
> to deal with it. The idea is to avoid calling the element() method for  
> every single byte we want.

It sounds similar to ByteBufferQueue.pollSlice(int length), but  
pollSlice() returns null when there's not enough data.  It should be  
basically identical to pollSlice(length()), and therefore it's just a  
shortcut method.  So.. I think we already have enough methods for this  
case.  WDYT?

Thanks,
-- 
Trustin Lee - Principal Software Engineer, JBoss, Red Hat
--
what we call human nature is actually human habit
--
http://gleamynode.net/

Re: Feed back appreciated - Quick preview of buffer rewrite

Posted by Emmanuel Lecharny <el...@apache.org>.
Hi,



이희승 (Trustin Lee) wrote:
> Hi,
>
> I've just checked in the following three classes:
>
> * IoQueue - a Queue (java.util) which supports event listeners
> * IoQueueListener - an event listener which is notified whenever a new 
> element is going to be added, added or removed
> * ByteBufferQueue - an IoQueue which provides special access methods 
> for primitive types
>
> The source code is available here:
>
> http://svn.apache.org/viewvc/mina/branches/buffer/core/src/main/java/org/apache/mina/queue/ 
>
>
> I chose java.util.Queue because we can reuse existing interface 
> (java.util.Queue) and people knows what Queue is as they know what 
> Input/OutputStream is.
>
> IoQueueListener is similar to IoEventQueueHandler 
> (org.apache.mina.filter.executor).  It can be used to throttle IoQueue 
> dynamically, just like IoEventQueueThrottle does.  The difference is 
> that it works with any type of objects including ByteBuffer rather 
> than just IoEvent.  This allows a user to reuse this framework even 
> after codec finished decoding (i.e. SEDA friendly).
We may need to deal with bits. ASN.1 PER decoder needs it.
>
> ByteBufferQueue is what a codec or low-level filter will interact 
> with.  It has all the sequential getters and putters including random 
> access getters based on byte offset.  It supports minimal set of 
> types, but more access methods will be provided as static methods of a 
> utility class.  Our long term goal is to provide a codec generater so 
> that a user doesn't need to spend a lot of time to implement any kind 
> of codec, so keeping it minimal makes sense.
+1
>
> All methods have been named after the methods in Queue, so it might 
> sound a little bit long, but it shouldn't be a problem in the long 
> term with the codec generator.
Just a question : do we need all those offerXXX() methods ? The type 
will be enough to select the correct method. And I also suggest that the 
default implementation should be for bytes, and that we have inherited 
classes for int, float, etc... which would make the implementation much 
easier (less methods).
>
> It satisfies most needs except that it doesn't have additional random 
> access operations such as mark/reset, movePosition and random 
> putters.  I dropped them because we can still read the buffer randomly 
> using elementAsXXX(int offset) methods.  Considering random access 
> operations are not used often, it should be OK.  Especially with the 
> codec generator, the number of random access operations should be 
> minimal.
it should be null :)

Another things would also be to get a byte[] getBytes( in length ) 
method returning as much bytes as possible, up to a length. If the 
number of available bytes is below the length, the returned byte array 
will only contains the available bytes, and it will be up to the decoder 
to deal with it. The idea is to avoid calling the element() method for 
every single byte we want.

-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org