diff --git a/Tracking/TrkFitter/TrkiPatFitterUtils/CMakeLists.txt b/Tracking/TrkFitter/TrkiPatFitterUtils/CMakeLists.txt
index 7c71ce76de63e26fd9a2ce28c17cb63774ce2ae5..ce7d77bc9a6c9da6678f1634a3bc620e7223227d 100644
--- a/Tracking/TrkFitter/TrkiPatFitterUtils/CMakeLists.txt
+++ b/Tracking/TrkFitter/TrkiPatFitterUtils/CMakeLists.txt
@@ -1,4 +1,4 @@
-# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
+# Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
 
 # Declare the package name:
 atlas_subdir( TrkiPatFitterUtils )
@@ -23,3 +23,17 @@ atlas_add_library( TrkiPatFitterUtils
                    PRIVATE_DEFINITIONS ${CLHEP_DEFINITIONS}
                    LINK_LIBRARIES AthenaBaseComps AthContainers GeoPrimitives EventPrimitives GaudiKernel TrkEventPrimitives TrkGeometry TrkParameters
                    PRIVATE_LINK_LIBRARIES ${CLHEP_LIBRARIES} TrkSurfaces TrkMaterialOnTrack TrkMeasurementBase TrkTrack TrkExInterfaces TrkExUtils )
+
+
+# Code in this file makes heavy use of eigen and runs orders of magnitude
+# more slowly without optimization.  So force this to be optimized even
+# in debug builds.  If you need to debug it you might want to change this.
+# Specifying optimization via an attribute on the particular
+# function didn't work, because that still didn't allow inlining.
+if ( "${CMAKE_BUILD_TYPE}" STREQUAL "Debug" )
+  set_source_files_properties(
+     ${CMAKE_CURRENT_SOURCE_DIR}/src/FitMatrices.cxx
+     PROPERTIES
+     COMPILE_FLAGS "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}"
+     COMPILE_DEFINITIONS "FLATTEN" )
+endif()
diff --git a/Tracking/TrkFitter/TrkiPatFitterUtils/src/FitMatrices.cxx b/Tracking/TrkFitter/TrkiPatFitterUtils/src/FitMatrices.cxx
index 6f59e89cebc2ea23b67e590300ffc6e3c89b24c4..a3c837e2fd3802ca82adb1261d0937fc74144980 100755
--- a/Tracking/TrkFitter/TrkiPatFitterUtils/src/FitMatrices.cxx
+++ b/Tracking/TrkFitter/TrkiPatFitterUtils/src/FitMatrices.cxx
@@ -607,6 +607,14 @@ FitMatrices::setDimensions(std::vector<FitMeasurement*>& measurements,
   return fitCode;
 }
 
+#if defined(FLATTEN) && defined(__GNUC__)
+// We compile this package with optimization, even in debug builds; otherwise,
+// the heavy use of Eigen makes it too slow.  However, from here we may call
+// to out-of-line Eigen code that is linked from other DSOs; in that case,
+// it would not be optimized.  Avoid this by forcing all Eigen code
+// to be inlined here if possible.
+__attribute__ ((flatten))
+#endif
 bool
 FitMatrices::solveEquations(void)
 {