Skip to content

Commit fd3e678

Browse files
committedDec 9, 2019
Use 128B chunks instead of 1B writes in Print::print(FlashStringHelper)
Fixes esp8266#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.
1 parent 7605dc1 commit fd3e678

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed
 

‎cores/esp8266/Print.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,19 @@ size_t Print::printf_P(PGM_P format, ...) {
104104
size_t Print::print(const __FlashStringHelper *ifsh) {
105105
PGM_P p = reinterpret_cast<PGM_P>(ifsh);
106106

107+
char buff[128] __attribute__ ((aligned(4)));
108+
auto len = strlen_P(p);
107109
size_t n = 0;
108-
while (1) {
109-
uint8_t c = pgm_read_byte(p++);
110-
if (c == 0) break;
111-
n += write(c);
110+
while (n < len) {
111+
int to_write = std::min(sizeof(buff), len - n);
112+
memcpy_P(buff, p, to_write);
113+
auto written = write(buff, to_write);
114+
n += written;
115+
p += written;
116+
if (!written) {
117+
// Some error, write() should write at least 1 byte before returning
118+
break;
119+
}
112120
}
113121
return n;
114122
}

0 commit comments

Comments
 (0)