As far as possible the use of stack variables

  Abstract: as far as possible the use of stack variables 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width = "100%" border = "0" cellspacing = "0" cellpadding = " 0 "> <tr> <td width="271" height="54" align="center" valign="top"> </ td> <td width="413" valign="top"> If you frequent depositors admission variables, it is necessary to consider where access these variables.    Variable is static variables, or stack variable, or class instance variables?    Variable storage location of the access code of its performance significantly impact?    For example, please consider the following this code: </ td> </ tr> <tr> <td height="20" colspan="2"> 

  Class StackVars 
  ( 
  Private int instVar; 
  Private static int staticVar; 

  / / Stack variables void stackAccess access (int val) 
  ( 
  Int j = 0; 
  For (int i = 0; i    J + = 1; 
  ) 

  / / Access class instance variables void instanceAccess (int val) 
  ( 
  For (int i = 0; i    InstVar + = 1; 
  ) 

  / / Static variables such as access to void staticAccess (int val) 
  ( 
  For (int i = 0; i    StaticVar + = 1; 
  ) 
  ) 

  The code for each method is the implementation of the same cycle and repeated the same number.    The only difference is that each cycle to a different type of variable increments.    StackAccess way to a partial stack variables increase instanceAccess an example of the kind of variable increments, and staticAccess the category of a static variable increments. 

  InstanceAccess staticAccess and the execution time is basically the same.    However, stackAccess two to three times faster.    Access stack variables so quickly is because the JVM access stack variable than its access to static variables or class instance variables implementation of the operation less.    See what these three methods for generating the byte code: 

  Method void stackAccess (int) 
  0 iconst_0 / / 0 pressed into stacks. 
  1 istore_2 / / 0 and pop it stored in the variable part of the index table for the 2 position (j). 
  2 iconst_0 / / pressed into 0. 
  3 istore_3 / / 0 and pop it stored in the local variables in Table 3 for the location Index (i). 
  4 goto 13 / / Skip to position 13. 
  7 iinc 2 1 / / 2 storage in the index of j + 1. 
  10 iinc 3 1 / / will be stored in the index three of the i + 1. 
  13 iload_3 / / pressure indexed three values (i). 

  14 iload_1 / / pressed into an index value (val). 
  15 if_icmplt 7 / / pop i and val.    If i is less than val, Skip position 7. 
  18 return / / call methods. 

  Method void instanceAccess (int) 
  0 iconst_0 / / 0 pressed into stacks. 
  1 istore_2 / / 0 and pop it stored in the local variables in Table 2 position Index (i). 
  2 goto 18 / / Skip to position 18. 
  5 aload_0 / / pressure indexed 0 (this). 
  6 dup / / copy of the top of the stack and pressed into it. 
  7 getfield # 19 
  / / Pop this object reference and pressed into instVar value. 
  10 iconst_1 / / pressed into one. 
  11 iadd / / pop Zhanding the two values, and their pressure and all. 
  12 putfield # 19 
  / / Value of the two pop-up Zhanding and instVar and stored in the. 
  15 iinc 2 1 / / 2 storage in the index of the i + 1. 
  18 iload_2 / / pressure indexed two values (i). 
  19 iload_1 / / pressed into an index value (val). 
  20 if_icmplt 5 / / pop i and val.    If i is less than val, Skip position 5. 
  23 return / / call methods. 

  Method void staticAccess (int) 
  0 iconst_0 / / 0 pressed into stacks. 
  1 istore_2 / / 0 and pop it stored in the local variables in Table 2 position Index (i). 
  2 goto 16 / / Skip to position 16. 
  5 getstatic # 25 
  / / Storage pool staticVar constant pressure to the value stack. 
  8 iconst_1 / / pressed into one. 
  9 iadd / / pop Zhanding the two values, and their pressure and all. 
  10 putstatic # 25 
  / / Pop-up and the value and saves it in the staticVar. 
  13 iinc 2 1 / / 2 storage in the index of the i + 1. 
  16 iload_2 / / pressure indexed two values (i). 
  17 iload_1 / / pressed into an index value (val). 
  18 if_icmplt 5 / / pop i and val.    If i is less than val, Skip position 5. 
  21 return / / call methods. 

  Show bytecode stack variables revealed the reasons for the higher efficiency.    JVM is a heap-based virtual machine, so the stack has been optimized data access and processing.    All local variables are stored in a local variable form, in Java in the operation of stack processing, and can be efficiently access.    Access instance variables, static variables and higher costs because JVM must use more costly operation code, and from the constant access to their storage pool.    (Preservation of a constant storage pools used by the type of all types, fields and methods of symbols used.) 

  Usually, in the first visit from the constant storage pool static variables or instance variables, the JVM bytecode will be dynamic changes to the use of more efficient operation code.    Despite this optimization, stack variables still faster access. 

  Taking into account these facts, can be resumed in front of the building code, in order to access through the stack variable rather than static variables or instance variables to operate more efficiently.    Please consider the revised code: 

  Class StackVars 
  ( 
  / / In front of the same … 
  Void instanceAccess (int val) 
  ( 
  Int j = instVar; 
  For (int i = 0; i    J + = 1; 
  InstVar = j; 
  ) 

  Void staticAccess (int val) 
  ( 
  Int j = staticVar; 
  For (int i = 0; i    J + = 1; 
  StaticVar = j; 
  ) 
  ) 

  StaticAccess instanceAccess methods and will be adapted to their instance variables or copied to the local static variable stack variables.    When after the completion of the treatment variables, and its value has been copied to instance variables or static variables.    This simple change has markedly enhanced the instanceAccess and staticAccess performance.    These three methods of execution time is now basically the same, and staticAccess instanceAccess the implementation of speed than stackAccess only slow the implementation of about 4 per cent. 

  This does not mean that you should avoid the use of static variables or instance variables.    You should use your design meaningful storage mechanism.    For example, if you access a cycle of static variables or instance variables, you can temporarily store them in a partial stack variables, and this can significantly improve code performance.    This will provide the most efficient byte code instruction sequences for the JVM implementation. 

  </ Td> </ tr> </ table> 

  Function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( "CommentXMLStore"); document . getElementById (ElementID). CommentsPersistDiv.getAttribute value = ( "CommentContent");) </ td> </ tr> <tr> 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags:

Releated Java Articles

Comments

Leave a Reply