Skip to content
Permalink
Browse files

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)
  • Loading branch information
nyalldawson committed Oct 28, 2016
1 parent 0af7331 commit ee4acb0b30fbb4e026a8313f1b8c432a2b9bf771
@@ -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 );
}
@@ -58,7 +58,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" );

@@ -472,7 +472,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 );
@@ -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 );
@@ -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() )
@@ -149,8 +149,8 @@ bool QgsMapToolFeatureAction::doAction( QgsVectorLayer *layer, int x, int y )
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::mapSettingsScope( mCanvas->mapSettings() );
QgsExpressionContextScope* actionScope = new QgsExpressionContextScope();
actionScope->setVariable( "click_x", point.x() );
actionScope->setVariable( "click_y", point.y() );
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QString( "click_x" ), point.x(), true ) );
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QString( "click_y" ), point.y(), true ) );
context << actionScope;

int actionIdx = layer->actions()->defaultAction();
@@ -557,7 +557,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;
@@ -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 ) );
@@ -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;
}
@@ -67,7 +67,7 @@ void QgsActionManager::doAction( int index, const QgsFeature& feat, int defaultV
{
QgsExpressionContext context = createExpressionContext();
QgsExpressionContextScope* actionScope = new QgsExpressionContextScope();
actionScope->setVariable( "current_field", feat.attribute( defaultValueIndex ) );
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QString( "current_field" ), feat.attribute( defaultValueIndex ), true ) );
context << actionScope;
doAction( index, feat, context );
}
@@ -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();
}

@@ -1348,7 +1348,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 )
{
@@ -4825,7 +4825,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
@@ -193,12 +193,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 ) );
}


@@ -691,8 +691,8 @@ void QgsArrowSymbolLayer::renderPolyline( const QPolygonF& points, QgsSymbolV2Re
}

context.renderContext().expressionContext().appendScope( mExpressionScope.data() );
mExpressionScope->setVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT, points.size() + 1 );
mExpressionScope->setVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM, 1 );
mExpressionScope->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT, points.size() + 1, true ) );
mExpressionScope->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM, 1, true ) );
if ( isCurved() )
{
_resolveDataDefined( context );
@@ -727,7 +727,7 @@ void QgsArrowSymbolLayer::renderPolyline( const QPolygonF& points, QgsSymbolV2Re
{
for ( int pIdx = 0; pIdx < points.size() - 1; pIdx += 2 )
{
mExpressionScope->setVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM, pIdx + 1 );
mExpressionScope->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM, pIdx + 1, true ) );
_resolveDataDefined( context );

if ( points.size() - pIdx >= 3 )
@@ -778,7 +778,7 @@ void QgsArrowSymbolLayer::renderPolyline( const QPolygonF& points, QgsSymbolV2Re
// only straight arrows
for ( int pIdx = 0; pIdx < points.size() - 1; pIdx++ )
{
mExpressionScope->setVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM, pIdx + 1 );
mExpressionScope->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM, pIdx + 1, true ) );
_resolveDataDefined( context );

// origin point
@@ -1010,7 +1010,7 @@ void QgsMarkerLineSymbolLayerV2::renderPolylineInterval( const QPolygonF& points
// "c" is 1 for regular point or in interval (0,1] for begin of line segment
lastPt += c * diff;
lengthLeft -= painterUnitInterval;
scope->setVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM, ++pointNum );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM, ++pointNum, true ) );
mMarker->renderPoint( lastPt, context.feature(), rc, -1, context.selected() );
c = 1; // reset c (if wasn't 1 already)
}
@@ -1044,7 +1044,7 @@ void QgsMarkerLineSymbolLayerV2::renderPolylineVertex( const QPolygonF& points,

QgsExpressionContextScope* scope = new QgsExpressionContextScope();
context.renderContext().expressionContext().appendScope( scope );
scope->setVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT, points.size() );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT, points.size(), true ) );

double offsetAlongLine = mOffsetAlongLine;
if ( hasDataDefinedProperty( QgsSymbolLayerV2::EXPR_OFFSET_ALONG_LINE ) )
@@ -1071,7 +1071,7 @@ void QgsMarkerLineSymbolLayerV2::renderPolylineVertex( const QPolygonF& points,
int pointNum = 0;
while ( context.renderContext().geometry()->nextVertex( vId, vPoint ) )
{
scope->setVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM, ++pointNum );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM, ++pointNum, true ) );

if (( placement == Vertex && vId.type == QgsVertexId::SegmentVertex )
|| ( placement == CurvePoint && vId.type == QgsVertexId::CurveVertex ) )
@@ -1137,7 +1137,7 @@ void QgsMarkerLineSymbolLayerV2::renderPolylineVertex( const QPolygonF& points,
int pointNum = 0;
for ( ; i < maxCount; ++i )
{
scope->setVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM, ++pointNum );
scope->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM, ++pointNum, true ) );

if ( isRing && placement == Vertex && i == points.count() - 1 )
{
@@ -732,8 +732,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, mSymbolRenderContext->geometryPartCount() );
mSymbolRenderContext->expressionContextScope()->setVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, 1 );
mSymbolRenderContext->expressionContextScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT, mSymbolRenderContext->geometryPartCount(), true ) );
mSymbolRenderContext->expressionContextScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, 1, true ) );
}

// Collection of markers to paint, only used for no curve types.
@@ -831,7 +831,7 @@ void QgsSymbolV2::renderFeature( const QgsFeature& feature, QgsRenderContext& co
for ( int i = 0; i < mp->numGeometries(); ++i )
{
mSymbolRenderContext->setGeometryPartNum( i + 1 );
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 );
@@ -867,7 +867,7 @@ void QgsSymbolV2::renderFeature( const QgsFeature& feature, QgsRenderContext& co
for ( unsigned int i = 0; i < num && wkbPtr; ++i )
{
mSymbolRenderContext->setGeometryPartNum( i + 1 );
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 )
{
@@ -917,7 +917,7 @@ void QgsSymbolV2::renderFeature( const QgsFeature& feature, QgsRenderContext& co
for ( unsigned int i = 0; i < num && wkbPtr; ++i )
{
mSymbolRenderContext->setGeometryPartNum( i + 1 );
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 )
{
@@ -77,7 +77,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 );
@@ -81,7 +81,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 ) );

0 comments on commit ee4acb0

Please sign in to comment.
You can’t perform that action at this time.