<img src="https://secure.leadforensics.com/794635.png" style="display:none;">

Does refactoring make code slower?

We refactored Street Fighter III’s 300K-line C codebase, raising Code Health from 5.6 to 10.0. There was no performance trade-off. The refactored version used between 1.0% less and 2.1% more CPU time.

Copied!

 

For the past 15 years, I’ve been pushing for the importance of healthy codebases. Agentic coding has made it more urgent than before: unhealthy code increases the failure risks, and consumes more tokens. So there are both sound financial and business arguments for healthy code.

Still, there’s one objection I keep hearing. It usually goes something like: “We can’t refactor this code. It’s performance-critical.” That objection never matched our own experience, so we decided to measure the runtime cost of a performance-critical application before and after refactoring.

Follow along, and we’ll look at the numbers.

 

Why the performance concern?

Refactoring typically leads to a more modular codebase. That is, you will have more files and more functions. That modularity — if done well — makes the code easier to understand and navigate for humans and coding agents alike.

The flip side is that there’s a potential overhead due to the extra function calls in the more modular code. While that might have been true back in the day, it doesn’t necessarily hold for modern CPUs and compilers. Optimizing compilers can often inline and eliminate the overhead.

In fact, Dave Farley, who made a career working on high-performance systems, shares the following in Modern Software Engineering:

“One of the most common excuses for unpleasant code [...] is that you have to write more complex code if you want high performance. [...] I can assure you that this is not the case.”

While experience like that should instill some confidence, there’s nothing like hard numbers. So let’s look at our brand new performance benchmarking study.

 

Benchmarking performance: the study

Our first step was to find a non-trivial codebase that’s suitable for measuring performance. In fact, we needed two versions of that codebase: one unhealthy, and one refactored. Our recent agentic refactoring research delivered exactly that: the Street Fighter III codebase.

  • The Street Fighter codebase consists of 300k lines of C code.
  • The original codebase had a Code Health of 5.6 on a 10-point scale. This implies a high degree of technical debt.
  • After 2,903 agentic commits, we had refactored the whole codebase to a 10.0 Code Health. (See this article for the full story).
  • Behavior was preserved via tests, so the before and after refactoring versions are functionally equivalent.

To study performance, we focused on measuring the game loop.

The Street Fighter III game loop runs at a fixed 60 frames per second, so the frame rate cannot reveal a difference. Instead, we measured the CPU time needed to simulate each frame when running at full speed. (The code contains a stress mode for this purpose).

This means that the test plays a full fight without a window, and is driven by random but seeded controller input. That makes the test fully deterministic: the same seed produces exactly the same fight. We then read the CPU clock inside the game at two points for comparison.

 

The results: does refactoring make code slower or faster?

We compared the performance before and after the Code Health uplift on three independent setups: two operating systems, two CPU architectures, and three compilers.

Here’s what we learned:

  • General runtime impact: In optimized builds, the uplifted code runs its game loop within −1.0% to +2.1% of the original.
  • Some platforms perform better: With clang and link-time optimization on Windows, the uplifted code is about 1% faster.
  • Program size: Remains about equal. Release executables differ by less than 3% on all three setups.

game_loop_CPU_time

Game-loop CPU time of the uplifted version relative to the original, in percent. Each marker is the geometric mean over 30 seeds with its 95% confidence interval. Filled markers: the interval excludes zero.

Build Mac · Apple clang (arm64) Windows · clang (x86-64) Windows · GCC (x86-64)
Release (-O3) +1.7% (0.8 to 2.5); 23/30 +0.4% (0.2 to 0.6); 20/30 +1.2% (1.0 to 1.4); 29/30
Release + LTO (clang: ThinLTO) +0.5% (−0.4 to 1.3); 20/30 −0.8% (−1.0 to −0.5); 2/30 +0.9% (0.7 to 1.1); 29/30
Release + full LTO +2.1% (1.4 to 2.7); 26/30 −1.0% (−1.2 to −0.8); 1/30 not built
Release + PGO +1.6% (0.9 to 2.3); 25/30 +0.5% (0.3 to 0.7); 23/30 +0.8% (0.6 to 1.0); 29/30
Debug +0.7% (0.6 to 0.9); 29/30 +0.8% (0.7 to 0.8); 30/30 not built
Debug + AddressSanitizer +3.1% (3.0 to 3.4); 30/30 not built not built

Game-loop CPU time, uplifted vs. original: mean difference (95% CI); seeds slower out of 30.

 

What does this mean for software engineering?

Runtime performance is a critical non-functional requirement in many domains: games, telecom, finance, or — why not — behavioral code analysis.

I’ve also had the pleasure of working on performance critical real-time systems, and Dave Farley’s earlier observation perfectly echoes my own experience. I remember one system in particular. This was a classic rescue mission where a deadline was looming. One sub-system written in C++ was in particularly bad shape, so I made the decision to redesign and reimplement it together with my team.

We did it by the book, aiming for healthy code and getting close to 100% code coverage thanks to our diligent TDD process. Once completed, we benchmarked against the system we replaced: our new and well-designed app ran 25% faster. Boom.

Historically, much code quality has been sacrificed on the altar of “performance”. So the approach I recommend is rather to:

      1. Focus on building well-designed code.

      2. Enable and utilize compilers for optimization rather than attempting micro-optimizations by hand (or via agents).

      3. Measure.

      4. If it misses your performance target, profile and optimize the hot path.


And in situations where performance is lacking, it’s always easier to optimize a program that you can understand. Often, the impactful optimizations are at the algorithmic level, and healthy code helps you spot those opportunities.

Adam Tornhill

Adam Tornhill is a programmer who combines degrees in engineering and psychology. He’s the founder and CTO of CodeScene where he designs tools for code analysis. Adam is also a recognized international speaker and the author of multiple technical books, including the best selling Your Code as a Crime Scene and Software Design X-Rays. Adam’s other interests include modern history, music, retro computing, and martial arts.

Keep reading

See more

Healthy code needs 22 to 35% fewer tokens for agentic code review

Does code quality affect AI token costs? A pre-registered study of 465 agent-authored pull requests links Code Health to...

Case Study: Refactoring at Scale with Agents

A legacy uplift that would traditionally have been a 12–18 month, high-risk engineering project was completed in three w...

Why AI Coding Agents Need a Deterministic Code Health Gate

The bottleneck is not writing code. The missing piece that AI agents need is an independent Code Health gate - a determi...