Skip to content

Commit c93d56a

Browse files
committedOct 28, 2016
Use QgsExpressionContextScope::addVariable instead of setVariable
...where appropriate (ie, read-only, non user set variables). It's much faster as it doesn't need to check whether the variable already exists. Results in ~10% improvement in rendering speed. Refs #15752. (cherry-picked from 8589788)
1 parent 5f01a87 commit c93d56a

11 files changed

+22
-22
lines changed
 

‎src/app/composer/qgsattributeselectiondialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static QgsExpressionContext _getExpressionContext( const void* context )
107107
}
108108

109109
QScopedPointer< QgsExpressionContext > expContext( object->createExpressionContext() );
110-
expContext->lastScope()->setVariable( "row_number", 1 );
110+
expContext->lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "row_number" ), 1, true ) );
111111
expContext->setHighlightedVariables( QStringList() << "row_number" );
112112
return QgsExpressionContext( *expContext );
113113
}

‎src/app/qgsattributetabledialog.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static QgsExpressionContext _getExpressionContext( const void* context )
5555
if ( layer )
5656
expContext << QgsExpressionContextUtils::layerScope( layer );
5757

58-
expContext.lastScope()->setVariable( "row_number", 1 );
58+
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "row_number" ), 1, true ) );
5959

6060
expContext.setHighlightedVariables( QStringList() << "row_number" );
6161

@@ -393,7 +393,7 @@ void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer* layer, const
393393
}
394394

395395
context.setFeature( feature );
396-
context.lastScope()->setVariable( QString( "row_number" ), rownum );
396+
context.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "row_number" ), rownum, true ) );
397397

398398
QVariant value = exp.evaluate( &context );
399399
fld.convertCompatible( value );

‎src/app/qgsfieldcalculator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer* vl, QWidget* parent )
4343
<< QgsExpressionContextUtils::projectScope()
4444
<< QgsExpressionContextUtils::layerScope( mVectorLayer );
4545

46-
expContext.lastScope()->setVariable( "row_number", 1 );
46+
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "row_number" ), 1, true ) );
4747
expContext.setHighlightedVariables( QStringList() << "row_number" );
4848

4949
builder->setLayer( vl );
@@ -281,7 +281,7 @@ void QgsFieldCalculator::accept()
281281
while ( fit.nextFeature( feature ) )
282282
{
283283
expContext.setFeature( feature );
284-
expContext.lastScope()->setVariable( QString( "row_number" ), rownum );
284+
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "row_number" ), rownum, true ) );
285285

286286
QVariant value = exp.evaluate( &expContext );
287287
if ( exp.hasEvalError() )

‎src/core/composer/qgscomposerattributetablev2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ bool QgsComposerAttributeTableV2::getTableContents( QgsComposerTableContents &co
503503
{
504504
// Lets assume it's an expression
505505
QgsExpression* expression = new QgsExpression(( *columnIt )->attribute() );
506-
context->lastScope()->setVariable( QString( "row_number" ), counter + 1 );
506+
context->lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "row_number" ), counter + 1, true ) );
507507
expression->prepare( context.data() );
508508
QVariant value = expression->evaluate( context.data() );
509509
currentRow << value;

‎src/core/composer/qgscomposermapgrid.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,8 +1502,8 @@ QString QgsComposerMapGrid::gridAnnotationString( double value, QgsComposerMapGr
15021502
}
15031503
else if ( mGridAnnotationFormat == CustomFormat )
15041504
{
1505-
expressionContext.lastScope()->setVariable( "grid_number", value );
1506-
expressionContext.lastScope()->setVariable( "grid_axis", coord == QgsComposerMapGrid::Longitude ? "x" : "y" );
1505+
expressionContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "grid_number" ), value, true ) );
1506+
expressionContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "grid_axis" ), coord == QgsComposerMapGrid::Longitude ? "x" : "y", true ) );
15071507
if ( !mGridAnnotationExpression.data() )
15081508
{
15091509
mGridAnnotationExpression.reset( new QgsExpression( mGridAnnotationExpressionString ) );
@@ -2238,8 +2238,8 @@ QgsExpressionContext* QgsComposerMapGrid::createExpressionContext() const
22382238
{
22392239
QgsExpressionContext* context = QgsComposerObject::createExpressionContext();
22402240
context->appendScope( new QgsExpressionContextScope( tr( "Grid" ) ) );
2241-
context->lastScope()->setVariable( "grid_number", 0 );
2242-
context->lastScope()->setVariable( "grid_axis", "x" );
2241+
context->lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "grid_number" ), 0, true ) );
2242+
context->lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "grid_axis" ), "x", true ) );
22432243
context->setHighlightedVariables( QStringList() << "grid_number" << "grid_axis" );
22442244
return context;
22452245
}

‎src/core/qgsconditionalstyle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ void QgsConditionalStyle::setSymbol( QgsSymbolV2* value )
195195
bool QgsConditionalStyle::matches( const QVariant& value, QgsExpressionContext& context ) const
196196
{
197197
QgsExpression exp( mRule );
198-
context.lastScope()->setVariable( "value", value );
198+
context.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "value" ), value, true ) );
199199
return exp.evaluate( &context ).toBool();
200200
}
201201

‎src/core/qgsexpression.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ static QVariant fcnFeature( const QVariantList&, const QgsExpressionContext* con
10271027
if ( !context )
10281028
return QVariant();
10291029

1030-
return context->variable( QgsExpressionContext::EXPR_FEATURE );
1030+
return context->feature();
10311031
}
10321032
static QVariant fcnAttribute( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
10331033
{
@@ -4313,7 +4313,7 @@ QVariant QgsExpression::NodeColumnRef::eval( QgsExpression *parent, const QgsExp
43134313

43144314
if ( context && context->hasVariable( QgsExpressionContext::EXPR_FEATURE ) )
43154315
{
4316-
QgsFeature feature = qvariant_cast<QgsFeature>( context->variable( QgsExpressionContext::EXPR_FEATURE ) );
4316+
QgsFeature feature = context->feature();
43174317
if ( index >= 0 )
43184318
return feature.attribute( index );
43194319
else

‎src/core/qgsexpressioncontext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,12 @@ void QgsExpressionContextScope::addFunction( const QString& name, QgsScopedExpre
191191

192192
void QgsExpressionContextScope::setFeature( const QgsFeature &feature )
193193
{
194-
setVariable( QgsExpressionContext::EXPR_FEATURE, QVariant::fromValue( feature ) );
194+
addVariable( StaticVariable( QgsExpressionContext::EXPR_FEATURE, QVariant::fromValue( feature ), true ) );
195195
}
196196

197197
void QgsExpressionContextScope::setFields( const QgsFields &fields )
198198
{
199-
setVariable( QgsExpressionContext::EXPR_FIELDS, QVariant::fromValue( fields ) );
199+
addVariable( StaticVariable( QgsExpressionContext::EXPR_FIELDS, QVariant::fromValue( fields ), true ) );
200200
}
201201

202202

‎src/core/symbology-ng/qgssymbolv2.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -738,8 +738,8 @@ void QgsSymbolV2::renderFeature( const QgsFeature& feature, QgsRenderContext& co
738738
{
739739
context.expressionContext().appendScope( mSymbolRenderContext->expressionContextScope() );
740740
QgsExpressionContextUtils::updateSymbolScope( this, mSymbolRenderContext->expressionContextScope() );
741-
mSymbolRenderContext->expressionContextScope()->setVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT, segmentizedGeometry->geometry()->partCount() );
742-
mSymbolRenderContext->expressionContextScope()->setVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, 1 );
741+
mSymbolRenderContext->expressionContextScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT, segmentizedGeometry->geometryPartCount(), true ) );
742+
mSymbolRenderContext->expressionContextScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, 1, true ) );
743743
}
744744

745745
switch ( QgsWKBTypes::flatType( segmentizedGeometry->geometry()->wkbType() ) )
@@ -806,7 +806,7 @@ void QgsSymbolV2::renderFeature( const QgsFeature& feature, QgsRenderContext& co
806806

807807
for ( int i = 0; i < mp->numGeometries(); ++i )
808808
{
809-
mSymbolRenderContext->expressionContextScope()->setVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, i + 1 );
809+
mSymbolRenderContext->expressionContextScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, i + 1, true ) );
810810

811811
const QgsPointV2* point = static_cast< const QgsPointV2* >( mp->geometryN( i ) );
812812
_getPoint( pt, context, point );
@@ -836,7 +836,7 @@ void QgsSymbolV2::renderFeature( const QgsFeature& feature, QgsRenderContext& co
836836

837837
for ( unsigned int i = 0; i < num && wkbPtr; ++i )
838838
{
839-
mSymbolRenderContext->expressionContextScope()->setVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, i + 1 );
839+
mSymbolRenderContext->expressionContextScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, i + 1, true ) );
840840

841841
if ( geomCollection )
842842
{
@@ -870,7 +870,7 @@ void QgsSymbolV2::renderFeature( const QgsFeature& feature, QgsRenderContext& co
870870

871871
for ( unsigned int i = 0; i < num && wkbPtr; ++i )
872872
{
873-
mSymbolRenderContext->expressionContextScope()->setVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, i + 1 );
873+
mSymbolRenderContext->expressionContextScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, i + 1, true ) );
874874

875875
if ( geomCollection )
876876
{

‎src/gui/attributetable/qgsfieldconditionalformatwidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void QgsFieldConditionalFormatWidget::setExpression()
6363
context << QgsExpressionContextUtils::globalScope()
6464
<< QgsExpressionContextUtils::projectScope()
6565
<< QgsExpressionContextUtils::layerScope( mLayer );
66-
context.lastScope()->setVariable( "value", 0 );
66+
context.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "value" ), 0, true ) );
6767
context.setHighlightedVariables( QStringList() << "value" );
6868

6969
QgsExpressionBuilderDialog dlg( mLayer, mRuleEdit->text(), this, "generic", context );

‎src/gui/symbology-ng/qgssymbollayerv2widget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static QgsExpressionContext _getExpressionContext( const void* context )
8080
{
8181
//cheat a bit - set the symbol color variable to match the symbol layer's color (when we should really be using the *symbols*
8282
//color, but that's not accessible here). 99% of the time these will be the same anyway
83-
symbolScope->setVariable( QgsExpressionContext::EXPR_SYMBOL_COLOR, symbolLayer->color() );
83+
symbolScope->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_SYMBOL_COLOR, symbolLayer->color(), true ) );
8484
}
8585
expContext << symbolScope;
8686
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT, 1, true ) );

0 commit comments

Comments
 (0)
Please sign in to comment.