Skip to content

Slow WiFiClient after commit "ClientContext (tcp) updates #5089" #5170

Closed
@mongozmaki

Description

@mongozmaki

Hi!

I realized that after the last commit (ClientContext (tcp) updates (#5089)), my Webserver was very slow while sending big files.
I looked at the changes and it seems that ClientContext::_write_some() causes the problem.
My suspicion is, that the if-condition in line 510 should be reverted so that tcp_output is always called.
If I set WiFiClient::setDefaultNoDelay(true) at the beginning of my program, it works.

Possible solution:
ClientContext::_write_some (line 510):

Change

   if (has_written && (_sync || tcp_nagle_disabled(_pcb))) {
      ...
      tcp_output(_pcb);
    }

to

   if (has_written) {
      ...
      tcp_output(_pcb);
    }

Testcode:
Compile and visit IP address in webbrowser.
Toggle variable NO_DELAY to see effect.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char* ssid = "***";
const char* password = "***";

constexpr size_t HUGE_PAGE_SIZE = 32 * 1024;
constexpr bool NO_DELAY = false;

ESP8266WebServer server(80);

void setup(void) {

    WiFiClient::setDefaultNoDelay(NO_DELAY);
   
    Serial.begin(115200);

    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    Serial.println("");

    // Wait for connection
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());

    server.onNotFound([]() {
        const String html = "<html><header><title>A test</title></header><body>A huge page</body></html>";
        String hugePage;
     
        hugePage.reserve(HUGE_PAGE_SIZE+html.length());
        for (size_t i = 0; i != HUGE_PAGE_SIZE; ++i) {
            hugePage += ' ';
        }

        hugePage += html;
        Serial.print("Sending huge page ");
        Serial.print(NO_DELAY?"WITHOUT": "WITH");
        Serial.print(" Nagle Algorithm...");
        ulong t1 = millis();
        server.send(200, "text/html", hugePage);
        ulong t2 = millis();
        Serial.print("done. Time: ");
        Serial.print(t2-t1);
        Serial.println("ms");
    });
    
    server.begin();
    Serial.println("HTTP server started");
}

void loop(void) {
    server.handleClient();
}

Debug Messages

Sending huge page WITH Nagle Algorithm...done. Time: 15542ms
Sending huge page WITHOUT Nagle Algorithm...done. Time: 318ms

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions