Skip to content

fix(core): StreamString performance improvements with large buffers (StreamString::readBytes()) #11229

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cores/esp32/StreamString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ int StreamString::read() {
return -1;
}

size_t StreamString::readBytes(char *buffer, size_t buffLen) {
if (!buffLen || !buffer) {
return 0;
}
const uint32_t start = millis();
size_t count = 0;
do {
if (this->length()) {
const size_t available = min(buffLen - count, (size_t)this->length());
memcpy(buffer + count, c_str(), available);
remove(0, available);
count += available;
}
if (count == buffLen || _timeout == 0) {
return count;
}
} while (millis() - start < _timeout);
return count;
}

int StreamString::peek() {
if (length()) {
char c = charAt(0);
Expand Down
1 change: 1 addition & 0 deletions cores/esp32/StreamString.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class StreamString : public Stream, public String {

int available() override;
int read() override;
size_t readBytes(char *buffer, size_t count) override;
int peek() override;
void flush() override;
};
Expand Down
Loading