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()
{} {}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
/** QDomElement VDomDocument::elementById(quint32 id, const QString &tagName)
* @brief Finds an element by id.
* @param id value id attribute.
* @return dom element.
*/
QDomElement VDomDocument::elementById(const QString& id, const QString &tagName)
{ {
if (id == 0)
{
return QDomElement();
}
if (map.contains(id)) if (map.contains(id))
{ {
const QDomElement e = map[id]; const QDomElement e = map[id];
@ -221,11 +221,19 @@ QDomElement VDomDocument::elementById(const QString& id, const QString &tagName)
const QDomElement domElement = list.at(i).toElement(); const QDomElement domElement = list.at(i).toElement();
if (not domElement.isNull() && domElement.hasAttribute(AttrId)) if (not domElement.isNull() && domElement.hasAttribute(AttrId))
{ {
const QString value = domElement.attribute(AttrId); try
this->map[value] = domElement;
if (value == id)
{ {
return domElement; const quint32 elementId = GetParametrUInt(domElement, AttrId, NULL_ID_STR);
this->map[elementId] = domElement;
if (elementId == id)
{
return domElement;
}
}
catch (const VExceptionConversionError &)
{
// do nothing
} }
} }
} }
@ -234,12 +242,6 @@ QDomElement VDomDocument::elementById(const QString& id, const QString &tagName)
return QDomElement(); return QDomElement();
} }
//---------------------------------------------------------------------------------------------------------------------
QDomElement VDomDocument::elementById(quint32 id, const QString &tagName)
{
return elementById(QString().setNum(id), tagName);
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
/** /**
* @brief Find element by id. * @brief Find element by id.
@ -247,15 +249,23 @@ QDomElement VDomDocument::elementById(quint32 id, const QString &tagName)
* @param id id value * @param id id value
* @return true if found * @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)) if (node.hasAttribute(AttrId))
{ {
const QString value = node.attribute(AttrId); try
this->map[value] = node;
if (value == id)
{ {
return true; const quint32 elementId = GetParametrUInt(node, AttrId, NULL_ID_STR);
this->map[elementId] = node;
if (elementId == id)
{
return true;
}
}
catch (const VExceptionConversionError &)
{
// do nothing
} }
} }
@ -815,7 +825,7 @@ QDomElement VDomDocument::CloneNodeById(const quint32 &nodeId)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QDomElement VDomDocument::NodeById(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) if (domElement.isNull() || domElement.isElement() == false)
{ {
throw VExceptionBadId(tr("Couldn't get node"), nodeId); throw VExceptionBadId(tr("Couldn't get node"), nodeId);

View File

@ -88,8 +88,7 @@ public:
VDomDocument(); VDomDocument();
virtual ~VDomDocument() Q_DECL_EQ_DEFAULT; virtual ~VDomDocument() Q_DECL_EQ_DEFAULT;
QDomElement elementById(const QString& id, const QString &tagName = QString()); QDomElement elementById(quint32 id, const QString &tagName = QString());
QDomElement elementById(quint32 id, const QString &tagName = QString());
template <typename T> template <typename T>
void SetAttribute(QDomElement &domElement, const QString &name, const T &value) const; void SetAttribute(QDomElement &domElement, const QString &name, const T &value) const;
@ -134,9 +133,9 @@ protected:
private: private:
Q_DISABLE_COPY(VDomDocument) Q_DISABLE_COPY(VDomDocument)
/** @brief Map used for finding element by id. */ /** @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; bool SaveCanonicalXML(QIODevice *file, int indent, QString &error) const;
}; };