Java: Use stack variables whenever possible

Source: Internet
Author: User

 

If you frequently access variables, you need to consider where to access them. Variable isStatic variables, stack variables, or instance variables of the class? Does the storage location of a variable significantly affect the performance of the code that is accessed? For example, consider the following code:

Class StackVars
{
Private int instVar;
Private static int staticVar;
 
// Access stack variables
Void stackAccess (int val)
{
Int j = 0;
For (int I = 0; I <val; I ++)
J + = 1;
}
 
// Instance variables of the access class
Void instanceAccess (int val)
{
For (int I = 0; I <val; I ++)
InstVar + = 1;
}
 
// Access static variables of the class
Void staticAccess (int val)
{
For (int I = 0; I <val; I ++)
StaticVar + = 1;
}
}

Each method in this Code executes the same loop and repeats the same number of times. The only difference is that each loop increments a variable of different types. The method stackAccess increments a local stack variable, instanceAccess increments an instance variable of the class, and staticAccess increments a static variable of the class.

InstanceAccess and staticAccess have the same execution time. However, stackAccess is two to three times faster. It is faster to access stack variables because the JVM has fewer operations to access stack variables than to access static variables or class instance variables. Take a look at the bytecode generated for these three methods:

Method void stackAccess (int)
0 iconst_0 // press 0 into the stack.
1 istore_2 // pop up 0 and store it in the location where the local variable table index is 2 (j ).
2 iconst_0 // press 0.
3 istore_3 // pop up 0 and store it in the position (I) where the index of the local variable table is 3 ).
4 goto 13 // jump to position 13.
7 iinc 2 1 // j plus 1 that will be stored at index 2.
10 iinc 3 1 // I plus 1 that will be stored at index 3.
13 iload_3 // The value (I) at the index 3 ).
14 iload_1 // press the value (val) at Index 1 ).
15 if_icmplt 7 // The I and val are displayed. If I is smaller than val, jump to position 7.
18 return // return the call method.
 
Method void instanceAccess (int)
0 iconst_0 // press 0 into the stack.
1 istore_2 // pop up 0 and store it in the location (I) where the index of the local variable table is 2 ).
2 goto 18 // jump to position 18.
5 aload_0 // Press index 0 (this ).
6 dup // copy the value at the top of the stack and press it in.
7 getfield #19 <Field int instVar>
// The value of instVar referenced by this object is displayed.
10 iconst_1 // press 1.
11 iadd // pop up the two values at the top of the stack and press them into the sum.
12 putfield #19 <Field int instVar>
// The two values at the top of the stack are displayed and stored in instVar.
15 iinc 2 1 // I + 1 that will be stored at index 2.
18 iload_2 // The value (I) at the index 2 ).
19 iload_1 // press the value (val) at Index 1 ).
20 if_icmplt 5 // The I and val are displayed. If I is smaller than val, jump to position 5.
23 return // return the call method.
 
Method void staticAccess (int)
0 iconst_0 // press 0 into the stack.
1 istore_2 // pop up 0 and store it in the location (I) where the index of the local variable table is 2 ).
2 goto 16 // jump to position 16.
5 getstatic #25 <Field int staticVar>
// Press the staticVar value in the constant storage pool into the stack.
8 iconst_1 // press 1.
9 iadd // pop up the two values at the top of the stack and press them into the sum.
10 putstatic #25 <Field int staticVar>
// The pop-up sum value and store it in staticVar.
13 iinc 2 1 // I + 1 that will be stored at index 2.
16 iload_2 // The value (I) at the index 2 ).
17 iload_1 // press the value (val) at Index 1 ).
18 if_icmplt 5 // The I and val are displayed. If I is smaller than val, jump to position 5.
21 return // return the call method.
Viewing bytecode reveals why the stack variable is more efficient. JVM is a stack-based virtual machine that optimizes the access and processing of stack data. All local variables are stored in a local variable table, which is processed in the Java operand stack and can be efficiently accessed. It is more costly to access static variables and instance variables because the JVM must use a more expensive operating code and access them from the constant storage pool. (The constant storage pool stores symbolic references of all types, fields, and methods used by a type .)

Generally, after the static variable or instance variable is accessed from the constant storage pool for the first time, JVM dynamically changes the bytecode to use a more efficient operating code. Despite this optimization, the access to stack variables is still faster.

With these facts in mind, you can rebuild the previous code to make the operation more efficient by accessing the stack variables instead of instance variables or static variables. Consider the modified Code:

Class StackVars
{
// Same as the previous one...
Void instanceAccess (int val)
{
Int j = instVar;
For (int I = 0; I <val; I ++)
J + = 1;
InstVar = j;
}
 
Void staticAccess (int val)
{
Int j = staticVar;
For (int I = 0; I <val; I ++)
J + = 1;
StaticVar = j;
}
}

Methods instanceAccess and staticAccess are modified to copy their instance variables or static variables to the local stack variables. After the variable is processed, its value is copied back to the instance variable or static variable. This simple change significantly improves the performance of instanceAccess and staticAccess. The execution time of these three methods is basically the same. The execution speed of instanceAccess and staticAccess is only about 4% slower than that of stackAccess.

This does not mean you should avoid using static variables or instance variables. You should use a storage mechanism that makes sense to your design. For example, if you access static variables or instance variables in a loop, you can temporarily store them in a local stack variable, which can significantly improve the Code performance. This will provide the most efficient bytecode instruction sequence for JVM to execute.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.