Ethernet Library slows entire program

HI everyone,

I've got an issue where my entire multi-threaded program runs at incredibly slow speed (seconds between loops) while using the Ethernet Library. I have narrowed it down to the ethernet server itself (if I have the actual modbus code commented out, just starting the ethernet server has the same slow-down effect.)

The same huge delays happen if there is a TCP connection on the other side or not (the other TCP device)

Am I doing something obviously wrong?

void startEthernet(bool idebug)
{

    // start the Ethernet connection and the server:
    Ethernet.begin(mac, ip);

    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
        if (idebug) {
            Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
        }
        while (true) {
            delay(1); // do nothing, no point running without Ethernet hardware
        }
    }
    if (Ethernet.linkStatus() == LinkOFF) {
        if (idebug) {
            Serial.println("Ethernet cable is not connected.");
        }
    }

    // start the server
    ethServer.begin();

    // start the Modbus TCP server
    if (!modbusTCPServer.begin()) {
        if (idebug) {
            Serial.println("Failed to start Modbus TCP Server!");
        }
        while (1);
    }
}

The background:

I'm building a CNC toolchanger where the h7 Machine control is connected to my pc using modbus TCP and I wrote a state machine to run on the H7 to control the toolchanger carousel, valves, etc...

Due to a "feature" in the only modbus TCP library I could get to work (ArduinoModbus), the server only stays connected if there is a blocking 'while' in the main loop (see below). So I ended up going multi-threaded.

    // listen for incoming clients
    EthernetClient client = ethServer.available();

    if (client) {
        // a new client connected
        digitalWrite(LEDG, HIGH);
        Serial.println("new client");

        // let the Modbus TCP accept the connection 
        modbusTCPServer.accept(client);

        while (client.connected()) {

            digitalWrite(LEDR, HIGH); //Turn on the light to show connection

            delay(25);// Wait for other threads to run
           //Update output registers here (check for new)
            updateOutputHregs(false);
            modbusTCPServer.poll();
            //Update input registers here (check for new)
            updateInputHregs(false);
            delay(50);// Wait for other threads to run
            counter++;

        }

        Serial.println("client disconnected");
        digitalWrite(LEDR, LOW); //Turn off the light to show connection
    }
    digitalWrite(LEDG, LOW); //Turn off the light to show connection

I therefore split the code into 2 parts: the modbus TCP code and the state machine code (I was originally intending to run one on the M7 and the other on the M4 until I realized this was (nearly) impossible in practice. Thus, I am now running them threaded on the M7 core using the Scheduler library.

I've tested each of the codebases individually and everything is fine. The significant delay comes only when the ethernet server is started.