Skip to content

Commit

Permalink
QgsGmlStreamingParser: fix quadratic performance pattern when parsing…
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Jan 20, 2024
1 parent c348cb8 commit 0d8649d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/core/qgsgml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1109,18 +1109,14 @@ void QgsGmlStreamingParser::endElement( const XML_Char *el )

if ( mAttributeValIsNested )
{
//find index with attribute name
const QMap<QString, QPair<int, QgsField> >::const_iterator att_it = mThematicAttributes.constFind( mAttributeName );
Q_ASSERT( mCurrentFeature );
const int attrIndex = att_it.value().first;
auto attrVal = mCurrentFeature->attribute( attrIndex );
if ( attrVal.isNull() )
auto iter = mMapFieldNameToJSONContent.find( mAttributeName );
if ( iter == mMapFieldNameToJSONContent.end() )
{
mCurrentFeature->setAttribute( attrIndex, QString::fromStdString( mAttributeJson.dump() ) );
mMapFieldNameToJSONContent[mAttributeName] = QString::fromStdString( mAttributeJson.dump() );
}
else
{
QString str = attrVal.toString();
QString &str = iter.value();
if ( str[0] == '[' && str.back() == ']' )
{
str.back() = ',';
Expand All @@ -1132,7 +1128,6 @@ void QgsGmlStreamingParser::endElement( const XML_Char *el )
}
str.append( QString::fromStdString( mAttributeJson.dump() ) );
str.append( ']' );
mCurrentFeature->setAttribute( attrIndex, str );
}
}
else
Expand Down Expand Up @@ -1242,6 +1237,14 @@ void QgsGmlStreamingParser::endElement( const XML_Char *el )
}
mCurrentFeature->setValid( true );

for ( auto iter = mMapFieldNameToJSONContent.constBegin(); iter != mMapFieldNameToJSONContent.constEnd(); ++iter )
{
const QMap<QString, QPair<int, QgsField> >::const_iterator att_it = mThematicAttributes.constFind( iter.key() );
const int attrIndex = att_it.value().first;
mCurrentFeature->setAttribute( attrIndex, iter.value() );
}
mMapFieldNameToJSONContent.clear();

mFeatureList.push_back( QgsGmlFeaturePtrGmlIdPair( mCurrentFeature, mCurrentFeatureId ) );

mCurrentFeature = nullptr;
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgsgml.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ class CORE_EXPORT QgsGmlStreamingParser
QString mAttributeName;
int mAttributeDepth = -1;
bool mAttributeValIsNested = false;
//! Map from field name to JSON content.
QMap< QString, QString > mMapFieldNameToJSONContent;
nlohmann::json mAttributeJson;
QStack<nlohmann::json *> mAttributeJsonCurrentStack;
char mEndian;
Expand Down

0 comments on commit 0d8649d

Please sign in to comment.