aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/ftw/qhashedstring_p.h
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2021-02-11 10:29:25 +0100
committerFabian Kosmale <fabian.kosmale@qt.io>2021-02-11 10:56:12 +0100
commit76f05314f5eb2ab3a13656647e99e20575471bed (patch)
tree98fd86936b2cb24df633afc3233929ff59475235 /src/qml/qml/ftw/qhashedstring_p.h
parent082cc7de37a4f728607cc4a669a2daa511e1216f (diff)
QHashedString(Ref): Use QStringView internally
We cannot store a QStringView internally, as that would increase the size of our objects (QHashedString(Ref) does not support strings with sizes that do not fit into 32bit, thus it can store the hash and the size in one 64 bit block). What we can however do is to construct a QStringView on demand (which is cheap), and use its methods. The benefits are twofold: We get rid of some accidental complexity in qtdeclarative; and the code in QStringView is actually SIMD optimized. Change-Id: I2445a2d5a16b253f4971d7f3be0e1b274326eacb Reviewed-by: Maximilian Goldstein <max.goldstein@qt.io> Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src/qml/qml/ftw/qhashedstring_p.h')
-rw-r--r--src/qml/qml/ftw/qhashedstring_p.h44
1 files changed, 21 insertions, 23 deletions
diff --git a/src/qml/qml/ftw/qhashedstring_p.h b/src/qml/qml/ftw/qhashedstring_p.h
index c5353acdb1..8f0bd74c53 100644
--- a/src/qml/qml/ftw/qhashedstring_p.h
+++ b/src/qml/qml/ftw/qhashedstring_p.h
@@ -79,7 +79,6 @@ public:
inline quint32 hash() const;
inline quint32 existingHash() const;
- static bool compare(const QChar *lhs, const QChar *rhs, int length);
static inline bool compare(const QChar *lhs, const char *rhs, int length);
static inline bool compare(const char *lhs, const char *rhs, int length);
@@ -217,9 +216,10 @@ bool QHashedString::operator==(const QHashedString &string) const
bool QHashedString::operator==(const QHashedStringRef &string) const
{
- return length() == string.m_length &&
- (string.m_hash == m_hash || !string.m_hash || !m_hash) &&
- QHashedString::compare(constData(), string.m_data, string.m_length);
+ if (m_hash && string.m_hash && m_hash != string.m_hash)
+ return false;
+ QStringView otherView {string.m_data, string.m_length};
+ return static_cast<const QString &>(*this) == otherView;
}
quint32 QHashedString::hash() const
@@ -277,22 +277,26 @@ QHashedStringRef &QHashedStringRef::operator=(const QHashedStringRef &o)
bool QHashedStringRef::operator==(const QString &string) const
{
- return m_length == string.length() &&
- QHashedString::compare(string.constData(), m_data, m_length);
+ QStringView view {m_data, m_length};
+ return view == string;
}
bool QHashedStringRef::operator==(const QHashedString &string) const
{
- return m_length == string.length() &&
- (m_hash == string.m_hash || !m_hash || !string.m_hash) &&
- QHashedString::compare(string.constData(), m_data, m_length);
+ if (m_hash && string.m_hash && m_hash != string.m_hash)
+ return false;
+ QStringView view {m_data, m_length};
+ QStringView otherView {string.constData(), string.length()};
+ return view == otherView;
}
bool QHashedStringRef::operator==(const QHashedStringRef &string) const
{
- return m_length == string.m_length &&
- (m_hash == string.m_hash || !m_hash || !string.m_hash) &&
- QHashedString::compare(string.m_data, m_data, m_length);
+ if (m_hash && string.m_hash && m_hash != string.m_hash)
+ return false;
+ QStringView view {m_data, m_length};
+ QStringView otherView {string.m_data, string.m_length};
+ return view == otherView;
}
bool QHashedStringRef::operator==(const QHashedCStringRef &string) const
@@ -304,29 +308,22 @@ bool QHashedStringRef::operator==(const QHashedCStringRef &string) const
bool QHashedStringRef::operator!=(const QString &string) const
{
- return m_length != string.length() ||
- !QHashedString::compare(string.constData(), m_data, m_length);
+ return !(*this == string);
}
bool QHashedStringRef::operator!=(const QHashedString &string) const
{
- return m_length != string.length() ||
- (m_hash != string.m_hash && m_hash && string.m_hash) ||
- !QHashedString::compare(string.constData(), m_data, m_length);
+ return !(*this == string);
}
bool QHashedStringRef::operator!=(const QHashedStringRef &string) const
{
- return m_length != string.m_length ||
- (m_hash != string.m_hash && m_hash && string.m_hash) ||
- QHashedString::compare(string.m_data, m_data, m_length);
+ return !(*this == string);
}
bool QHashedStringRef::operator!=(const QHashedCStringRef &string) const
{
- return m_length != string.m_length ||
- (m_hash != string.m_hash && m_hash && string.m_hash) ||
- QHashedString::compare(m_data, string.m_data, m_length);
+ return !(*this == string);
}
QChar *QHashedStringRef::data()
@@ -452,6 +449,7 @@ bool QHashedString::compare(const char *lhs, const char *rhs, int length)
return 0 == ::memcmp(lhs, rhs, length);
}
+
quint32 QHashedString::stringHash(const QChar *data, int length)
{
return QV4::String::createHashValue(data, length, nullptr);