From 256f4805bc98bc975773ff4c4053a980d8c5277c Mon Sep 17 00:00:00 2001 From: Ivan Tkachenko Date: Sun, 31 Oct 2021 21:39:50 +0300 Subject: Improve node resolution process Since QSvgUse nodes are the only ones that ever get stored in the m_resolveNodes member, we can avoid extra cast and checks around it. Also a more descriptive name wouldn't hurt. Currently, the list is merely cleaned at the end of a parsing process, but ideally it should not be stored at all, hence I left the comment for possible future optimizations, if anyone's willing to do it. Pick-to: 6.2 Change-Id: I86e83a8661679503e532c230e70f6d52092ad58e Reviewed-by: Eirik Aavitsland --- src/svg/qsvghandler.cpp | 22 +++++++++++----------- src/svg/qsvghandler_p.h | 7 ++++--- 2 files changed, 15 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/svg/qsvghandler.cpp b/src/svg/qsvghandler.cpp index 778f175..f520c3f 100644 --- a/src/svg/qsvghandler.cpp +++ b/src/svg/qsvghandler.cpp @@ -3818,8 +3818,9 @@ bool QSvgHandler::startElement(const QString &localName, } else if (node->type() == QSvgNode::TSPAN) { static_cast(node)->setWhitespaceMode(m_whitespaceMode.top()); } else if (node->type() == QSvgNode::USE) { - if (!static_cast(node)->isResolved()) - m_resolveNodes.append(node); + auto useNode = static_cast(node); + if (!useNode->isResolved()) + m_toBeResolved.append(useNode); } } } @@ -3926,17 +3927,16 @@ void QSvgHandler::resolveGradients(QSvgNode *node, int nestedDepth) void QSvgHandler::resolveNodes() { - for (QSvgNode *node : qAsConst(m_resolveNodes)) { - if (!node || !node->parent() || node->type() != QSvgNode::USE) + for (QSvgUse *useNode : qAsConst(m_toBeResolved)) { + const auto parent = useNode->parent(); + if (!parent) continue; - QSvgUse *useNode = static_cast(node); - if (useNode->isResolved()) - continue; - QSvgNode::Type t = useNode->parent()->type(); - if (!(t == QSvgNode::DOC || t == QSvgNode::DEFS || t == QSvgNode::G || t == QSvgNode::SWITCH)) + + QSvgNode::Type t = parent->type(); + if (t != QSvgNode::DOC && t != QSvgNode::DEFS && t != QSvgNode::G && t != QSvgNode::SWITCH) continue; - QSvgStructureNode *group = static_cast(useNode->parent()); + QSvgStructureNode *group = static_cast(parent); QSvgNode *link = group->scopeNode(useNode->linkId()); if (!link) { qCWarning(lcSvgHandler, "link #%s is undefined!", qPrintable(useNode->linkId())); @@ -3948,7 +3948,7 @@ void QSvgHandler::resolveNodes() useNode->setLink(link); } - m_resolveNodes.clear(); + m_toBeResolved.clear(); } bool QSvgHandler::characters(const QStringView str) diff --git a/src/svg/qsvghandler_p.h b/src/svg/qsvghandler_p.h index 86b9fa4..2c4b158 100644 --- a/src/svg/qsvghandler_p.h +++ b/src/svg/qsvghandler_p.h @@ -144,9 +144,10 @@ private: void init(); QSvgTinyDocument *m_doc; - QStack m_nodes; - - QList m_resolveNodes; + QStack m_nodes; + // TODO: This is only needed during parsing, so it unnecessarily takes up space after that. + // Temporary container for nodes which haven't been resolved yet. + QList m_toBeResolved; enum CurrentNode { -- cgit v1.2.1