Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lwip): Add early out in NetworkUDP::parsePacket() when socket has no data #10075

Merged

Conversation

nitz
Copy link
Contributor

@nitz nitz commented Jul 25, 2024

Description of Change

Previously, NetworkUDP::parsePacket() would take the time to allocate a 1460 byte buffer to call recvfrom() with, immediately freeing it if there was no data read. This is an waste of cycles and provides no benefit.

This change has parsePacket() check if there is available data via ioctl() with FIONREAD first, saving the allocation and thus significantly increasing performance in no data situations.

Tests scenarios

Tested on Arduino-esp32 core v2.0.3 on an ESP32-C3 dev board (XAIO_ESP32C3) by sending UDP messages from my development machine.

In a near-empty sketch, invoking parsePacket() every loop() call, I was executing about 20,000 calls per second. After the change, the performance was closer to 35,000 calls/second.

Related links

I did not find any related issues. However, as I was making this change, I did note the parsePacket()'s allocation behavior overall. It seems excessive to allocate 1460 bytes every single call to it, here:

char *buf = (char *)malloc(1460);

Even with the change in this PR, that buffer is allocated, and then written into a second buffer, rx_buffer, which is allocated at the end of the function, here:

}
if (len > 0) {
rx_buffer = new (std::nothrow) cbuf(len);
rx_buffer->write(buf, len);
}
free(buf);

It seems like it would be a better option to at least attempt to only allocate a single time; or to consider moving the temporary buffer allocation to be static (or potentially stack allocated.)

@CLAassistant
Copy link

CLAassistant commented Jul 25, 2024

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

github-actions bot commented Jul 25, 2024

Messages
📖 🎉 Good Job! All checks are passing!

👋 Hello nitz, we appreciate your contribution to this project!


Click to see more instructions ...


This automated output is generated by the PR linter DangerJS, which checks if your Pull Request meets the project's requirements and helps you fix potential issues.

DangerJS is triggered with each push event to a Pull Request and modify the contents of this comment.

Please consider the following:
- Danger mainly focuses on the PR structure and formatting and can't understand the meaning behind your code or changes.
- Danger is not a substitute for human code reviews; it's still important to request a code review from your colleagues.
- To manually retry these Danger checks, please navigate to the Actions tab and re-run last Danger workflow.

Review and merge process you can expect ...


We do welcome contributions in the form of bug reports, feature requests and pull requests.

1. An internal issue has been created for the PR, we assign it to the relevant engineer.
2. They review the PR and either approve it or ask you for changes or clarifications.
3. Once the GitHub PR is approved we do the final review, collect approvals from core owners and make sure all the automated tests are passing.
- At this point we may do some adjustments to the proposed change, or extend it by adding tests or documentation.
4. If the change is approved and passes the tests it is merged into the default branch.

Generated by 🚫 dangerJS against 0e5f420

…as no data

Previously, `NetworkUDP::parsePacket()` would take the time to allocate a 1460 byte buffer
to call `recvfrom()` with, immediately freeing it if there was no data read.

This change has it check if there is available data via `ioctl()` with `FIONREAD` first,
saving the allocation and thus significantly increasing performance in no data situations.
@nitz nitz force-pushed the networkudp-parse-packet-perf-improvements branch from 4871631 to 47993cd Compare July 25, 2024 19:27
@lucasssvaz lucasssvaz added the Area: Libraries Issue is related to Library support. label Jul 25, 2024
@VojtechBartoska VojtechBartoska added the Status: Review needed Issue or PR is awaiting review label Jul 26, 2024
@VojtechBartoska
Copy link
Collaborator

thank you for the PR @nitz, we will review it during next week 💯

@@ -297,6 +297,13 @@ int NetworkUDP::parsePacket() {
struct sockaddr_storage si_other_storage; // enough storage for v4 and v6
socklen_t slen = sizeof(sockaddr_storage);
int len;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here len should be set to 0, else it might not be initialized and might not be 0 for the next check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! I've clearly spent too much time in C# lately, where "out" variables are guaranteed to be assigned 😂

@me-no-dev me-no-dev added Status: Pending Merge Pull Request is ready to be merged and removed Status: Review needed Issue or PR is awaiting review labels Jul 31, 2024
Copy link
Contributor

github-actions bot commented Jul 31, 2024

Test Results

 56 files   -  61   56 suites   - 61   4m 52s ⏱️ - 1h 19m 41s
 21 tests  -   9   21 ✅  -   9  0 💤 ±0  0 ❌ ±0 
135 runs   - 118  135 ✅  - 118  0 💤 ±0  0 ❌ ±0 

Results for commit 0e5f420. ± Comparison against base commit 0fa4aa6.

This pull request removes 9 tests.
performance.coremark.test_coremark ‑ test_coremark
performance.fibonacci.test_fibonacci ‑ test_fibonacci
performance.psramspeed.test_psramspeed ‑ test_psramspeed
performance.ramspeed.test_ramspeed ‑ test_ramspeed
performance.superpi.test_superpi ‑ test_superpi
test_touch_errors
test_touch_interrtupt
test_touch_read
validation.periman.test_periman ‑ test_periman

♻️ This comment has been updated with latest results.

@VojtechBartoska VojtechBartoska added this to the 3.0.4 milestone Jul 31, 2024
Copy link
Contributor

Memory usage test (comparing PR against master branch)

The table below shows the summary of memory usage change (decrease - increase) in bytes and percentage for each target.

MemoryFLASH [bytes]FLASH [%]RAM [bytes]RAM [%]
TargetDECINCDECINCDECINCDECINC
ESP32S3💚 -4⚠️ +1600.00⚠️ +0.02000.000.00
ESP32S2💚 -20⚠️ +1520.00⚠️ +0.02💚 -160💚 -0.040.00
ESP32C3💚 -10⚠️ +1400.00⚠️ +0.02000.000.00
ESP32C6💚 -2⚠️ +1380.00⚠️ +0.02000.000.00
ESP32H2000.000.00000.000.00
ESP32💚 -24⚠️ +1560.00⚠️ +0.02💚 -160💚 -0.030.00
Click to expand the detailed deltas report [usage change in BYTES]
TargetESP32S3ESP32S2ESP32C3ESP32C6ESP32H2ESP32
ExampleFLASHRAMFLASHRAMFLASHRAMFLASHRAMFLASHRAMFLASHRAM
Ethernet/examples/ETH_W5500_Arduino_SPI000000000000
Ethernet/examples/ETH_W5500_IDF_SPI000000000000
NetworkClientSecure/examples/WiFiClientInsecure00000000--00
NetworkClientSecure/examples/WiFiClientPSK00000000--00
NetworkClientSecure/examples/WiFiClientSecure00000000--00
NetworkClientSecure/examples/WiFiClientSecureEnterprise00000000--00
NetworkClientSecure/examples/WiFiClientSecureProtocolUpgrade00000000--00
NetworkClientSecure/examples/WiFiClientShowPeerCredentials00000000--00
NetworkClientSecure/examples/WiFiClientTrustOnFirstUse00000000--00
PPP/examples/PPP_Basic000000000000
WebServer/examples/AdvancedWebServer00000000--00
WebServer/examples/FSBrowser00000000--00
WebServer/examples/Filters00000000--00
WebServer/examples/HelloServer00000000--00
WebServer/examples/HttpAdvancedAuth⚠️ +1360⚠️ +1520⚠️ +1380⚠️ +1380--⚠️ +1440
WebServer/examples/HttpAuthCallback⚠️ +1360⚠️ +1520⚠️ +1380⚠️ +1380--⚠️ +1480
WebServer/examples/HttpAuthCallbackInline⚠️ +1360⚠️ +1520⚠️ +1380⚠️ +1380--⚠️ +1440
WebServer/examples/HttpBasicAuth⚠️ +1360⚠️ +1520⚠️ +1380⚠️ +1380--⚠️ +1440
WebServer/examples/HttpBasicAuthSHA1⚠️ +1520⚠️ +1400⚠️ +1380⚠️ +1380--⚠️ +1560
WebServer/examples/HttpBasicAuthSHA1orBearerToken⚠️ +1320⚠️ +1360⚠️ +1380⚠️ +1380--⚠️ +1440
WebServer/examples/MultiHomedServers00000000--00
WebServer/examples/PathArgServer00000000--00
WebServer/examples/SDWebServer00000000--00
WebServer/examples/SimpleAuthentification00000000--00
WebServer/examples/UploadHugeFile00000000--00
WebServer/examples/WebServer00000000--00
WebServer/examples/WebUpdate00000000--00
WiFi/examples/FTM/FTM_Initiator00000000--00
WiFi/examples/FTM/FTM_Responder00000000--00
WiFi/examples/SimpleWiFiServer00000000--00
WiFi/examples/WPS00000000--00
WiFi/examples/WiFiAccessPoint00000000--00
WiFi/examples/WiFiBlueToothSwitch00--0000--00
WiFi/examples/WiFiClient00000000--00
WiFi/examples/WiFiClientBasic💚 -40💚 -400000--00
WiFi/examples/WiFiClientConnect00000000--00
WiFi/examples/WiFiClientEnterprise00000000--00
WiFi/examples/WiFiClientEvents00000000--00
WiFi/examples/WiFiClientStaticIP00000000--00
WiFi/examples/WiFiIPv6⚠️ +1600⚠️ +1360⚠️ +1380⚠️ +1380--⚠️ +1440
WiFi/examples/WiFiMulti💚 -40💚 -400000--00
WiFi/examples/WiFiMultiAdvanced💚 -40💚 -20💚 -16💚 -10000--💚 -24💚 -16
WiFi/examples/WiFiScan💚 -40💚 -400000--00
WiFi/examples/WiFiScanAsync💚 -40💚 -400000--00
WiFi/examples/WiFiScanDualAntenna💚 -40💚 -4000💚 -20--00
WiFi/examples/WiFiSmartConfig00000000--00
WiFi/examples/WiFiTelnetToSerial💚 -40💚 -40💚 -2000--00
WiFi/examples/WiFiUDPClient⚠️ +1280⚠️ +1360⚠️ +1400⚠️ +1380--⚠️ +1520
Ethernet/examples/ETH_LAN8720----------00
Ethernet/examples/ETH_TLK110----------00

@me-no-dev me-no-dev merged commit ad5aaf8 into espressif:master Jul 31, 2024
73 of 79 checks passed
@nitz nitz deleted the networkudp-parse-packet-perf-improvements branch July 31, 2024 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Libraries Issue is related to Library support. Status: Pending Merge Pull Request is ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants