From f490ac671267438d8a539f4639c9e987861924cf Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sat, 9 May 2020 16:36:08 +0200 Subject: QXmlStreamReader: avoid double QHash lookups Replace if (hash.contains(x)) { // lookup #1 ~~~ hash[x]; // lookup #2 with if (auto it = hash.find(x); it != hash.end()) { // lookup ~~~ *it; // no lookup halving the number of QHash lookups. The container is not shared, so there's no danger of a detach when going directly to the non-const function. Change-Id: Ifae409f98e0be972b31a24326ad548723831fda8 Reviewed-by: Lars Knoll --- src/corelib/serialization/qxmlstream.g | 12 ++++++------ src/corelib/serialization/qxmlstream_p.h | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'src/corelib/serialization') diff --git a/src/corelib/serialization/qxmlstream.g b/src/corelib/serialization/qxmlstream.g index b623de9505..e630366822 100644 --- a/src/corelib/serialization/qxmlstream.g +++ b/src/corelib/serialization/qxmlstream.g @@ -1642,8 +1642,8 @@ entity_ref ::= AMPERSAND name SEMICOLON; case $rule_number: { sym(1).len += sym(2).len + 1; QStringView reference = symView(2); - if (entityHash.contains(reference)) { - Entity &entity = entityHash[reference]; + if (const auto it = entityHash.find(reference); it != entityHash.end()) { + Entity &entity = *it; if (entity.unparsed) { raiseWellFormedError(QXmlStream::tr("Reference to unparsed entity '%1'.").arg(reference)); } else { @@ -1684,9 +1684,9 @@ pereference ::= PERCENT name SEMICOLON; case $rule_number: { sym(1).len += sym(2).len + 1; QStringView reference = symView(2); - if (parameterEntityHash.contains(reference)) { + if (const auto it = parameterEntityHash.find(reference); it != parameterEntityHash.end()) { referenceToParameterEntityDetected = true; - Entity &entity = parameterEntityHash[reference]; + Entity &entity = *it; if (entity.unparsed || entity.external) { referenceToUnparsedEntityDetected = true; } else { @@ -1715,8 +1715,8 @@ entity_ref_in_attribute_value ::= AMPERSAND name SEMICOLON; case $rule_number: { sym(1).len += sym(2).len + 1; QStringView reference = symView(2); - if (entityHash.contains(reference)) { - Entity &entity = entityHash[reference]; + if (const auto it = entityHash.find(reference); it != entityHash.end()) { + Entity &entity = *it; if (entity.unparsed || entity.value.isNull()) { raiseWellFormedError(QXmlStream::tr("Reference to external entity '%1' in attribute value.").arg(reference)); break; diff --git a/src/corelib/serialization/qxmlstream_p.h b/src/corelib/serialization/qxmlstream_p.h index 103b123b10..0ffe57e403 100644 --- a/src/corelib/serialization/qxmlstream_p.h +++ b/src/corelib/serialization/qxmlstream_p.h @@ -1814,8 +1814,8 @@ bool QXmlStreamReaderPrivate::parse() case 240: { sym(1).len += sym(2).len + 1; QStringView reference = symView(2); - if (entityHash.contains(reference)) { - Entity &entity = entityHash[reference]; + if (const auto it = entityHash.find(reference); it != entityHash.end()) { + Entity &entity = *it; if (entity.unparsed) { raiseWellFormedError(QXmlStream::tr("Reference to unparsed entity '%1'.").arg(reference)); } else { @@ -1853,9 +1853,9 @@ bool QXmlStreamReaderPrivate::parse() case 241: { sym(1).len += sym(2).len + 1; QStringView reference = symView(2); - if (parameterEntityHash.contains(reference)) { + if (const auto it = parameterEntityHash.find(reference); it != parameterEntityHash.end()) { referenceToParameterEntityDetected = true; - Entity &entity = parameterEntityHash[reference]; + Entity &entity = *it; if (entity.unparsed || entity.external) { referenceToUnparsedEntityDetected = true; } else { @@ -1876,8 +1876,8 @@ bool QXmlStreamReaderPrivate::parse() case 243: { sym(1).len += sym(2).len + 1; QStringView reference = symView(2); - if (entityHash.contains(reference)) { - Entity &entity = entityHash[reference]; + if (const auto it = entityHash.find(reference); it != entityHash.end()) { + Entity &entity = *it; if (entity.unparsed || entity.value.isNull()) { raiseWellFormedError(QXmlStream::tr("Reference to external entity '%1' in attribute value.").arg(reference)); break; -- cgit v1.2.3