Hasty Briefsbeta

Bilingual

"Clean" Code, Horrible Performance

5 hours ago
  • Many 'clean code' rules, such as preferring polymorphism over switch statements, lead to significant performance degradation (1.5x to 15x slower).
  • Using a class hierarchy with virtual functions introduces indirection, pointer arrays, and hides code patterns from the compiler, causing poor optimization.
  • Replacing polymorphism with a switch statement on a flat struct union improved performance by 1.5x; further using a lookup table (data-driven approach) achieved 10x speedup.
  • Adding more properties (like corner count) to the problem widened the performance gap: clean code became 15x slower than the table-driven version.
  • The compiler cannot optimize code that is fragmented across translation units and behind virtual calls, limiting both manual and automatic optimization.
  • AVX-optimized code (which violates clean code principles) achieved 20-25x speedup over the clean code version.
  • Rules like 'DRY' are reasonable, but the four structural rules (polymorphism, no internal knowledge, small functions, single responsibility) are harmful to performance.
  • The author argues that clean code methodology can erase 12-14 years of hardware progress, and that maintainability must be weighed against such extreme performance costs.

Related

Loading…