QJsonValueConcreteRef: optimize concrete()

Inline the content to avoid a round-trip through qjsonarray.cpp and
qjsonobject.cpp.

This change revealed an inadviseable unit test check that dereferences
the end() iterator to get its type. I haven't changed it, but have
marked with ###. I also fixed a likely copy&paste mistake in that test.

Change-Id: I89446ea06b5742efb194fffd16bb774f3bfbe5f5
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Thiago Macieira 2021-11-27 11:27:51 -06:00
parent e99417106f
commit dccd1e87f5
6 changed files with 33 additions and 8 deletions

View File

@ -56,6 +56,9 @@
#include <qcborvalue.h>
#include <private/qcborvalue_p.h>
#include <qjsonarray.h>
#include <qjsonobject.h>
QT_BEGIN_NAMESPACE
namespace QJsonPrivate {
@ -203,8 +206,27 @@ inline void swap(KeyIterator::reference a, KeyIterator::reference b)
class Value
{
public:
static QCborContainerPrivate *container(const QCborValue &v) { return v.container; }
static qint64 valueHelper(const QCborValue &v) { return v.n; }
static QCborContainerPrivate *container(const QCborValue &v) { return v.container; }
static const QCborContainerPrivate *container(QJsonValueConstRef r) noexcept
{
return (r.is_object ? r.o->o : r.a->a).data();
}
static QCborContainerPrivate *container(QJsonValueRef r) noexcept
{
return const_cast<QCborContainerPrivate *>(container(QJsonValueConstRef(r)));
}
static qsizetype indexHelper(QJsonValueConstRef r) noexcept
{
qsizetype index = r.index;
if (r.is_object)
index = index * 2 + 1;
return index;
}
static const QtCbor::Element &elementHelper(QJsonValueConstRef r) noexcept
{
return container(r)->elements.at(indexHelper(r));
}
static QJsonValue fromTrustedCbor(const QCborValue &v)
{

View File

@ -262,6 +262,8 @@ public:
private:
friend class QJsonValue;
friend class QJsonValueConstRef;
friend class QJsonPrivate::Value;
friend class QJsonDocument;
friend class QCborArray;
friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &);

View File

@ -284,6 +284,7 @@ public:
private:
friend class QJsonValue;
friend class QJsonDocument;
friend class QJsonPrivate::Value;
friend class QJsonValueConstRef;
friend class QJsonValueRef;
friend class QCborMap;

View File

@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Copyright (C) 2022 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@ -951,9 +952,9 @@ QJsonObject QJsonValueConstRef::toObject() const
QJsonValue QJsonValueConstRef::concrete(QJsonValueConstRef self) noexcept
{
if (!self.is_object)
return self.a->at(self.index);
return self.o->valueAt(self.index);
const QCborContainerPrivate *d = QJsonPrivate::Value::container(self);
qsizetype index = QJsonPrivate::Value::indexHelper(self);
return QJsonPrivate::Value::fromTrustedCbor(d->valueAt(index));
}
#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED)

View File

@ -206,6 +206,7 @@ protected:
friend class QJsonArray;
friend class QJsonObject;
friend class QJsonPrivate::Value;
};
class Q_CORE_EXPORT QJsonValueRef : public QJsonValueConstRef

View File

@ -1,7 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Copyright (C) 2021 Intel Corporation.
** Copyright (C) 2022 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
@ -1226,14 +1226,12 @@ void tst_QtJson::testObjectFind()
QJsonObject::iterator it = object.find(QLatin1String("1"));
QCOMPARE((*it).toDouble(), 1.);
it = object.find(QString("11"));
QCOMPARE((*it).type(), QJsonValue::Undefined);
QCOMPARE(it, object.end());
QJsonObject::const_iterator cit = object.constFind(QLatin1String("1"));
QCOMPARE((*cit).toDouble(), 1.);
cit = object.constFind(QString("11"));
QCOMPARE((*it).type(), QJsonValue::Undefined);
QCOMPARE(it, object.end());
QCOMPARE(cit, object.constEnd());
}
void tst_QtJson::testDocument()