From fd3e6787a0d742d874e407fac08971708131ccf1 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Mon, 9 Dec 2019 11:23:49 -0800 Subject: [PATCH] Use 128B chunks instead of 1B writes in Print::print(FlashStringHelper) 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. --- cores/esp8266/Print.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/cores/esp8266/Print.cpp b/cores/esp8266/Print.cpp index ff640edae1..a1add05781 100644 --- a/cores/esp8266/Print.cpp +++ b/cores/esp8266/Print.cpp @@ -104,11 +104,19 @@ size_t Print::printf_P(PGM_P format, ...) { size_t Print::print(const __FlashStringHelper *ifsh) { PGM_P p = reinterpret_cast(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; }