I have a long process that has two main stages. The first stage and the second stage slightly vary in terms of their execution path.
I just realized that if I create a copy of the same method with a different name and use different names in each stage, according to JMH (-server
, on java-7-openjdk-amd64
), I get more than 25% speedup for the method calls in the second stage (over millions of calls to the method, measured with 5 invocations, after a 5 invocation warmup).
Is there a way to tell JVM to forget previous optimizations about a method and relearn from scratch?
In the following example code, the benchmarked method is run
and the comparison is done between two versions calling checkChar
and checkChar0
in stage2
.
final public void run(){
sumStg1=0;
for(int i=0; i< 10000; i++){
String str = consumeString();
for(int i= 0; i<K; i++){
sumStg1 += checkChar(str.charAt(i), i)?1:0;
}
}
sumStg2=0;
for(int j=0; j< 10000000; j++){
String str = consumeString();
for(int i=K/2; i<str.length(); i++){
sumStg2 += checkChar(str.charAt(i), i)?1:0;
}
}
}
final public boolean checkChar(char in, int i){
if(i < K/2){
...
} else if (i < K){
...
} else {
...
}
}
//identical method to checkChar
final public boolean checkChar0(char in, int i){
if(i < K/2){
...
} else if (i < K){
...
} else {
...
}
}