Skip to content
Permalink
Browse files
Greatly speed up attribute table loading
Don't advise for rows added when a model reset is in progress.
Otherwise the rows are tested for sort order, etc triggering
a bunch of useless calculations, given that the model is in
the process of being reset anyway.

Tested using a 150k point shapefile, decreased attribute table
load times from 50+ seconds to 4 seconds.

Refs #16577, #16239
  • Loading branch information
nyalldawson committed May 22, 2017
1 parent af8fb04 commit b97a980b99a32f7cbbb8cc32ac6a781246df1171
Showing with 7 additions and 5 deletions.
  1. +6 −4 src/gui/attributetable/qgsattributetablemodel.cpp
  2. +1 −1 src/gui/attributetable/qgsattributetablemodel.h
@@ -197,7 +197,7 @@ bool QgsAttributeTableModel::removeRows( int row, int count, const QModelIndex &
return true;
}

void QgsAttributeTableModel::featureAdded( QgsFeatureId fid )
void QgsAttributeTableModel::featureAdded( QgsFeatureId fid , bool resettingModel )
{
QgsDebugMsgLevel( QString( "(%2) fid: %1" ).arg( fid ).arg( mFeatureRequest.filterType() ), 4 );
bool featOk = true;
@@ -225,10 +225,12 @@ void QgsAttributeTableModel::featureAdded( QgsFeatureId fid )
if ( ! mIdRowMap.contains( fid ) )
{
int n = mRowIdMap.size();
beginInsertRows( QModelIndex(), n, n );
if ( !resettingModel )
beginInsertRows( QModelIndex(), n, n );
mIdRowMap.insert( fid, n );
mRowIdMap.insert( n, fid );
endInsertRows();
if ( !resettingModel )
endInsertRows();
reload( index( rowCount() - 1, 0 ), index( rowCount() - 1, columnCount() ) );
}
}
@@ -412,7 +414,7 @@ void QgsAttributeTableModel::loadLayer()

t.restart();
}
featureAdded( mFeat.id() );
featureAdded( mFeat.id(), true );
}

emit finished();
@@ -307,7 +307,7 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
* Launched when a feature has been added
* @param fid feature id
*/
virtual void featureAdded( QgsFeatureId fid );
virtual void featureAdded( QgsFeatureId fid, bool resettingModel = false );

/**
* Launched when layer has been deleted

7 comments on commit b97a980

@NathanW2

This comment has been minimized.

Copy link
Member

@NathanW2 NathanW2 replied May 22, 2017

Win!

@m-kuhn

This comment has been minimized.

Copy link
Member

@m-kuhn m-kuhn replied May 22, 2017

Nice

@elpaso

This comment has been minimized.

Copy link
Contributor

@elpaso elpaso replied May 22, 2017

Nice!

@luipir

This comment has been minimized.

Copy link
Contributor

@luipir luipir replied May 22, 2017

elegant solution, but some questions:
when featureAdded would be called to trigger model reset? the end of editing? the end of coping, the end of something. Is it a way to move control outside the the MV qt model to a business function level?

@nyalldawson

This comment has been minimized.

Copy link
Collaborator Author

@nyalldawson nyalldawson replied May 22, 2017

feature added is used in two places - one is when the table is already visible and a new feature is actually added to the layer. The other is for every feature added to the table when the table is being loaded. This is when it's being used whole the model is being reset.

I'd say it's more of a workaround qt's model handling. I think any rowsAboutToBeInserted, ... should always be ignored if a model is in the process of being reset. But it seems this isn't the case, so adding the thousands of features when loading the table triggers a whole bunch of expensive sort handling after every feature is added.

@luipir

This comment has been minimized.

Copy link
Contributor

@luipir luipir replied May 22, 2017

+1 for me :)

@gioman

This comment has been minimized.

Copy link
Contributor

@gioman gioman replied May 22, 2017

Hi @nyalldawson

b) 2.18.7

rendering time:
real 0m25.341s
user 0m19.684s
sys 0m1.120s

table
in 9:30 minutes it loadaed about half of ~758k records (the I aborted)

Please test b97a980

  • I think this should fix it.

with your patch

2.18.8

rendering time:
real 0m38.663s
user 0m33.572s
sys 0m1.244s

so... quite slower in rendering my test 758k ploygon layer.

table:
it took 27 seconds to open the whole table, which is 10 seconds slower
than on 2.14.4

cheers!

Please sign in to comment.