From 0f415edec3c2589daacb57de117d6b1aca23a5aa Mon Sep 17 00:00:00 2001 From: Mitch Bradley Date: Tue, 9 Feb 2021 13:45:40 -1000 Subject: [PATCH 1/2] Speed up upload by a factor of 17 Uploads are very slow because of an unnecessary "client.connected()" check in _uploadReadByte(). Here is what happens: client.connected() is called for every byte read. WiFiClient::connected() calls recv(fd(), &dummy, 0, MSG_DONTWAIT); which takes a relatively long time, so the optimized path of returning a buffered byte via client.read() is effectively nullified. Removing the one line changed the upload speed for a 2 MB file (discarding the received data) from 22 KB/sec (before) to 367 KB/sec (after). The change is safe in the face of disconnects because client.read(), when it no longer has buffered data, calls (WiFiClient) fillBuffer(), which calls recv(), so the disconnection will be detected in due course. --- libraries/WebServer/src/Parsing.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/libraries/WebServer/src/Parsing.cpp b/libraries/WebServer/src/Parsing.cpp index 1db2aef5541..9a7af6543c6 100644 --- a/libraries/WebServer/src/Parsing.cpp +++ b/libraries/WebServer/src/Parsing.cpp @@ -303,7 +303,6 @@ void WebServer::_uploadWriteByte(uint8_t b){ } int WebServer::_uploadReadByte(WiFiClient& client){ - if (!client.connected()) return -1; int res = client.read(); if(res < 0) { // keep trying until you either read a valid byte or timeout From a35b5c71a415571fce3251f56b6db429dc2e633b Mon Sep 17 00:00:00 2001 From: Mitch Bradley Date: Fri, 12 Feb 2021 08:20:29 -1000 Subject: [PATCH 2/2] Move disconnect check into the timeout loop --- libraries/WebServer/src/Parsing.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/WebServer/src/Parsing.cpp b/libraries/WebServer/src/Parsing.cpp index 9a7af6543c6..ef455c8ab65 100644 --- a/libraries/WebServer/src/Parsing.cpp +++ b/libraries/WebServer/src/Parsing.cpp @@ -310,6 +310,7 @@ int WebServer::_uploadReadByte(WiFiClient& client){ long timeoutIntervalMillis = client.getTimeout(); boolean timedOut = false; for(;;) { + if (!client.connected()) return -1; // loosely modeled after blinkWithoutDelay pattern while(!timedOut && !client.available() && client.connected()){ delay(2);