JDK-6914113 : Copy int to byte[] in 1 step
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 7,9,10
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: x86
  • Submitted: 2010-01-05
  • Updated: 2018-10-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Blocks :  
Blocks :  
Description
A DESCRIPTION OF THE REQUEST :
See source code and disassembly below.
Copying an int value to a byte[] in java code is only declarable in subsequent byte copies
I guess, copying the 4 bytes could be done at once after HotSpot compilation.

JUSTIFICATION :
I guess, copying the 4 bytes at once would be much faster, and save footprint.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The source code could be compiled to something like:
mov    0x24(%esp),%edi
..
mov    %eax,%ebx
bswap  %ebx     ; only little-endian architectures
mov    0x20(%esp),%esi
mov    0x8(%esi),%edx     ; implicit exception: dispatches to 0x00ba49e4
cmp    %edx,%edi
jae    0x00ba49a5
mov    0x24(%esp),%ebp
add    $0x3,%ebp
cmp    %edx,%ebp
jae    0x00ba49c1
mov    %ebx,0xc(%esi,%edi,4)  ;*bastore
...

ACTUAL -
Output from disassembly:

  0x00ba48d6: mov    0x24(%esp),%edi
  ...
  0x00ba492a: mov    %eax,%ebx
  0x00ba492c: sar    $0x18,%ebx         ;*ishr
                                        ; - sun.nio.cs.ext.EUC_TW_C_d_b_c1_f3_shortMap4$Encoder::encode@93 (line 491)
  0x00ba492f: mov    0x20(%esp),%esi
  0x00ba4933: mov    0x8(%esi),%edx     ; implicit exception: dispatches to 0x00ba49e4
  0x00ba4936: cmp    %edx,%edi
  0x00ba4938: jae    0x00ba49a5
  0x00ba493a: mov    %bl,0xc(%esi,%edi,1)  ;*bastore
                                        ; - sun.nio.cs.ext.EUC_TW_C_d_b_c1_f3_shortMap4$Encoder::encode@95 (line 491)
  0x00ba493e: mov    0x24(%esp),%ebp
  0x00ba4942: add    $0x3,%ebp
  0x00ba4945: mov    %eax,%ecx
  0x00ba4947: sar    $0x10,%ecx         ;*ishr
                                        ; - sun.nio.cs.ext.EUC_TW_C_d_b_c1_f3_shortMap4$Encoder::encode@104 (line 492)
  0x00ba494a: cmp    %edx,%ebp
  0x00ba494c: jae    0x00ba49c1
  0x00ba494e: mov    %cl,0xd(%esi,%edi,1)  ;*bastore
                                        ; - sun.nio.cs.ext.EUC_TW_C_d_b_c1_f3_shortMap4$Encoder::encode@106 (line 492)
  0x00ba4952: mov    %eax,%ebx
  0x00ba4954: sar    $0x8,%ebx
  0x00ba4957: mov    %bl,0xe(%esi,%edi,1)  ;*bastore
                                        ; - sun.nio.cs.ext.EUC_TW_C_d_b_c1_f3_shortMap4$Encoder::encode@117 (line 493)
  0x00ba495b: mov    %al,0xf(%esi,%edi,1)  ;*bastore
                                        ; - sun.nio.cs.ext.EUC_TW_C_d_b_c1_f3_shortMap4$Encoder::encode@125 (line 494)
 ...

Complete output here (line 2497):
https://java-nio-charset-enhanced.dev.java.net/source/browse/java-nio-charset-enhanced/branches/j7_EUC_TW/log/C_d_b_c1_f3_shortMap4_PA_2.xml?rev=888&view=markup


---------- BEGIN SOURCE ----------
In my code I have following snippet:

        byte[] da = ...;
        int dp = ...;
        int qb = ...;
        da[dp+3] = (byte)qb;
        da[dp+2] = (byte)(qb >> 8);
        da[dp+1] = (byte)(qb >> 16);
        da[dp+0] = (byte)(qb >> 24);

Complete source code (line 451):
https://java-nio-charset-enhanced.dev.java.net/source/browse/java-nio-charset-enhanced/branches/j7_EUC_TW/src/sun/nio/cs/ext/EUC_TW_C_d_b_c1_f3_shortMap4.java?rev=888

Complete project:
https://java-nio-charset-enhanced.dev.java.net/source/browse/java-nio-charset-enhanced/branches/j7_EUC_TW?rev=888

Run from NetBeans 6.8 using:
https://java-nio-charset-enhanced.dev.java.net/source/browse/java-nio-charset-enhanced/branches/j7_EUC_TW/nbproject/configs/C_d_b_c1_f3_shortMap4_PA.properties?rev=887&view=markup

---------- END SOURCE ----------

Comments
8026049 only optimizes ByteBuffer.put/get methods. It does not optimize general byte[] array access. You have to modify your code to use ByteBuffer to take advantage of 8026049 optimization.
31-03-2015

Isn't this bug now resolved by fix of https://bugs.openjdk.java.net/browse/JDK-8026049 ?
31-03-2015

Same optimization is arguable for char, short, long, float, double, String, BigXxxx.
14-03-2014

EVALUATION Will not do this. C2 type system will not allow store int into byte array. Other platforms require address alignment to 4 bytes for int store. Also on platforms with different endian the result will be different.
11-10-2011