Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Micro optimization of DOM loops
  • Loading branch information
elpaso committed Dec 10, 2020
1 parent 4056de7 commit 55869a0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
23 changes: 13 additions & 10 deletions src/core/qgslayerdefinition.cpp
Expand Up @@ -78,18 +78,20 @@ bool QgsLayerDefinition::loadLayerDefinition( QDomDocument doc, QgsProject *proj
}
QDomNode layersNode = doc.elementsByTagName( QStringLiteral( "maplayers" ) ).at( 0 );
// replace old children with new ones
QDomNodeList childNodes = layersNode.childNodes();
for ( int i = 0; i < childNodes.size(); i++ )
QDomNode childNode = layersNode.firstChild();
for ( int i = 0; ! childNode.isNull(); i++ )
{
layersNode.replaceChild( clonedSorted.at( i ), childNodes.at( i ) );
layersNode.replaceChild( clonedSorted.at( i ), childNode );
childNode = childNode.nextSibling();
}
}
// if a dependency is missing, we still try to load layers, since dependencies may already be loaded

// IDs of layers should be changed otherwise we may have more then one layer with the same id
// We have to replace the IDs before we load them because it's too late once they are loaded
QDomNodeList treeLayerNodes = doc.elementsByTagName( QStringLiteral( "layer-tree-layer" ) );
for ( int i = 0; i < treeLayerNodes.size(); ++i )
const QDomNodeList treeLayerNodes = doc.elementsByTagName( QStringLiteral( "layer-tree-layer" ) );
QDomNode treeLayerNode = treeLayerNodes.at( 0 );
for ( int i = 0; ! treeLayerNode.isNull(); ++i )
{
QDomNode treeLayerNode = treeLayerNodes.at( i );
QDomElement treeLayerElem = treeLayerNode.toElement();
Expand All @@ -99,10 +101,11 @@ bool QgsLayerDefinition::loadLayerDefinition( QDomDocument doc, QgsProject *proj
treeLayerElem.setAttribute( QStringLiteral( "id" ), newid );

// Replace IDs for map layers
QDomNodeList ids = doc.elementsByTagName( QStringLiteral( "id" ) );
for ( int i = 0; i < ids.size(); ++i )
const QDomNodeList ids = doc.elementsByTagName( QStringLiteral( "id" ) );
QDomNode idnode = ids.at( 0 );
for ( int j = 0; ! idnode.isNull() ; ++j )
{
QDomNode idnode = ids.at( i );
QDomNode idnode = ids.at( j );
QDomElement idElem = idnode.toElement();
if ( idElem.text() == oldid )
{
Expand All @@ -111,7 +114,7 @@ bool QgsLayerDefinition::loadLayerDefinition( QDomDocument doc, QgsProject *proj
}

// change layer IDs for vector joins
QDomNodeList vectorJoinNodes = doc.elementsByTagName( QStringLiteral( "join" ) ); // TODO: Find a better way of searching for vectorjoins, there might be other <join> elements within the project.
const QDomNodeList vectorJoinNodes = doc.elementsByTagName( QStringLiteral( "join" ) ); // TODO: Find a better way of searching for vectorjoins, there might be other <join> elements within the project.
for ( int j = 0; j < vectorJoinNodes.size(); ++j )
{
QDomNode joinNode = vectorJoinNodes.at( j );
Expand All @@ -123,7 +126,7 @@ bool QgsLayerDefinition::loadLayerDefinition( QDomDocument doc, QgsProject *proj
}

// change IDs of dependencies
QDomNodeList dataDeps = doc.elementsByTagName( QStringLiteral( "dataDependencies" ) );
const QDomNodeList dataDeps = doc.elementsByTagName( QStringLiteral( "dataDependencies" ) );
for ( int i = 0; i < dataDeps.size(); i++ )
{
QDomNodeList layers = dataDeps.at( i ).childNodes();
Expand Down
47 changes: 29 additions & 18 deletions src/core/qgsproject.cpp
Expand Up @@ -954,7 +954,7 @@ void _getProperties( const QDomDocument &doc, QgsProjectPropertyKey &project_pro

QDomNodeList scopes = propertiesElem.childNodes();

if ( scopes.count() < 1 )
if ( propertiesElem.firstChild().isNull() )
{
QgsDebugMsg( QStringLiteral( "empty ``properties'' XML tag ... bailing" ) );
return;
Expand Down Expand Up @@ -993,23 +993,23 @@ QgsPropertyCollection getDataDefinedServerProperties( const QDomDocument &doc, c
*/
static void _getTitle( const QDomDocument &doc, QString &title )
{
QDomElement titleElement = doc.documentElement().firstChildElement( QStringLiteral( "title" ) );
QDomElement titleNode = doc.documentElement().firstChildElement( QStringLiteral( "title" ) );

title.clear(); // by default the title will be empty

if ( titleElement.isNull() )
if ( titleNode.isNull() )
{
QgsDebugMsgLevel( QStringLiteral( "unable to find title element" ), 2 );
return;
}

if ( !titleElement.hasChildNodes() ) // if not, then there's no actual text
if ( !titleNode.hasChildNodes() ) // if not, then there's no actual text
{
QgsDebugMsgLevel( QStringLiteral( "unable to find title element" ), 2 );
return;
}

QDomNode titleTextNode = titleElement.firstChild(); // should only have one child
QDomNode titleTextNode = titleNode.firstChild(); // should only have one child

if ( !titleTextNode.isText() )
{
Expand All @@ -1025,15 +1025,17 @@ static void _getTitle( const QDomDocument &doc, QString &title )

static void readProjectFileMetadata( const QDomDocument &doc, QString &lastUser, QString &lastUserFull, QDateTime &lastSaveDateTime )
{
QDomElement qgisElement = doc.documentElement().firstChildElement( QStringLiteral( "qgis" ) );
QDomNodeList nl = doc.elementsByTagName( QStringLiteral( "qgis" ) );

if ( qgisElement.isNull() )
if ( !nl.count() )
{
QgsDebugMsg( "unable to find qgis element" );
return;
}

QDomNode qgisNode = nl.item( 0 ); // there should only be one, so zeroth element OK

QDomElement qgisElement = qgisNode.toElement(); // qgis node should be element
lastUser = qgisElement.attribute( QStringLiteral( "saveUser" ), QString() );
lastUserFull = qgisElement.attribute( QStringLiteral( "saveUserFull" ), QString() );
lastSaveDateTime = QDateTime::fromString( qgisElement.attribute( QStringLiteral( "saveDateTime" ), QString() ), Qt::ISODate );
Expand All @@ -1042,14 +1044,17 @@ static void readProjectFileMetadata( const QDomDocument &doc, QString &lastUser,

QgsProjectVersion getVersion( const QDomDocument &doc )
{
QDomElement qgisElement = doc.documentElement().firstChildElement( QStringLiteral( "qgis" ) );
QDomNodeList nl = doc.elementsByTagName( QStringLiteral( "qgis" ) );

if ( qgisElement.isNull() )
if ( !nl.count() )
{
QgsDebugMsg( QStringLiteral( " unable to find qgis element in project file" ) );
return QgsProjectVersion( 0, 0, 0, QString() );
}

QDomNode qgisNode = nl.item( 0 ); // there should only be one, so zeroth element OK

QDomElement qgisElement = qgisNode.toElement(); // qgis node should be element
QgsProjectVersion projectVersion( qgisElement.attribute( QStringLiteral( "version" ) ) );
return projectVersion;
}
Expand Down Expand Up @@ -1084,12 +1089,11 @@ bool QgsProject::_getMapLayers( const QDomDocument &doc, QList<QDomNode> &broken
// Layer order is set by the restoring the legend settings from project file.
// This is done on the 'readProject( ... )' signal

const QDomNodeList nl = doc.elementsByTagName( QStringLiteral( "maplayer" ) );
QDomElement layerElement = doc.documentElement().firstChildElement( QStringLiteral( "projectlayers" ) ).firstChildElement( QStringLiteral( "maplayer" ) );

// process the map layer nodes
const int numLayers { nl.count() };

if ( 0 == numLayers ) // if we have no layers to process, bail
if ( layerElement.isNull() ) // if we have no layers to process, bail
{
return true; // Decided to return "true" since it's
// possible for there to be a project with no
Expand All @@ -1100,6 +1104,13 @@ bool QgsProject::_getMapLayers( const QDomDocument &doc, QList<QDomNode> &broken
}

bool returnStatus = true;
int numLayers = 0;

while ( ! layerElement.isNull() )
{
numLayers++;
layerElement = layerElement.nextSiblingElement( QStringLiteral( "maplayer" ) );
}

// order layers based on their dependencies
QgsScopedRuntimeProfile profile( tr( "Sorting layers" ), QStringLiteral( "projectload" ) );
Expand Down Expand Up @@ -2840,22 +2851,22 @@ bool QgsProject::createEmbeddedLayer( const QString &layerId, const QString &pro
return false;
}

QDomElement mapLayerElement = projectLayersElem.firstChildElement( QStringLiteral( "maplayer" ) );
while ( ! mapLayerElement.isNull() )
QDomElement mapLayerElem = projectLayersElem.firstChildElement( QStringLiteral( "maplayer" ) );
while ( ! mapLayerElem.isNull() )
{
// get layer id
QString id = mapLayerElement.firstChildElement( QStringLiteral( "id" ) ).text();
QString id = mapLayerElem.firstChildElement( QStringLiteral( "id" ) ).text();
if ( id == layerId )
{
// layer can be embedded only once
if ( mapLayerElement.attribute( QStringLiteral( "embedded" ) ) == QLatin1String( "1" ) )
if ( mapLayerElem.attribute( QStringLiteral( "embedded" ) ) == QLatin1String( "1" ) )
{
return false;
}

mEmbeddedLayers.insert( layerId, qMakePair( projectFilePath, saveFlag ) );

if ( addLayer( mapLayerElement, brokenNodes, embeddedContext, flags ) )
if ( addLayer( mapLayerElem, brokenNodes, embeddedContext, flags ) )
{
return true;
}
Expand All @@ -2865,7 +2876,7 @@ bool QgsProject::createEmbeddedLayer( const QString &layerId, const QString &pro
return false;
}
}
mapLayerElement = mapLayerElement.nextSiblingElement( QStringLiteral( "maplayer" ) );
mapLayerElem = mapLayerElem.nextSiblingElement( QStringLiteral( "maplayer" ) );
}

return false;
Expand Down

0 comments on commit 55869a0

Please sign in to comment.