Swap should be better done native?

Ulf Zibis Ulf.Zibis at gmx.de
Wed Mar 31 11:20:18 PDT 2010


Am 31.03.2010 02:59, schrieb Martin Buchholz:
> On Tue, Mar 30, 2010 at 07:39, Ulf Zibis<Ulf.Zibis at gmx.de>  wrote:
>    
>> In java.nio.Bits, there are several swap methods.
>>      
>    
>> Shouldn't this be done better by a native method, as popular CPU's IMO would
>> do that faster, and HotSpot easily could inline the single instruction?
>>      
> http://cr.openjdk.java.net/~martin/webrevs/openjdk7/nioBits.java/
>    

I don't see any advantage using Integer.reverseBytes(int).
Anyway, if swapping in java scope, for both following code could have 
little advantage (prevents from 32-bit immediate AND):
         return (i >>> 24) |
                ((i >> 8) & 0xFF00) |
                ((i & 0xFF00) << 8) |
                (i << 24);

After disassembly, current int swapping + save looks like:
   0x00b94cf0: mov    %ebp,%ecx
   0x00b94cf2: shl    $0x18,%ecx
   0x00b94cf5: mov    %eax,%esi
   0x00b94cf7: and    $0xff0000,%esi
   0x00b94cfd: and    $0xff00,%eax
   0x00b94d03: or     $0xa00000,%esi
   0x00b94d09: shl    $0x8,%eax
   0x00b94d0c: or     %eax,%ecx
   0x00b94d0e: shr    $0x8,%esi
   0x00b94d11: or     %esi,%ecx
   0x00b94d13: shr    $0x18,%ebp
   0x00b94d16: or     %ebp,%ecx
   ...
   0x00b94d2a: mov    %ecx,(%eax,%edx,1)  ;*invokevirtual putInt

but could look:
   ...
   0x00b94d02: movbe    %ebp,(%eax,%edx,1)  ;*invokevirtual putInt

char swap + save could look:
   ...
   0x00b94d02: movbe    %bp,(%eax,%edx,1)  ;*invokevirtual putChar


> Please study ByteArrayAccess.java and SwapMicroBenchmark.java
>    

Where to find?

> Can you measure a performance improvement from my change?
>    

IMHO above disassembly should demonstrate this obviously.

>    
>> Additionally, in current XByteBuffer implementations the term (x<<  0) is
>> frequently used.
>> Martin Buchholz has reported, that those zero-shifts reside in the byte
>> code, so IMO should be refactored, as at least interpreter would be
>> throttled by that.
>>      
> Probably hotspot can optimize it away, but why not help?
>    

Do you plan a mq patch for java.nio.Direct-X-Buffer.java.template and 
java.nio.Direct-X-Buffer-bin.java.template ?

-Ulf




More information about the nio-discuss mailing list