Skip to content

Commit ab309e4

Browse files
earlephilhowerme-no-dev
authored andcommitted
Copy ESP8266 String w/SSO to ESP32 repo (#2715)
I redid the ESP8266 WString library to enable small string optimization (SSO) a while back, and think it would be helpful even on the ESP32 with its higher memory complement. SSO avoids lots of tiny mallocs() on the heap which cause fragmentation by using the memory in the class object itself to store the actual string and only mallocing() for buffers that are larger than what can fit in thie class object. Modern C++ std::string implementations have this optimization as well, but since we're using Arduino strings we had to roll our own.
1 parent 932666a commit ab309e4

File tree

3 files changed

+656
-729
lines changed

3 files changed

+656
-729
lines changed

Diff for: cores/esp32/StreamString.cpp

+10-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
StreamString.cpp
33
44
Copyright (c) 2015 Markus Sattler. All rights reserved.
5+
This file is part of the esp8266 core for Arduino environment.
56
67
This library is free software; you can redistribute it and/or
78
modify it under the terms of the GNU Lesser General Public
@@ -22,31 +23,27 @@
2223
#include <Arduino.h>
2324
#include "StreamString.h"
2425

25-
size_t StreamString::write(const uint8_t *data, size_t size)
26-
{
26+
size_t StreamString::write(const uint8_t *data, size_t size) {
2727
if(size && data) {
2828
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
29+
memcpy((void *) (wbuffer() + len()), (const void *) data, size);
30+
setLen(len() + size);
31+
*(wbuffer() + len()) = 0x00; // add null for string end
3232
return size;
3333
}
3434
}
3535
return 0;
3636
}
3737

38-
size_t StreamString::write(uint8_t data)
39-
{
38+
size_t StreamString::write(uint8_t data) {
4039
return concat((char) data);
4140
}
4241

43-
int StreamString::available()
44-
{
42+
int StreamString::available() {
4543
return length();
4644
}
4745

48-
int StreamString::read()
49-
{
46+
int StreamString::read() {
5047
if(length()) {
5148
char c = charAt(0);
5249
remove(0, 1);
@@ -56,16 +53,14 @@ int StreamString::read()
5653
return -1;
5754
}
5855

59-
int StreamString::peek()
60-
{
56+
int StreamString::peek() {
6157
if(length()) {
6258
char c = charAt(0);
6359
return c;
6460
}
6561
return -1;
6662
}
6763

68-
void StreamString::flush()
69-
{
64+
void StreamString::flush() {
7065
}
7166

0 commit comments

Comments
 (0)