Performance improvement. Speed up method VDomDocument::elementById.

Use integer key instead of string.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2017-08-02 14:40:17 +03:00
parent 7734b7a59d
commit 07a7b033b7
2 changed files with 35 additions and 26 deletions

View File

@ -189,13 +189,13 @@ VDomDocument::VDomDocument()
{}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief Finds an element by id.
* @param id value id attribute.
* @return dom element.
*/
QDomElement VDomDocument::elementById(const QString& id, const QString &tagName)
QDomElement VDomDocument::elementById(quint32 id, const QString &tagName)
{
if (id == 0)
{
return QDomElement();
}
if (map.contains(id))
{
const QDomElement e = map[id];
@ -221,25 +221,27 @@ QDomElement VDomDocument::elementById(const QString& id, const QString &tagName)
const QDomElement domElement = list.at(i).toElement();
if (not domElement.isNull() && domElement.hasAttribute(AttrId))
{
const QString value = domElement.attribute(AttrId);
this->map[value] = domElement;
if (value == id)
try
{
const quint32 elementId = GetParametrUInt(domElement, AttrId, NULL_ID_STR);
this->map[elementId] = domElement;
if (elementId == id)
{
return domElement;
}
}
catch (const VExceptionConversionError &)
{
// do nothing
}
}
}
}
return QDomElement();
}
//---------------------------------------------------------------------------------------------------------------------
QDomElement VDomDocument::elementById(quint32 id, const QString &tagName)
{
return elementById(QString().setNum(id), tagName);
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief Find element by id.
@ -247,17 +249,25 @@ QDomElement VDomDocument::elementById(quint32 id, const QString &tagName)
* @param id id value
* @return true if found
*/
bool VDomDocument::find(const QDomElement &node, const QString& id)
bool VDomDocument::find(const QDomElement &node, quint32 id)
{
if (node.hasAttribute(AttrId))
{
const QString value = node.attribute(AttrId);
this->map[value] = node;
if (value == id)
try
{
const quint32 elementId = GetParametrUInt(node, AttrId, NULL_ID_STR);
this->map[elementId] = node;
if (elementId == id)
{
return true;
}
}
catch (const VExceptionConversionError &)
{
// do nothing
}
}
for (qint32 i=0; i<node.childNodes().length(); ++i)
{
@ -815,7 +825,7 @@ QDomElement VDomDocument::CloneNodeById(const quint32 &nodeId)
//---------------------------------------------------------------------------------------------------------------------
QDomElement VDomDocument::NodeById(const quint32 &nodeId)
{
QDomElement domElement = elementById(QString().setNum(nodeId));
QDomElement domElement = elementById(nodeId);
if (domElement.isNull() || domElement.isElement() == false)
{
throw VExceptionBadId(tr("Couldn't get node"), nodeId);

View File

@ -88,7 +88,6 @@ public:
VDomDocument();
virtual ~VDomDocument() Q_DECL_EQ_DEFAULT;
QDomElement elementById(const QString& id, const QString &tagName = QString());
QDomElement elementById(quint32 id, const QString &tagName = QString());
template <typename T>
@ -134,9 +133,9 @@ protected:
private:
Q_DISABLE_COPY(VDomDocument)
/** @brief Map used for finding element by id. */
QHash<QString, QDomElement> map;
QHash<quint32, QDomElement> map;
bool find(const QDomElement &node, const QString& id);
bool find(const QDomElement &node, quint32 id);
bool SaveCanonicalXML(QIODevice *file, int indent, QString &error) const;
};