From e862beae260aaf41495352bd16af1e871089f9b6 Mon Sep 17 00:00:00 2001
From: Vickie Lynch <lynchve@ornl.gov>
Date: Fri, 18 Jan 2019 11:00:17 -0500
Subject: [PATCH] Refs #24333 over a factor of 10 faster

---
 .../Crystal/inc/MantidCrystal/PeakHKLErrors.h |  6 ++++
 Framework/Crystal/src/PeakHKLErrors.cpp       | 36 ++++++++++---------
 2 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/Framework/Crystal/inc/MantidCrystal/PeakHKLErrors.h b/Framework/Crystal/inc/MantidCrystal/PeakHKLErrors.h
index 5d06df47752..4a7666de583 100644
--- a/Framework/Crystal/inc/MantidCrystal/PeakHKLErrors.h
+++ b/Framework/Crystal/inc/MantidCrystal/PeakHKLErrors.h
@@ -153,6 +153,12 @@ private:
                 // OptRuns setup
 
   void setUpOptRuns();
+
+  mutable boost::shared_ptr<Geometry::Instrument> instChange;
+  mutable bool hasParameterMap = false;
+  mutable Kernel::V3D sampPos;
+  mutable boost::shared_ptr<const Geometry::ParameterMap> pmapSv;
+
 };
 } // namespace Crystal
 } // namespace Mantid
diff --git a/Framework/Crystal/src/PeakHKLErrors.cpp b/Framework/Crystal/src/PeakHKLErrors.cpp
index 3edfe24e37f..51ebf414e0a 100644
--- a/Framework/Crystal/src/PeakHKLErrors.cpp
+++ b/Framework/Crystal/src/PeakHKLErrors.cpp
@@ -190,26 +190,31 @@ boost::shared_ptr<Geometry::Instrument>
 PeakHKLErrors::getNewInstrument(PeaksWorkspace_sptr Peaks) const {
   Geometry::Instrument_const_sptr instSave = Peaks->getPeak(0).getInstrument();
   auto pmap = boost::make_shared<Geometry::ParameterMap>();
-  boost::shared_ptr<const Geometry::ParameterMap> pmapSv =
-      instSave->getParameterMap();
 
   if (!instSave) {
     g_log.error(" Peaks workspace does not have an instrument");
     throw std::invalid_argument(" Not all peaks have an instrument");
   }
-  auto instChange = boost::shared_ptr<Geometry::Instrument>();
 
-  if (!instSave->isParametrized()) {
-
-    boost::shared_ptr<Geometry::Instrument> instClone(instSave->clone());
-    auto Pinsta = boost::make_shared<Geometry::Instrument>(instSave, pmap);
-
-    instChange = Pinsta;
-  } else // catch(... )
-  {
-    auto P1 = boost::make_shared<Geometry::Instrument>(
-        instSave->baseInstrument(), instSave->makeLegacyParameterMap());
-    instChange = P1;
+  if (!hasParameterMap) {
+    pmapSv = instSave->getParameterMap();
+    hasParameterMap = true;
+    if (!instSave->isParametrized()) {
+  
+      boost::shared_ptr<Geometry::Instrument> instClone(instSave->clone());
+      auto Pinsta = boost::make_shared<Geometry::Instrument>(instSave, pmap);
+  
+      instChange = Pinsta;
+      IComponent_const_sptr sample = instChange->getSample();
+      sampPos = sample->getRelativePos();
+    } else // catch(... )
+    {
+      auto P1 = boost::make_shared<Geometry::Instrument>(
+          instSave->baseInstrument(), instSave->makeLegacyParameterMap());
+      instChange = P1;
+      IComponent_const_sptr sample = instChange->getSample();
+      sampPos = sample->getRelativePos();
+    }
   }
 
   if (!instChange) {
@@ -219,11 +224,10 @@ PeakHKLErrors::getNewInstrument(PeaksWorkspace_sptr Peaks) const {
   //------------------"clone" orig instruments pmap -------------------
 
   cLone(pmap, instSave, pmapSv);
-  IComponent_const_sptr sample = instChange->getSample();
-  V3D sampPos = sample->getRelativePos();
   V3D sampOffsets(getParameter("SampleXOffset"), getParameter("SampleYOffset"),
                   getParameter("SampleZOffset"));
 
+  IComponent_const_sptr sample = instChange->getSample();
   pmap->addPositionCoordinate(sample.get(), std::string("x"),
                               sampPos.X() + sampOffsets.X());
   pmap->addPositionCoordinate(sample.get(), std::string("y"),
-- 
GitLab