Skip to content

Commit cc87fb1

Browse files
committed
Utilise expression context cache to store some more expensive
expression calculation results (cherry-picked from 0f1c8df)
1 parent 7978bfd commit cc87fb1

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
@@ -3418,7 +3418,7 @@ static QVariant fcnGetFeatureById( const QVariantList &values, const QgsExpressi
34183418
return result;
34193419
}
34203420

3421-
static QVariant fcnGetFeature( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
3421+
static QVariant fcnGetFeature( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction * )
34223422
{
34233423
//arguments: 1. layer id / name, 2. key attribute, 3. eq value
34243424
QgsVectorLayer *vl = QgsExpressionUtils::getVectorLayer( values.at( 0 ), parent );
@@ -3437,6 +3437,13 @@ static QVariant fcnGetFeature( const QVariantList &values, const QgsExpressionCo
34373437
}
34383438

34393439
const QVariant &attVal = values.at( 2 );
3440+
3441+
const QString cacheValueKey = QStringLiteral( "getfeature:%1:%2:%3" ).arg( vl->id() ).arg( attributeId ).arg( attVal.toString() );
3442+
if ( context && context->hasCachedValue( cacheValueKey ) )
3443+
{
3444+
return context->cachedValue( cacheValueKey );
3445+
}
3446+
34403447
QgsFeatureRequest req;
34413448
req.setFilterExpression( QStringLiteral( "%1=%2" ).arg( QgsExpression::quotedColumnRef( attribute ),
34423449
QgsExpression::quotedString( attVal.toString() ) ) );
@@ -3448,10 +3455,15 @@ static QVariant fcnGetFeature( const QVariantList &values, const QgsExpressionCo
34483455
QgsFeatureIterator fIt = vl->getFeatures( req );
34493456

34503457
QgsFeature fet;
3458+
QVariant res;
34513459
if ( fIt.nextFeature( fet ) )
3452-
return QVariant::fromValue( fet );
3460+
{
3461+
res = QVariant::fromValue( fet );
3462+
}
34533463

3454-
return QVariant();
3464+
if ( context )
3465+
context->setCachedValue( cacheValueKey, res );
3466+
return res;
34553467
}
34563468

34573469
static QVariant fcnRepresentValue( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node )
@@ -3481,7 +3493,14 @@ static QVariant fcnRepresentValue( const QVariantList &values, const QgsExpressi
34813493
}
34823494
else
34833495
{
3484-
QgsVectorLayer *layer = QgsExpressionUtils::getVectorLayer( context->variable( "layer" ), parent );
3496+
QgsVectorLayer *layer = QgsExpressionUtils::getVectorLayer( context->variable( QStringLiteral( "layer" ) ), parent );
3497+
3498+
const QString cacheValueKey = QStringLiteral( "repvalfcnval:%1:%2:%3" ).arg( layer ? layer->id() : QStringLiteral( "[None]" ), fieldName, value.toString() );
3499+
if ( context->hasCachedValue( cacheValueKey ) )
3500+
{
3501+
return context->cachedValue( cacheValueKey );
3502+
}
3503+
34853504
const QgsEditorWidgetSetup setup = fields.at( fieldIndex ).editorWidgetSetup();
34863505
const QgsFieldFormatter *formatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );
34873506

@@ -3497,6 +3516,8 @@ static QVariant fcnRepresentValue( const QVariantList &values, const QgsExpressi
34973516
cache = context->cachedValue( cacheKey );
34983517

34993518
result = formatter->representValue( layer, fieldIndex, setup.config(), cache, value );
3519+
3520+
context->setCachedValue( cacheValueKey, result );
35003521
}
35013522
}
35023523
else

0 commit comments

Comments
 (0)