From 5b78d7fdcf9b758f117eaa65dc5eeddd0d4557e0 Mon Sep 17 00:00:00 2001 From: Alex Trotsenko Date: Fri, 7 May 2021 18:16:17 +0300 Subject: QLocalSocket/Win: avoid double buffering on write As QWindowsPipeWriter now maintains a chunk queue, there is no need to use the internal QIODevice buffer and wait for the previous operation to complete. Change-Id: Id4d54edfe920aeb4a534980cb0cc85a56c067a70 Reviewed-by: Oswald Buddenhagen --- src/corelib/io/qiodevice.cpp | 4 +--- src/corelib/io/qiodevice_p.h | 7 +++++++ src/corelib/io/qwindowspipewriter.cpp | 29 +++++++++++++++++++++++------ src/corelib/io/qwindowspipewriter_p.h | 5 +++++ 4 files changed, 36 insertions(+), 9 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/io/qiodevice.cpp b/src/corelib/io/qiodevice.cpp index f3354cad65..3e3ee6e406 100644 --- a/src/corelib/io/qiodevice.cpp +++ b/src/corelib/io/qiodevice.cpp @@ -1763,9 +1763,7 @@ qint64 QIODevice::write(const QByteArray &data) */ void QIODevicePrivate::write(const char *data, qint64 size) { - if (currentWriteChunk != nullptr - && currentWriteChunk->constData() == data - && currentWriteChunk->size() == size) { + if (isWriteChunkCached(data, size)) { // We are called from write(const QByteArray &) overload. // So, we can make a shallow copy of chunk. writeBuffer.append(*currentWriteChunk); diff --git a/src/corelib/io/qiodevice_p.h b/src/corelib/io/qiodevice_p.h index 3f8e459d88..3639418bbc 100644 --- a/src/corelib/io/qiodevice_p.h +++ b/src/corelib/io/qiodevice_p.h @@ -185,6 +185,13 @@ public: qint64 skipByReading(qint64 maxSize); void write(const char *data, qint64 size); + inline bool isWriteChunkCached(const char *data, qint64 size) const + { + return currentWriteChunk != nullptr + && currentWriteChunk->constData() == data + && currentWriteChunk->size() == size; + } + #ifdef QT_NO_QOBJECT QIODevice *q_ptr = nullptr; #endif diff --git a/src/corelib/io/qwindowspipewriter.cpp b/src/corelib/io/qwindowspipewriter.cpp index f2f4dce056..cfa486451c 100644 --- a/src/corelib/io/qwindowspipewriter.cpp +++ b/src/corelib/io/qwindowspipewriter.cpp @@ -121,16 +121,35 @@ qint64 QWindowsPipeWriter::bytesToWrite() const } /*! - Writes data to the pipe. + Writes a shallow copy of \a ba to the internal buffer. */ bool QWindowsPipeWriter::write(const QByteArray &ba) +{ + return writeImpl(ba); +} + +/*! + Writes data to the internal buffer. + */ +bool QWindowsPipeWriter::write(const char *data, qint64 size) +{ + return writeImpl(data, size); +} + +template +inline bool QWindowsPipeWriter::writeImpl(Args... args) { QMutexLocker locker(&mutex); if (lastError != ERROR_SUCCESS) return false; - writeBuffer.append(ba); + writeBuffer.append(args...); + return writeImplTail(&locker); +} + +bool QWindowsPipeWriter::writeImplTail(QMutexLocker *locker) +{ if (writeSequenceStarted) return true; @@ -143,10 +162,10 @@ bool QWindowsPipeWriter::write(const QByteArray &ba) if (!winEventActPosted) { winEventActPosted = true; - locker.unlock(); + locker->unlock(); QCoreApplication::postEvent(this, new QEvent(QEvent::WinEventAct)); } else { - locker.unlock(); + locker->unlock(); } SetEvent(syncHandle); @@ -232,8 +251,6 @@ void QWindowsPipeWriter::waitCallback(PTP_CALLBACK_INSTANCE instance, PVOID cont bool QWindowsPipeWriter::writeCompleted(DWORD errorCode, DWORD numberOfBytesWritten) { if (errorCode == ERROR_SUCCESS) { - Q_ASSERT(numberOfBytesWritten == DWORD(writeBuffer.nextDataBlockSize())); - bytesWrittenPending = true; pendingBytesWrittenValue += numberOfBytesWritten; writeBuffer.free(numberOfBytesWritten); diff --git a/src/corelib/io/qwindowspipewriter_p.h b/src/corelib/io/qwindowspipewriter_p.h index cb78195a85..f8cabb225d 100644 --- a/src/corelib/io/qwindowspipewriter_p.h +++ b/src/corelib/io/qwindowspipewriter_p.h @@ -69,6 +69,7 @@ public: ~QWindowsPipeWriter(); bool write(const QByteArray &ba); + bool write(const char *data, qint64 size); void stop(); bool waitForWrite(int msecs); bool checkForWrite() { return consumePendingAndEmit(false); } @@ -83,6 +84,10 @@ protected: bool event(QEvent *e) override; private: + template + inline bool writeImpl(Args... args); + bool writeImplTail(QMutexLocker *locker); + void startAsyncWriteLocked(); static void CALLBACK waitCallback(PTP_CALLBACK_INSTANCE instance, PVOID context, PTP_WAIT wait, TP_WAIT_RESULT waitResult); -- cgit v1.2.3