summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Shienkov <denis.shienkov@gmail.com>2017-04-12 10:03:37 +0300
committerDenis Shienkov <denis.shienkov@gmail.com>2017-04-26 06:47:34 +0000
commita5ecc626bc6cf4e0890150acabcbff3a8d9af871 (patch)
tree2a3eb29fb73b63219904207c0eac6dd5da7df137
parentaa1ab248bd3dad450caabb445cd71874048d75b3 (diff)
downloadqtserialport-a5ecc626bc6cf4e0890150acabcbff3a8d9af871.tar.gz
Increase buffer chunk sizes
This reduces the number of syscalls and thus the CPU load when dealing with fast ports. And 32KiB seems still small enough to be no concern on modern hardware even if it's excessive for a given port. Change-Id: I4749e4255627d3abf2393323216e01ac6436413b Reviewed-by: Alex Trotsenko <alex1973tr@gmail.com> Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@qt.io> Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
-rw-r--r--src/serialport/qserialport.cpp5
-rw-r--r--src/serialport/qserialport_p.h9
-rw-r--r--src/serialport/qserialport_unix.cpp2
-rw-r--r--src/serialport/qserialport_win.cpp8
4 files changed, 13 insertions, 11 deletions
diff --git a/src/serialport/qserialport.cpp b/src/serialport/qserialport.cpp
index fa780f3..65fcfbb 100644
--- a/src/serialport/qserialport.cpp
+++ b/src/serialport/qserialport.cpp
@@ -101,7 +101,7 @@ QSerialPortPrivate::QSerialPortPrivate()
, isBreakEnabled(false)
#if defined(Q_OS_WIN32)
, handle(INVALID_HANDLE_VALUE)
- , readChunkBuffer(ReadChunkSize, 0)
+ , readChunkBuffer(QSERIALPORT_BUFFERSIZE, 0)
, communicationStarted(false)
, writeStarted(false)
, readStarted(false)
@@ -121,7 +121,8 @@ QSerialPortPrivate::QSerialPortPrivate()
, writeSequenceStarted(false)
#endif
{
- writeBufferChunkSize = InitialBufferSize;
+ writeBufferChunkSize = QSERIALPORT_BUFFERSIZE;
+ readBufferChunkSize = QSERIALPORT_BUFFERSIZE;
}
void QSerialPortPrivate::setError(const QSerialPortErrorInfo &errorInfo)
diff --git a/src/serialport/qserialport_p.h b/src/serialport/qserialport_p.h
index 7512da0..a81cc4b 100644
--- a/src/serialport/qserialport_p.h
+++ b/src/serialport/qserialport_p.h
@@ -97,6 +97,10 @@ struct serial_struct {
# error Unsupported OS
#endif
+#ifndef QSERIALPORT_BUFFERSIZE
+#define QSERIALPORT_BUFFERSIZE 32768
+#endif
+
QT_BEGIN_NAMESPACE
class QWinOverlappedIoNotifier;
@@ -120,11 +124,6 @@ class QSerialPortPrivate : public QIODevicePrivate
{
Q_DECLARE_PUBLIC(QSerialPort)
public:
- enum IoConstants {
- ReadChunkSize = 512,
- InitialBufferSize = 16384
- };
-
QSerialPortPrivate();
bool open(QIODevice::OpenMode mode);
diff --git a/src/serialport/qserialport_unix.cpp b/src/serialport/qserialport_unix.cpp
index 8f9f087..0c79b47 100644
--- a/src/serialport/qserialport_unix.cpp
+++ b/src/serialport/qserialport_unix.cpp
@@ -769,7 +769,7 @@ bool QSerialPortPrivate::readNotification()
// Always buffered, read data from the port into the read buffer
qint64 newBytes = buffer.size();
- qint64 bytesToRead = ReadChunkSize;
+ qint64 bytesToRead = QSERIALPORT_BUFFERSIZE;
if (readBufferMaxSize && bytesToRead > (readBufferMaxSize - buffer.size())) {
bytesToRead = readBufferMaxSize - buffer.size();
diff --git a/src/serialport/qserialport_win.cpp b/src/serialport/qserialport_win.cpp
index 43660ff..67c68ee 100644
--- a/src/serialport/qserialport_win.cpp
+++ b/src/serialport/qserialport_win.cpp
@@ -355,7 +355,7 @@ bool QSerialPortPrivate::waitForReadyRead(int msecs)
if (overlapped == &readCompletionOverlapped) {
const qint64 readBytesForOneReadOperation = qint64(buffer.size()) - currentReadBufferSize;
- if (readBytesForOneReadOperation == ReadChunkSize) {
+ if (readBytesForOneReadOperation == QSERIALPORT_BUFFERSIZE) {
currentReadBufferSize = buffer.size();
} else if (readBytesForOneReadOperation == 0) {
if (initialReadBufferSize != currentReadBufferSize)
@@ -481,7 +481,7 @@ bool QSerialPortPrivate::completeAsyncRead(qint64 bytesTransferred)
readStarted = false;
bool result = true;
- if (bytesTransferred == ReadChunkSize
+ if (bytesTransferred == QSERIALPORT_BUFFERSIZE
|| queuedBytesCount(QSerialPort::Input) > 0) {
result = startAsyncRead();
} else if (readBufferMaxSize == 0
@@ -538,7 +538,7 @@ bool QSerialPortPrivate::startAsyncRead()
if (readStarted)
return true;
- DWORD bytesToRead = ReadChunkSize;
+ DWORD bytesToRead = QSERIALPORT_BUFFERSIZE;
if (readBufferMaxSize && bytesToRead > (readBufferMaxSize - buffer.size())) {
bytesToRead = readBufferMaxSize - buffer.size();
@@ -549,6 +549,8 @@ bool QSerialPortPrivate::startAsyncRead()
}
}
+ Q_ASSERT(int(bytesToRead) <= readChunkBuffer.size());
+
::ZeroMemory(&readCompletionOverlapped, sizeof(readCompletionOverlapped));
if (::ReadFile(handle, readChunkBuffer.data(), bytesToRead, nullptr, &readCompletionOverlapped)) {
readStarted = true;