Skip to content
Permalink
Browse files

Big boost to QgsPointCloudAttributeCollection::find by internal cache

  • Loading branch information
nyalldawson committed Nov 4, 2020
1 parent 0d77269 commit 31fa79ee459d28219d8b056acb03fa81b4a83cca
Showing with 20 additions and 19 deletions.
  1. +8 −19 src/core/pointcloud/qgspointcloudattribute.cpp
  2. +12 −0 src/core/pointcloud/qgspointcloudattribute.h
@@ -69,18 +69,17 @@ void QgsPointCloudAttribute::updateSize()
QgsPointCloudAttributeCollection::QgsPointCloudAttributeCollection() = default;

QgsPointCloudAttributeCollection::QgsPointCloudAttributeCollection( const QVector<QgsPointCloudAttribute> &attributes )
: mAttributes( attributes )
{
for ( int i = 0; i < mAttributes.size(); ++i )
mAttributes.reserve( attributes.size() );
for ( const QgsPointCloudAttribute &attribute : attributes )
{
const QgsPointCloudAttribute &attribute = mAttributes.at( i );
mSize += attribute.size();
push_back( attribute );
}

}

void QgsPointCloudAttributeCollection::push_back( const QgsPointCloudAttribute &attribute )
{
mCachedAttributes.insert( attribute.name(), CachedAttributeData( mAttributes.size(), mSize ) );
mAttributes.push_back( attribute );
mSize += attribute.size();
}
@@ -92,21 +91,11 @@ QVector<QgsPointCloudAttribute> QgsPointCloudAttributeCollection::attributes() c

const QgsPointCloudAttribute *QgsPointCloudAttributeCollection::find( const QString &attributeName, int &offset ) const
{

int off = 0;

for ( int i = 0; i < mAttributes.size(); ++i )
auto it = mCachedAttributes.constFind( attributeName );
if ( it != mCachedAttributes.constEnd() )
{
const QgsPointCloudAttribute &attr = mAttributes.at( i );
if ( attr.name() == attributeName )
{
offset = off;
return &attr;
}
else
{
off += attr.size();
}
offset = it->offset;
return &mAttributes.at( it->index );
}

// not found
@@ -101,6 +101,18 @@ class CORE_EXPORT QgsPointCloudAttributeCollection
private:
int mSize = 0;
QVector<QgsPointCloudAttribute> mAttributes;

struct CachedAttributeData
{
int index;
int offset;
CachedAttributeData( int index, int offset )
: index( index )
, offset( offset )
{}
};

QMap< QString, CachedAttributeData > mCachedAttributes;
};

#endif // QGSPOINTCLOUDATTRIBUTE_H

0 comments on commit 31fa79e

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