Asked “What is Refactoring?” Martin Fowler answers “Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.”
Cyrus Najmabadi (developer at Microsoft) writes on his blog Cyrus’ Blather, “… if you perform an “extract method” of some code that sits in a tight loop, and that the jit doesn’t inline, then you’ll have changed the external behavior with respect to its performance. However, there’s no good way to detect this, and most people don’t care. Similarly, if your code makes decisions based on runtime type information (i.e. it uses reflection), then it’s possible to change it’s external behavior because suddenly this alteration of internal structure becomes visible. However, customers have spoken and have stated that they’re fine with these caveats and so we accept them. “. He then goes into the issue of what tools should do if the code that’s being refactored is broken.
I disagree, I think developers do care. It’s just that there *are* no tools that provide such information but true there is “no good way to detect” refactorings that could potentially affect performance. The major problem is with the runtime providers themselves (CLR, Hotspot, J9,…), they just don’t publish the heuristics used in sub systems like JIT compilers. Hotspot and to some extent JRockit expose some of their internal metrics with interfaces like JMX and tools like jvmstat/visualgc but it’s all just runtime info. (for now?) which makes it hard to infer things like compiler heuristics.
What’s clear is that one should certainly watch out for performance when refactoring, as code structure and size matter (eg. PermGen size increase can cause Full GCs in Hotspot, invocation threshold for JIT compilation is a tunable in some runtimes, each JIT has its own inlining heuristics, etc)., especially now that concepts like Refactoring to Patterns are gaining momentum. What’s known is that if not wisely applied, patterns affect code complexity . And code complexity as we all know is second cousin to poor performance.