Skip to content

Commit 0f1c8df

Browse files
committed
Utilise expression context cache to store some more expensive
expression calculation results
1 parent 222977f commit 0f1c8df

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/core/expression/qgsexpressionfunction.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3462,7 +3462,7 @@ static QVariant fcnGetFeatureById( const QVariantList &values, const QgsExpressi
34623462
return result;
34633463
}
34643464

3465-
static QVariant fcnGetFeature( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
3465+
static QVariant fcnGetFeature( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction * )
34663466
{
34673467
//arguments: 1. layer id / name, 2. key attribute, 3. eq value
34683468
QgsVectorLayer *vl = QgsExpressionUtils::getVectorLayer( values.at( 0 ), parent );
@@ -3481,6 +3481,13 @@ static QVariant fcnGetFeature( const QVariantList &values, const QgsExpressionCo
34813481
}
34823482

34833483
const QVariant &attVal = values.at( 2 );
3484+
3485+
const QString cacheValueKey = QStringLiteral( "getfeature:%1:%2:%3" ).arg( vl->id() ).arg( attributeId ).arg( attVal.toString() );
3486+
if ( context && context->hasCachedValue( cacheValueKey ) )
3487+
{
3488+
return context->cachedValue( cacheValueKey );
3489+
}
3490+
34843491
QgsFeatureRequest req;
34853492
req.setFilterExpression( QStringLiteral( "%1=%2" ).arg( QgsExpression::quotedColumnRef( attribute ),
34863493
QgsExpression::quotedString( attVal.toString() ) ) );
@@ -3492,10 +3499,15 @@ static QVariant fcnGetFeature( const QVariantList &values, const QgsExpressionCo
34923499
QgsFeatureIterator fIt = vl->getFeatures( req );
34933500

34943501
QgsFeature fet;
3502+
QVariant res;
34953503
if ( fIt.nextFeature( fet ) )
3496-
return QVariant::fromValue( fet );
3504+
{
3505+
res = QVariant::fromValue( fet );
3506+
}
34973507

3498-
return QVariant();
3508+
if ( context )
3509+
context->setCachedValue( cacheValueKey, res );
3510+
return res;
34993511
}
35003512

35013513
static QVariant fcnRepresentValue( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node )
@@ -3525,7 +3537,14 @@ static QVariant fcnRepresentValue( const QVariantList &values, const QgsExpressi
35253537
}
35263538
else
35273539
{
3528-
QgsVectorLayer *layer = QgsExpressionUtils::getVectorLayer( context->variable( "layer" ), parent );
3540+
QgsVectorLayer *layer = QgsExpressionUtils::getVectorLayer( context->variable( QStringLiteral( "layer" ) ), parent );
3541+
3542+
const QString cacheValueKey = QStringLiteral( "repvalfcnval:%1:%2:%3" ).arg( layer ? layer->id() : QStringLiteral( "[None]" ), fieldName, value.toString() );
3543+
if ( context->hasCachedValue( cacheValueKey ) )
3544+
{
3545+
return context->cachedValue( cacheValueKey );
3546+
}
3547+
35293548
const QgsEditorWidgetSetup setup = fields.at( fieldIndex ).editorWidgetSetup();
35303549
const QgsFieldFormatter *formatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );
35313550

@@ -3541,6 +3560,8 @@ static QVariant fcnRepresentValue( const QVariantList &values, const QgsExpressi
35413560
cache = context->cachedValue( cacheKey );
35423561

35433562
result = formatter->representValue( layer, fieldIndex, setup.config(), cache, value );
3563+
3564+
context->setCachedValue( cacheValueKey, result );
35443565
}
35453566
}
35463567
else

0 commit comments

Comments
 (0)