Skip to content

Commit

Permalink
Small String Optimization (esp8266#5690)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
earlephilhower authored Feb 8, 2019
1 parent 1959311 commit 7369133
Show file tree
Hide file tree
Showing 4 changed files with 390 additions and 230 deletions.
132 changes: 66 additions & 66 deletions cores/esp8266/StreamString.cpp
Original file line number Diff line number Diff line change
@@ -1,66 +1,66 @@
/**
StreamString.cpp
Copyright (c) 2015 Markus Sattler. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <Arduino.h>
#include "StreamString.h"

size_t StreamString::write(const uint8_t *data, size_t size) {
if(size && data) {
if(reserve(length() + size + 1)) {
memcpy((void *) (buffer + len), (const void *) data, size);
len += size;
*(buffer + len) = 0x00; // add null for string end
return size;
}
}
return 0;
}

size_t StreamString::write(uint8_t data) {
return concat((char) data);
}

int StreamString::available() {
return length();
}

int StreamString::read() {
if(length()) {
char c = charAt(0);
remove(0, 1);
return c;

}
return -1;
}

int StreamString::peek() {
if(length()) {
char c = charAt(0);
return c;
}
return -1;
}

void StreamString::flush() {
}

/**
StreamString.cpp
Copyright (c) 2015 Markus Sattler. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <Arduino.h>
#include "StreamString.h"

size_t StreamString::write(const uint8_t *data, size_t size) {
if(size && data) {
if(reserve(length() + size + 1)) {
memcpy((void *) (wbuffer() + len()), (const void *) data, size);
setLen(len() + size);
*(wbuffer() + len()) = 0x00; // add null for string end
return size;
}
}
return 0;
}

size_t StreamString::write(uint8_t data) {
return concat((char) data);
}

int StreamString::available() {
return length();
}

int StreamString::read() {
if(length()) {
char c = charAt(0);
remove(0, 1);
return c;

}
return -1;
}

int StreamString::peek() {
if(length()) {
char c = charAt(0);
return c;
}
return -1;
}

void StreamString::flush() {
}

Loading

0 comments on commit 7369133

Please sign in to comment.