Skip to content

Commit b97a980

Browse files
committedMay 22, 2017
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
1 parent af8fb04 commit b97a980

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed
 

‎src/gui/attributetable/qgsattributetablemodel.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ bool QgsAttributeTableModel::removeRows( int row, int count, const QModelIndex &
197197
return true;
198198
}
199199

200-
void QgsAttributeTableModel::featureAdded( QgsFeatureId fid )
200+
void QgsAttributeTableModel::featureAdded( QgsFeatureId fid , bool resettingModel )
201201
{
202202
QgsDebugMsgLevel( QString( "(%2) fid: %1" ).arg( fid ).arg( mFeatureRequest.filterType() ), 4 );
203203
bool featOk = true;
@@ -225,10 +225,12 @@ void QgsAttributeTableModel::featureAdded( QgsFeatureId fid )
225225
if ( ! mIdRowMap.contains( fid ) )
226226
{
227227
int n = mRowIdMap.size();
228-
beginInsertRows( QModelIndex(), n, n );
228+
if ( !resettingModel )
229+
beginInsertRows( QModelIndex(), n, n );
229230
mIdRowMap.insert( fid, n );
230231
mRowIdMap.insert( n, fid );
231-
endInsertRows();
232+
if ( !resettingModel )
233+
endInsertRows();
232234
reload( index( rowCount() - 1, 0 ), index( rowCount() - 1, columnCount() ) );
233235
}
234236
}
@@ -412,7 +414,7 @@ void QgsAttributeTableModel::loadLayer()
412414

413415
t.restart();
414416
}
415-
featureAdded( mFeat.id() );
417+
featureAdded( mFeat.id(), true );
416418
}
417419

418420
emit finished();

‎src/gui/attributetable/qgsattributetablemodel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
307307
* Launched when a feature has been added
308308
* @param fid feature id
309309
*/
310-
virtual void featureAdded( QgsFeatureId fid );
310+
virtual void featureAdded( QgsFeatureId fid, bool resettingModel = false );
311311

312312
/**
313313
* Launched when layer has been deleted

7 commit comments

Comments
 (7)

NathanW2 commented on May 22, 2017

@NathanW2
Member

Win!

m-kuhn commented on May 22, 2017

@m-kuhn
Member

Nice

elpaso commented on May 22, 2017

@elpaso
Contributor

Nice!

luipir commented on May 22, 2017

@luipir
Contributor

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 commented on May 22, 2017

@nyalldawson
CollaboratorAuthor

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 commented on May 22, 2017

@luipir
Contributor

+1 for me :)

gioman commented on May 22, 2017

@gioman
Contributor

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.