From 35525eeedf917cfffac5a9ca2523acad31f62271 Mon Sep 17 00:00:00 2001 From: Kristian Bendiksen Date: Wed, 24 Feb 2021 11:49:21 +0100 Subject: [PATCH] #7400 StimPlanModel: Cache calculated results. --- .../StimPlanModel/RimStimPlanModel.cpp | 3 ++ .../RimStimPlanModelCalculator.cpp | 46 +++++++++++++++++-- .../RimStimPlanModelCalculator.h | 7 +++ 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/ApplicationLibCode/ProjectDataModel/StimPlanModel/RimStimPlanModel.cpp b/ApplicationLibCode/ProjectDataModel/StimPlanModel/RimStimPlanModel.cpp index 877b6fcf07..41ee1a7c7e 100644 --- a/ApplicationLibCode/ProjectDataModel/StimPlanModel/RimStimPlanModel.cpp +++ b/ApplicationLibCode/ProjectDataModel/StimPlanModel/RimStimPlanModel.cpp @@ -379,6 +379,7 @@ void RimStimPlanModel::fieldChangedByUi( const caf::PdmFieldHandle* changedField } else { + m_calculator->clearCache(); updateViewsAndPlots(); } } @@ -1692,6 +1693,8 @@ RimStimPlanModelTemplate* RimStimPlanModel::stimPlanModelTemplate() const //-------------------------------------------------------------------------------------------------- void RimStimPlanModel::stimPlanModelTemplateChanged( const caf::SignalEmitter* emitter ) { + m_calculator->clearCache(); + if ( m_stimPlanModelTemplate() ) { m_eclipseCase = m_stimPlanModelTemplate()->dynamicEclipseCase(); diff --git a/ApplicationLibCode/ProjectDataModel/StimPlanModel/RimStimPlanModelCalculator.cpp b/ApplicationLibCode/ProjectDataModel/StimPlanModel/RimStimPlanModelCalculator.cpp index 4e608e768b..5371acbaef 100644 --- a/ApplicationLibCode/ProjectDataModel/StimPlanModel/RimStimPlanModelCalculator.cpp +++ b/ApplicationLibCode/ProjectDataModel/StimPlanModel/RimStimPlanModelCalculator.cpp @@ -77,18 +77,56 @@ bool RimStimPlanModelCalculator::extractCurveData( RiaDefines::CurveProperty cur std::vector& tvDepthValues, double& rkbDiff ) const { - for ( const auto& calculator : m_resultCalculators ) + ResultKey key = std::make_pair( curveProperty, timeStep ); + auto data = m_resultCache.find( key ); + if ( data != m_resultCache.end() ) { - if ( calculator->isMatching( curveProperty ) ) + // Cache hit: reuse previous result + auto [cachedValues, cachedMeasuredDepthValues, cachedTvDepthValues, cachedRkbDiff] = data->second; + + values = cachedValues; + measuredDepthValues = cachedMeasuredDepthValues; + tvDepthValues = cachedTvDepthValues; + rkbDiff = cachedRkbDiff; + return true; + } + else + { + // Cache miss: try to calculate the request data + for ( const auto& calculator : m_resultCalculators ) { - return calculator - ->calculate( curveProperty, m_stimPlanModel, timeStep, values, measuredDepthValues, tvDepthValues, rkbDiff ); + if ( calculator->isMatching( curveProperty ) ) + { + bool isOk = calculator->calculate( curveProperty, + m_stimPlanModel, + timeStep, + values, + measuredDepthValues, + tvDepthValues, + rkbDiff ); + + if ( isOk ) + { + // Populate cache when calculation succeeds + m_resultCache[key] = std::make_tuple( values, measuredDepthValues, tvDepthValues, rkbDiff ); + } + + return isOk; + } } } return false; } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimStimPlanModelCalculator::clearCache() +{ + m_resultCache.clear(); +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationLibCode/ProjectDataModel/StimPlanModel/RimStimPlanModelCalculator.h b/ApplicationLibCode/ProjectDataModel/StimPlanModel/RimStimPlanModelCalculator.h index 3327b19522..2f4d106da4 100644 --- a/ApplicationLibCode/ProjectDataModel/StimPlanModel/RimStimPlanModelCalculator.h +++ b/ApplicationLibCode/ProjectDataModel/StimPlanModel/RimStimPlanModelCalculator.h @@ -21,6 +21,7 @@ #include "RimStimPlanModelPropertyCalculator.h" +#include #include #include @@ -66,6 +67,8 @@ public: void calculateTemperature( std::vector& temperatures ) const; + void clearCache(); + protected: std::vector findCurveAndComputeLayeredAverage( RiaDefines::CurveProperty curveProperty ) const; std::vector findCurveXValuesByProperty( RiaDefines::CurveProperty curveProperty ) const; @@ -93,4 +96,8 @@ protected: private: RimStimPlanModel* m_stimPlanModel; std::vector> m_resultCalculators; + + typedef std::pair ResultKey; + typedef std::tuple, std::vector, std::vector, double> ResultData; + mutable std::map m_resultCache; };