Skip to content

Commit

Permalink
Don't prefetch attribute table sort values when no sorting set
Browse files Browse the repository at this point in the history
Shaves some seconds off opening the attribute table in certain
circumstances (no sorting applied)

Drops load time for table from 100 seconds to 50 seconds for a
2.6 million feature shapefile, and from 6.5 seconds to 3.5 seconds
for a 160k feature shapefile.

Refs #16577, #16239

(cherry-picked from 0b95c77)
  • Loading branch information
nyalldawson committed May 23, 2017
1 parent 6c126a3 commit 4acc480
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/gui/attributetable/qgsattributetablemodel.cpp
Expand Up @@ -39,7 +39,6 @@ QgsAttributeTableModel::QgsAttributeTableModel( QgsVectorLayerCache *layerCache,
: QAbstractTableModel( parent )
, mLayerCache( layerCache )
, mFieldCount( 0 )
, mSortCacheExpression( "" )
, mSortFieldIndex( -1 )
, mExtraColumns( 0 )
{
Expand Down Expand Up @@ -207,19 +206,20 @@ void QgsAttributeTableModel::featureAdded( QgsFeatureId fid , bool resettingMode

if ( featOk && mFeatureRequest.acceptFeature( mFeat ) )
{
if ( mSortFieldIndex == -1 )
{
mExpressionContext.setFeature( mFeat );
mSortCache[mFeat.id()] = mSortCacheExpression.evaluate( &mExpressionContext );
}
else

if ( mSortFieldIndex >= 0 )
{
QgsEditorWidgetFactory* widgetFactory = mWidgetFactories.at( mSortFieldIndex );
const QVariant& widgetCache = mAttributeWidgetCaches.at( mSortFieldIndex );
const QgsEditorWidgetConfig& widgetConfig = mWidgetConfigs.at( mSortFieldIndex );
QVariant sortValue = widgetFactory->representValue( layer(), mSortFieldIndex, widgetConfig, widgetCache, mFeat.attribute( mSortFieldIndex ) );
mSortCache.insert( mFeat.id(), sortValue );
}
else if ( mSortCacheExpression.isValid() )
{
mExpressionContext.setFeature( mFeat );
mSortCache[mFeat.id()] = mSortCacheExpression.evaluate( &mExpressionContext );
}

// Skip if the fid is already in the map (do not add twice)!
if ( ! mIdRowMap.contains( fid ) )
Expand Down Expand Up @@ -780,7 +780,14 @@ void QgsAttributeTableModel::prefetchSortData( const QString& expressionString )
mSortCache.clear();
mSortCacheAttributes.clear();
mSortFieldIndex = -1;
mSortCacheExpression = QgsExpression( expressionString );
if ( !expressionString.isEmpty() )
mSortCacheExpression = QgsExpression( expressionString );
else
{
// no sorting
mSortCacheExpression = QgsExpression();
return;
}

QgsEditorWidgetFactory* widgetFactory = nullptr;
QVariant widgetCache;
Expand Down Expand Up @@ -833,7 +840,7 @@ void QgsAttributeTableModel::prefetchSortData( const QString& expressionString )

QString QgsAttributeTableModel::sortCacheExpression() const
{
if ( mSortCacheExpression.rootNode() )
if ( mSortCacheExpression.isValid() )
return mSortCacheExpression.expression();
else
return QString();
Expand Down

0 comments on commit 4acc480

Please sign in to comment.