diff options
author | Marc Mutz <marc.mutz@qt.io> | 2022-12-08 16:10:06 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2022-12-11 10:46:52 +0000 |
commit | fa2153bd10057d7adbc5f5ededa1fd97c4a68161 (patch) | |
tree | 4800b8de04c8045646cbf2f5442a84770ed92852 /src/corelib/serialization | |
parent | be3fbd804579770db0303e6f2ba5a08d5b4fb3b1 (diff) | |
download | qtbase-fa2153bd10057d7adbc5f5ededa1fd97c4a68161.tar.gz |
Optimize QXmlStreamWriterPrivate::doWriteToDevice(QStringView)
Use a stack buffer, and perform the recoding from U16 to U8 in chunks.
Since no-one so far managed to get a chunked QStringEncoder to produce
the same output as the test expected, at least not without additional
API, fall back to the raw QUtf8 functions. This also avoids the
indirect function call overhead that QStringEncoder would entail.
Fixes: QTBUG-109284
Change-Id: Icaa26c3988ac8506c9cf3fde18fd5892e5e63ef2
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/corelib/serialization')
-rw-r--r-- | src/corelib/serialization/qxmlstream.cpp | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/src/corelib/serialization/qxmlstream.cpp b/src/corelib/serialization/qxmlstream.cpp index 0aeef451bd..e68544b1cf 100644 --- a/src/corelib/serialization/qxmlstream.cpp +++ b/src/corelib/serialization/qxmlstream.cpp @@ -3063,13 +3063,19 @@ void QXmlStreamWriterPrivate::indent(int level) void QXmlStreamWriterPrivate::doWriteToDevice(QStringView s) { - QStringEncoder toUtf8(QStringEncoder::Utf8, QStringEncoder::Flag::Stateless); - QByteArray bytes = toUtf8(s); - if (toUtf8.hasError()) { - hasEncodingError = true; - return; + constexpr qsizetype MaxChunkSize = 512; + char buffer [3 * MaxChunkSize]; + QStringEncoder::State state; + while (!s.isEmpty()) { + const qsizetype chunkSize = std::min(s.size(), MaxChunkSize); + char *end = QUtf8::convertFromUnicode(buffer, s.first(chunkSize), &state); + if (state.remainingChars > 0) { + hasEncodingError = true; + return; + } + doWriteToDevice(QUtf8StringView{buffer, end}); + s = s.sliced(chunkSize); } - doWriteToDevice(QUtf8StringView{bytes}); } void QXmlStreamWriterPrivate::doWriteToDevice(QUtf8StringView s) |