title: Efficiently collating the number of method calls per thread via Byte Buddy?
body: <p>I am currently using byte buddy to add some simple logic that counts the number of total method invocations on a per thread basis.</p>
  
  <p>For advice instrumentation, I have something along the lines of:</p>
  
  <pre><code> @Advice.OnMethodEnter
   static void handle() {
     MethodCounter.increment();
   }
  </code></pre>
  
  <p>In <code>MethodCounter#increment</code>, I have a very simple <code>ThreadLocal</code> counter, and the counter itself just increments an integer:</p>
  
  <pre><code>public class MethodCounter {
      final ThreadLocal&lt;Counter&gt; threadCounter = new ThreadLocal&lt;Counter&gt;();
      public static void increment() {
          threadCounter.get().increment();
      }
  
      ... and some other logic that ensures that the Counter is initialized for the current thread ...
  }
  </code></pre>
  
  <p>When using JMH to benchmark this new logic, I am noticing about a 30% degradation in performance in a sample workflow (containing tight loops).  Most of this seems to be due to the <code>ThreadLocal</code> -- if I instead get rid of the <code>ThreadLocal.get()</code> and hardcoding <code>increment()</code> to increment a static <code>Counter</code>, there is minimal performance impact.</p>
  
  <p>Is there a more performant way to accomplish this with byte buddy while maintaining per-thread isolation?  </p>