title: Why is a small wrapped method not getting optimised?
body: <p>I am running an experiment to determine the performance overhead of wrapping a method. I have read that the JIT compiler and/or JVM optimise small methods, but I seem to consistently incur a 3-5% performance penalty.</p>
  
  <p>The code is as follows:</p>
  
  <pre><code>import java.util.* ;
  
  public class WrappingTest1{
      private WrappingTest1(){
          // Empty.
      }
  
      private static void findPrimes(
          final Long maxValue ,
          final List&lt; Long &gt; foundPrimes
      ){
          if(
              maxValue &gt; 2L
          ){
              Boolean isPrime ;
              foundPrimes.clear() ;
  
              for(
                  Long i = 2L ;
                  i &lt;= maxValue ;
                  i += 1L
              ){
                  isPrime = true ;
                  for(
                      Long j = 2L ;
                      j &lt; i ;
                      j += 1L
                  ){
                      if(
                          ( i % j ) == 0
                      ){
                          isPrime = false ;
                      }
                  }
                  if(
                      isPrime
                  ){
                      foundPrimes.add(
                          i
                      ) ;
                  }
              }
          }
      }
  
      private static void wrapper(
          final Long input ,
          final List&lt; Long &gt; output
      ){
          findPrimes(
              input ,
              output
          ) ;
      }
  
      public static void main(
          final String[] args
      ){
          ArrayList&lt; Long &gt; primes ;
          Long startTime ;
          Long endTime ;
          Double duration ;
          Double meanDuration ;
          Long primeRange ;
          Long warmupIterations ;
          Long benchmarkIterations ;
  
          primes = new ArrayList&lt;&gt;() ;
          meanDuration = 0.0 ;
          primeRange = 100L ;
          warmupIterations = 20000L ;
          benchmarkIterations = 100000L ;
  
          System.out.println(
              "Experiment started."
          ) ;
  
          // Unwrapped warmup.
          for(
              Long i = 0L ;
              i &lt; warmupIterations ;
              i += 1L
          ){
              findPrimes(
                  primeRange ,
                  primes
              ) ;
          }
  
          // Unwrapped benchmark.
          startTime = System.nanoTime() ;
          for(
              Long i = 0L ;
              i &lt; benchmarkIterations ;
              i += 1L
          ){
              findPrimes(
                  primeRange ,
                  primes
              ) ;
          }
          endTime = System.nanoTime() ;
          duration = ( endTime.doubleValue() - startTime.doubleValue() ) / 1E9 ;
          System.out.println(
              "Unwrapped runtime: " + duration + " seconds."
          ) ;
  
          // Wrapped warmup.
          for(
              Long i = 0L ;
              i &lt; warmupIterations ;
              i += 1L
          ){
              wrapper(
                  primeRange ,
                  primes
              ) ;
          }
  
          // Wrapped benchmark.
          startTime = System.nanoTime() ;
          for(
              Long i = 0L ;
              i &lt; benchmarkIterations ;
              i += 1L
          ){
              wrapper(
                  primeRange ,
                  primes
              ) ;
          }
          endTime = System.nanoTime() ;
          duration = ( endTime.doubleValue() - startTime.doubleValue() ) / 1E9 ;
          System.out.println(
              "Wrapped runtime: " + duration + " seconds."
          ) ;
  
          System.out.println(
              "Experiment completed."
          ) ;
      }
  }
  </code></pre>
  
  <p>The results are as follows:</p>
  
  <pre><code>Experiment started.
  Unwrapped runtime: 4.851473465 seconds.
  Wrapped runtime: 5.078349508 seconds.
  Experiment completed.
  </code></pre>
  
  <p>Why is this happening? How can I get the JVM to inline the wrapped method, or otherwise optimise it such that the wrapper is ignored?</p>
  
  <p>Thanks.</p>