Quick Tips for Fast Code on the JVM
I&#x27;d recommend a process rather than a handful of implementation-specific rules of thumb that are, IMO, at risk of being applied too generally. I&#x27;d hate someone to read this article and then go ahead and avoid &#x27;new&#x27; wherever possible because they think that&#x27;s how you make code fast, or avoiding boxing and polymorphic call sites, etc.<p>My process for writing fast code on the JVM:<p>1) Measure. Set up a benchmark so you know whether you&#x27;re on the happy path or not, or whether you&#x27;ve fallen off a performance cliff, for whatever reason. Make this part of your testing suite with some kind of notification for regressions.<p>2) Start small, ideally with a do-nothing loop over the input. This gives you a baseline; you can&#x27;t go faster than a do-nothing loop (presuming you can&#x27;t skip part of your input, which is an algorithm problem, not specific to JVM optimization).<p>3) Incrementally build up your algorithm and pay attention to when it falls off the performance cliff, using your benchmark from (1). If and when you fall off the performance cliff, that&#x27;s when you start bringing in tricks like avoiding new, ensuring call sites are monomorphic &#x2F; bimorphic, avoiding boxing, reducing pointer indirections and other cache friendly code, etc.<p>Another trick to consider is playing around with inlining, but not in the way you might think: try pushing infrequently executed code (conditionals) one level deeper in the call stack (i.e. making the body of an if-block into a method and calling it). The idea here is to encourage inlining of method doing the calling. Inlining is where the JVM gets to specialize your code to the specific situation at hand, but the JVM is reluctant to inline big methods because it has a time budget. So you need to help it.