Skip to content

Commit 7369133

Browse files
Small String Optimization (#5690)
Reduce String memory overhead from 24 bytes to 16 bytes by limiting the maximum string length to <64Kbytes (which is larger than heap so no effective problem). Add Small String Optimization, SSO, which instead of allocating pointers to small strings on the heap will store the string in place of the pointer in the class. This should reduce memory fragmentation as Save up to 12 chars (11 + \0) in String itself by using the terminating \0 in the inline string as a flag to identify if this is a SSO or a heap string. Add a host test that verifies that no memory is allocated until a full 11 characters are assigned to a string, as well as checking all intermediate values. No user code changes should be required to work with this optimization.
1 parent 1959311 commit 7369133

File tree

4 files changed

+390
-230
lines changed

4 files changed

+390
-230
lines changed

cores/esp8266/StreamString.cpp

+66-66
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,66 @@
1-
/**
2-
StreamString.cpp
3-
4-
Copyright (c) 2015 Markus Sattler. All rights reserved.
5-
This file is part of the esp8266 core for Arduino environment.
6-
7-
This library is free software; you can redistribute it and/or
8-
modify it under the terms of the GNU Lesser General Public
9-
License as published by the Free Software Foundation; either
10-
version 2.1 of the License, or (at your option) any later version.
11-
12-
This library is distributed in the hope that it will be useful,
13-
but WITHOUT ANY WARRANTY; without even the implied warranty of
14-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15-
Lesser General Public License for more details.
16-
17-
You should have received a copy of the GNU Lesser General Public
18-
License along with this library; if not, write to the Free Software
19-
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20-
21-
*/
22-
23-
#include <Arduino.h>
24-
#include "StreamString.h"
25-
26-
size_t StreamString::write(const uint8_t *data, size_t size) {
27-
if(size && data) {
28-
if(reserve(length() + size + 1)) {
29-
memcpy((void *) (buffer + len), (const void *) data, size);
30-
len += size;
31-
*(buffer + len) = 0x00; // add null for string end
32-
return size;
33-
}
34-
}
35-
return 0;
36-
}
37-
38-
size_t StreamString::write(uint8_t data) {
39-
return concat((char) data);
40-
}
41-
42-
int StreamString::available() {
43-
return length();
44-
}
45-
46-
int StreamString::read() {
47-
if(length()) {
48-
char c = charAt(0);
49-
remove(0, 1);
50-
return c;
51-
52-
}
53-
return -1;
54-
}
55-
56-
int StreamString::peek() {
57-
if(length()) {
58-
char c = charAt(0);
59-
return c;
60-
}
61-
return -1;
62-
}
63-
64-
void StreamString::flush() {
65-
}
66-
1+
/**
2+
StreamString.cpp
3+
4+
Copyright (c) 2015 Markus Sattler. All rights reserved.
5+
This file is part of the esp8266 core for Arduino environment.
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
21+
*/
22+
23+
#include <Arduino.h>
24+
#include "StreamString.h"
25+
26+
size_t StreamString::write(const uint8_t *data, size_t size) {
27+
if(size && data) {
28+
if(reserve(length() + size + 1)) {
29+
memcpy((void *) (wbuffer() + len()), (const void *) data, size);
30+
setLen(len() + size);
31+
*(wbuffer() + len()) = 0x00; // add null for string end
32+
return size;
33+
}
34+
}
35+
return 0;
36+
}
37+
38+
size_t StreamString::write(uint8_t data) {
39+
return concat((char) data);
40+
}
41+
42+
int StreamString::available() {
43+
return length();
44+
}
45+
46+
int StreamString::read() {
47+
if(length()) {
48+
char c = charAt(0);
49+
remove(0, 1);
50+
return c;
51+
52+
}
53+
return -1;
54+
}
55+
56+
int StreamString::peek() {
57+
if(length()) {
58+
char c = charAt(0);
59+
return c;
60+
}
61+
return -1;
62+
}
63+
64+
void StreamString::flush() {
65+
}
66+

0 commit comments

Comments
 (0)