Skip to content

Commit

Permalink
Use QgsExpressionContextScope::addVariable instead of setVariable
Browse files Browse the repository at this point in the history
...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)
  • Loading branch information
nyalldawson committed Oct 28, 2016
1 parent 5f01a87 commit c93d56a
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/app/composer/qgsattributeselectiondialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static QgsExpressionContext _getExpressionContext( const void* context )
}

QScopedPointer< QgsExpressionContext > expContext( object->createExpressionContext() );
expContext->lastScope()->setVariable( "row_number", 1 );
expContext->lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "row_number" ), 1, true ) );
expContext->setHighlightedVariables( QStringList() << "row_number" );
return QgsExpressionContext( *expContext );
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsattributetabledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static QgsExpressionContext _getExpressionContext( const void* context )
if ( layer )
expContext << QgsExpressionContextUtils::layerScope( layer );

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

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

Expand Down Expand Up @@ -393,7 +393,7 @@ void QgsAttributeTableDialog::runFieldCalculation( QgsVectorLayer* layer, const
}

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

QVariant value = exp.evaluate( &context );
fld.convertCompatible( value );
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsfieldcalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer* vl, QWidget* parent )
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mVectorLayer );

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

builder->setLayer( vl );
Expand Down Expand Up @@ -281,7 +281,7 @@ void QgsFieldCalculator::accept()
while ( fit.nextFeature( feature ) )
{
expContext.setFeature( feature );
expContext.lastScope()->setVariable( QString( "row_number" ), rownum );
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "row_number" ), rownum, true ) );

QVariant value = exp.evaluate( &expContext );
if ( exp.hasEvalError() )
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerattributetablev2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ bool QgsComposerAttributeTableV2::getTableContents( QgsComposerTableContents &co
{
// Lets assume it's an expression
QgsExpression* expression = new QgsExpression(( *columnIt )->attribute() );
context->lastScope()->setVariable( QString( "row_number" ), counter + 1 );
context->lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "row_number" ), counter + 1, true ) );
expression->prepare( context.data() );
QVariant value = expression->evaluate( context.data() );
currentRow << value;
Expand Down
8 changes: 4 additions & 4 deletions src/core/composer/qgscomposermapgrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1502,8 +1502,8 @@ QString QgsComposerMapGrid::gridAnnotationString( double value, QgsComposerMapGr
}
else if ( mGridAnnotationFormat == CustomFormat )
{
expressionContext.lastScope()->setVariable( "grid_number", value );
expressionContext.lastScope()->setVariable( "grid_axis", coord == QgsComposerMapGrid::Longitude ? "x" : "y" );
expressionContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "grid_number" ), value, true ) );
expressionContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "grid_axis" ), coord == QgsComposerMapGrid::Longitude ? "x" : "y", true ) );
if ( !mGridAnnotationExpression.data() )
{
mGridAnnotationExpression.reset( new QgsExpression( mGridAnnotationExpressionString ) );
Expand Down Expand Up @@ -2238,8 +2238,8 @@ QgsExpressionContext* QgsComposerMapGrid::createExpressionContext() const
{
QgsExpressionContext* context = QgsComposerObject::createExpressionContext();
context->appendScope( new QgsExpressionContextScope( tr( "Grid" ) ) );
context->lastScope()->setVariable( "grid_number", 0 );
context->lastScope()->setVariable( "grid_axis", "x" );
context->lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "grid_number" ), 0, true ) );
context->lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "grid_axis" ), "x", true ) );
context->setHighlightedVariables( QStringList() << "grid_number" << "grid_axis" );
return context;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsconditionalstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ void QgsConditionalStyle::setSymbol( QgsSymbolV2* value )
bool QgsConditionalStyle::matches( const QVariant& value, QgsExpressionContext& context ) const
{
QgsExpression exp( mRule );
context.lastScope()->setVariable( "value", value );
context.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "value" ), value, true ) );
return exp.evaluate( &context ).toBool();
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ static QVariant fcnFeature( const QVariantList&, const QgsExpressionContext* con
if ( !context )
return QVariant();

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

if ( context && context->hasVariable( QgsExpressionContext::EXPR_FEATURE ) )
{
QgsFeature feature = qvariant_cast<QgsFeature>( context->variable( QgsExpressionContext::EXPR_FEATURE ) );
QgsFeature feature = context->feature();
if ( index >= 0 )
return feature.attribute( index );
else
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsexpressioncontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ void QgsExpressionContextScope::addFunction( const QString& name, QgsScopedExpre

void QgsExpressionContextScope::setFeature( const QgsFeature &feature )
{
setVariable( QgsExpressionContext::EXPR_FEATURE, QVariant::fromValue( feature ) );
addVariable( StaticVariable( QgsExpressionContext::EXPR_FEATURE, QVariant::fromValue( feature ), true ) );
}

void QgsExpressionContextScope::setFields( const QgsFields &fields )
{
setVariable( QgsExpressionContext::EXPR_FIELDS, QVariant::fromValue( fields ) );
addVariable( StaticVariable( QgsExpressionContext::EXPR_FIELDS, QVariant::fromValue( fields ), true ) );
}


Expand Down
10 changes: 5 additions & 5 deletions src/core/symbology-ng/qgssymbolv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,8 +738,8 @@ void QgsSymbolV2::renderFeature( const QgsFeature& feature, QgsRenderContext& co
{
context.expressionContext().appendScope( mSymbolRenderContext->expressionContextScope() );
QgsExpressionContextUtils::updateSymbolScope( this, mSymbolRenderContext->expressionContextScope() );
mSymbolRenderContext->expressionContextScope()->setVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT, segmentizedGeometry->geometry()->partCount() );
mSymbolRenderContext->expressionContextScope()->setVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, 1 );
mSymbolRenderContext->expressionContextScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT, segmentizedGeometry->geometryPartCount(), true ) );
mSymbolRenderContext->expressionContextScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, 1, true ) );
}

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

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

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

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

if ( geomCollection )
{
Expand Down Expand Up @@ -870,7 +870,7 @@ void QgsSymbolV2::renderFeature( const QgsFeature& feature, QgsRenderContext& co

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

if ( geomCollection )
{
Expand Down
2 changes: 1 addition & 1 deletion src/gui/attributetable/qgsfieldconditionalformatwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void QgsFieldConditionalFormatWidget::setExpression()
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mLayer );
context.lastScope()->setVariable( "value", 0 );
context.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QString( "value" ), 0, true ) );
context.setHighlightedVariables( QStringList() << "value" );

QgsExpressionBuilderDialog dlg( mLayer, mRuleEdit->text(), this, "generic", context );
Expand Down
2 changes: 1 addition & 1 deletion src/gui/symbology-ng/qgssymbollayerv2widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static QgsExpressionContext _getExpressionContext( const void* context )
{
//cheat a bit - set the symbol color variable to match the symbol layer's color (when we should really be using the *symbols*
//color, but that's not accessible here). 99% of the time these will be the same anyway
symbolScope->setVariable( QgsExpressionContext::EXPR_SYMBOL_COLOR, symbolLayer->color() );
symbolScope->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_SYMBOL_COLOR, symbolLayer->color(), true ) );
}
expContext << symbolScope;
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT, 1, true ) );
Expand Down

0 comments on commit c93d56a

Please sign in to comment.