Swap should be better done native?

Ulf Zibis Ulf.Zibis at gmx.de
Thu Apr 1 16:17:40 PDT 2010


Am 01.04.2010 23:55, schrieb Martin Buchholz:
> On Thu, Apr 1, 2010 at 05:32, Ulf Zibis<Ulf.Zibis at gmx.de>  wrote:
>
>    
>>> How can 4 put(byte) be converted into one put(int)?
>>>
>>>        
>> See the following code snippets ...
>>
>> ===================================================
>> Codesnippet from EUC_TW$Encoder:
>>             dst.put(SS2);
>>             dst.put((byte)(0xa0 | p));
>>             dst.put((byte)(db>>  8));
>>             dst.put((byte)db);
>>      
>    
>> ===================================================
>> Alternative 1 codesnippet:
>>             dst.putInt((SS2<<  24) | (0xa0<<  16) | (p<<  16) | db);
>>      
>
> Ahhh, I see.  Direct buffers are optimized to use Unsafe.putInt
> on platforms where unaligned writes are possible (i.e. x86).
>
> Some problems with that:
> - direct buffers are difficult to use, because the GC
>    cannot manage them well.
>    

Hm, but we can't ignore them, they are still there.

> - this only works on x86
>    

Correct me if I'm wrong: On all platforms 4 put(byte) can be converted 
into one put(int), but the performance win would be different.
Maybe using put(byte[]) would be better on other platforms ?

> - this doesn't work with other subclasses of ByteBuffer
>    

Not sure, if I understand right. Which subclasses you mean? CharBuffer, 
e.g., is NOT subclass of ByteBuffer.
But I see an advantage to have putInt/Long in Char/Short/IntegerBuffer.

> - I would think optimizing the case of ByteBuffer with arrays
>    (ie. hasArray() is true) would be more important.
>    

I don't think, optimizing the case for the direct buffers would harm the 
case for the heap buffers.
I too did some work on mixed cases, see:
https://java-nio-charset-enhanced.dev.java.net/source/browse/java-nio-charset-enhanced/trunk/src/sun/nio/cs/SingleByteDecoder_new.java?rev=&view=markup

All said should be too valid for getX methods.

-Ulf




More information about the nio-discuss mailing list