Skip to content
Permalink
Browse files
Don't prefetch attribute table sort values when no sorting set
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 4acc48058c60b58368bcee007173a5c3658ede63
Showing with 16 additions and 9 deletions.
  1. +16 −9 src/gui/attributetable/qgsattributetablemodel.cpp
@@ -39,7 +39,6 @@ QgsAttributeTableModel::QgsAttributeTableModel( QgsVectorLayerCache *layerCache,
: QAbstractTableModel( parent )
, mLayerCache( layerCache )
, mFieldCount( 0 )
, mSortCacheExpression( "" )
, mSortFieldIndex( -1 )
, mExtraColumns( 0 )
{
@@ -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 ) )
@@ -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;
@@ -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();

0 comments on commit 4acc480

Please sign in to comment.