Most Important C++ Optimizations

12 Jan 2023

[ c++  performance  interface  ]

General guidelines

1. Enable compiler optimisations

2. Set target architecture

3. Use fast math (faster computation, less precise results, non-standard compliant)

4. Disable exceptions and RTTI (limited performqance gains, brekas code using exceptions, non-standard compliant)

6. Use unity builds (multiple source files into single source file)

8. Use profile guided optimization

9. Try different compilers

10. Try different standard libraries

11. Keep your tools updated

12. Preload with a replacement lib

13. Use binary post-processing tools

14. Constexpr all the things

15. Make variables const

16. Noexcept all the things

17. Use static for internal linkage

18. Use [[noreturn]]

19. Use [[likely]] and [[unlikely]]

20. Use [[assume(condition)]]

21. Use __restrict

22. Make functions pure

23. Take parameters properly

24. Avoid allocations in loops (move objects out of loops)

25. Avoid copying exceptions (catch by reference, rethrow current exception)

26. Avoid copies in range-for (of iterated object)

27. Avoid copies in lambda captures

28. Avoid copies in structured bindings

29. Provide ref qualified methods

30. Keep the working set size small

31. Exploit data locality

32. Export temporal locality

33. Avoid false sharing

34. Use non temporal stores

35. Use indirected calls

36. Make branches predictable

37. Use branchless optimisations

38. Use SIMD intrinsics

References