diff options
| author | Marc Mutz <marc.mutz@qt.io> | 2025-02-10 18:55:38 +0100 |
|---|---|---|
| committer | Marc Mutz <marc.mutz@qt.io> | 2025-02-12 00:45:13 +0100 |
| commit | bbdbcb2eba1a4f0da0161f6e72eb86799235b554 (patch) | |
| tree | 9aa05f413709ea2199f706353ffaa173809e299d /src/corelib/serialization/qjsonobject.cpp | |
| parent | 6aaa874d79805dc6407a68e56f8d9ee128ad37bc (diff) | |
QJsonObject::iterator: add keyView()
Unlike CBOR maps, JSON objects can only have string keys, but, like
CBOR, they are internally stored as either US-ASCII (ie. Latin-1),
UTF-8, or UTF-16, so in order to return a view on the internal
storage, QAnyStringView is the perfect fit.
Add (const_)iterator::keyView() and add a benchmark, prepared for
testing a similar change to the value side of things.
Results (fastest each of ten runs) on my machine suggest a 40%
speedup:
PASS : BenchmarkQtJson::iteratorKey()
RESULT : BenchmarkQtJson::iteratorKey():
0.071 msecs per iteration (total: 73, iterations: 1024)
PASS : BenchmarkQtJson::iteratorKeyView()
RESULT : BenchmarkQtJson::iteratorKeyView():
0.042 msecs per iteration (total: 87, iterations: 2048)
[ChangeLog][QtCore][QJsonObject] Added keyView() methods to iterator
and const_iterator, allowing zero-copy inspection of the key().
Task-number: QTBUG-133688
Change-Id: I0ccedaf8a4fa41125b12bdbab5bea3bd2468d9a5
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/serialization/qjsonobject.cpp')
| -rw-r--r-- | src/corelib/serialization/qjsonobject.cpp | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/src/corelib/serialization/qjsonobject.cpp b/src/corelib/serialization/qjsonobject.cpp index 83eb76469d2..99f5507aa12 100644 --- a/src/corelib/serialization/qjsonobject.cpp +++ b/src/corelib/serialization/qjsonobject.cpp @@ -914,7 +914,24 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const iterator, although it can be done by calling QJsonObject::erase() followed by QJsonObject::insert(). - \sa value() + \sa value(), keyView() +*/ + +/*! + \fn QAnyStringView QJsonObject::iterator::keyView() const + \since 6.10 + + Returns the current item's key as a QAnyStringView. This function does not + allocate memory. + + Since QJsonObject stores keys in US-ASCII, UTF-8 or UTF-16, the returned + QAnyStringView may be in any of these encodings. + + There is no direct way of changing an item's key through an + iterator, although it can be done by calling QJsonObject::erase() + followed by QJsonObject::insert(). + + \sa key(), value() */ /*! \fn QJsonValueRef QJsonObject::iterator::value() const @@ -930,7 +947,7 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const the assignment will apply to the element in the QJsonArray or QJsonObject from which you got the reference. - \sa key(), operator*() + \sa key(), keyView(), operator*() */ /*! \fn QJsonValueRef QJsonObject::iterator::operator*() const @@ -945,7 +962,7 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const the assignment will apply to the element in the QJsonArray or QJsonObject from which you got the reference. - \sa key() + \sa key(), keyView() */ /*! \fn QJsonValueRef *QJsonObject::iterator::operator->() @@ -1189,14 +1206,27 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const Returns the current item's key. - \sa value() + \sa value(), keyView() +*/ + +/*! + \fn QAnyStringView QJsonObject::const_iterator::keyView() const + \since 6.10 + + Returns the current item's key as a QAnyStringView. This function does not + allocate. + + Since QJsonObject stores keys in US-ASCII, UTF-8 or UTF-16, the returned + QAnyStringView may be in any of these encodings. + + \sa value(), key() */ /*! \fn QJsonValueConstRef QJsonObject::const_iterator::value() const Returns the current item's value. - \sa key(), operator*() + \sa key(), keyView(), operator*() */ /*! \fn const QJsonValueConstRef QJsonObject::const_iterator::operator*() const @@ -1205,7 +1235,7 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const Same as value(). - \sa key() + \sa key(), keyView() */ /*! \fn const QJsonValueConstRef *QJsonObject::const_iterator::operator->() const |
