1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-16 00:43:00 +03:00

Use 128B chunks instead of 1B writes in Print::print(FlashStringHelper) (#6893)

Fixes #6524

Should help with speed of output when printing large flash strings to
things like a file or a TCP connection.

Use a 128 byte chunk in a temp buffer to send data using write(),
reducing the # of write calls by ~128x.
This commit is contained in:
Earle F. Philhower, III
2019-12-09 18:04:49 -08:00
committed by Develo
parent 7605dc1643
commit 759ba27b62

View File

@ -104,11 +104,19 @@ size_t Print::printf_P(PGM_P format, ...) {
size_t Print::print(const __FlashStringHelper *ifsh) {
PGM_P p = reinterpret_cast<PGM_P>(ifsh);
char buff[128] __attribute__ ((aligned(4)));
auto len = strlen_P(p);
size_t n = 0;
while (1) {
uint8_t c = pgm_read_byte(p++);
if (c == 0) break;
n += write(c);
while (n < len) {
int to_write = std::min(sizeof(buff), len - n);
memcpy_P(buff, p, to_write);
auto written = write(buff, to_write);
n += written;
p += written;
if (!written) {
// Some error, write() should write at least 1 byte before returning
break;
}
}
return n;
}