Skip to content

Commit a07ec5c

Browse files
committed
WString: Optimize a bit
* move bodies of dtor, `init()` and `charAt()` to .h (implicitly inlined) * unify the description of initialization into one: `init()` (literally), and called from each ctors, `invalidate()` and `move()`. * invert the SSO state logic in order to make init state zeroed (as a result, inlined `init()` insns: 4 to 3) IROM size @ FSBrowser: 304836 -> 304596 (saved 240 from master)
1 parent cc042b9 commit a07ec5c

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

cores/esp8266/WString.cpp

-14
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,10 @@ String::String(double value, unsigned char decimalPlaces) {
118118
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
119119
}
120120

121-
String::~String() {
122-
invalidate();
123-
}
124-
125121
/*********************************************/
126122
/* Memory Management */
127123
/*********************************************/
128124

129-
inline void String::init(void) {
130-
setSSO(true);
131-
setLen(0);
132-
wbuffer()[0] = 0;
133-
}
134-
135125
void String::invalidate(void) {
136126
if(!isSSO() && wbuffer())
137127
free(wbuffer());
@@ -562,10 +552,6 @@ unsigned char String::endsWith(const String &s2) const {
562552
/* Character Access */
563553
/*********************************************/
564554

565-
char String::charAt(unsigned int loc) const {
566-
return operator[](loc);
567-
}
568-
569555
void String::setCharAt(unsigned int loc, char c) {
570556
if (loc < len())
571557
wbuffer()[loc] = c;

cores/esp8266/WString.h

+15-7
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ class String {
6969
explicit String(unsigned long, unsigned char base = 10);
7070
explicit String(float, unsigned char decimalPlaces = 2);
7171
explicit String(double, unsigned char decimalPlaces = 2);
72-
~String(void);
72+
~String() {
73+
invalidate();
74+
}
7375

7476
// memory management
7577
// return true on success, false on failure (in which case, the string
@@ -218,7 +220,9 @@ class String {
218220
}
219221

220222
// character access
221-
char charAt(unsigned int index) const;
223+
char charAt(unsigned int index) const {
224+
return operator[](index);
225+
}
222226
void setCharAt(unsigned int index, char c);
223227
char operator [](unsigned int index) const;
224228
char& operator [](unsigned int index);
@@ -287,19 +291,19 @@ class String {
287291
enum { SSOSIZE = sizeof(struct _ptr) + 4 - 1 }; // Characters to allocate space for SSO, must be 12 or more
288292
struct _sso {
289293
char buff[SSOSIZE];
290-
unsigned char len : 7; // Ensure only one byte is allocated by GCC for the bitfields
291-
unsigned char isSSO : 1;
294+
unsigned char len : 7; // Ensure only one byte is allocated by GCC for the bitfields
295+
unsigned char isHeap : 1;
292296
} __attribute__((packed)); // Ensure that GCC doesn't expand the flag byte to a 32-bit word for alignment issues
293297
enum { CAPACITY_MAX = 65535 }; // If typeof(cap) changed from uint16_t, be sure to update this enum to the max value storable in the type
294298
union {
295299
struct _ptr ptr;
296300
struct _sso sso;
297301
};
298302
// Accessor functions
299-
inline bool isSSO() const { return sso.isSSO; }
303+
inline bool isSSO() const { return !sso.isHeap; }
300304
inline unsigned int len() const { return isSSO() ? sso.len : ptr.len; }
301305
inline unsigned int capacity() const { return isSSO() ? (unsigned int)SSOSIZE - 1 : ptr.cap; } // Size of max string not including terminal NUL
302-
inline void setSSO(bool set) { sso.isSSO = set; }
306+
inline void setSSO(bool set) { sso.isHeap = !set; }
303307
inline void setLen(int len) { if (isSSO()) sso.len = len; else ptr.len = len; }
304308
inline void setCapacity(int cap) { if (!isSSO()) ptr.cap = cap; }
305309
inline void setBuffer(char *buff) { if (!isSSO()) ptr.buff = buff; }
@@ -308,7 +312,11 @@ class String {
308312
inline char *wbuffer() const { return isSSO() ? const_cast<char *>(sso.buff) : ptr.buff; } // Writable version of buffer
309313

310314
protected:
311-
void init(void);
315+
void init(void) {
316+
sso.isHeap = 0;
317+
sso.len = 0;
318+
sso.buff[0] = 0;
319+
}
312320
void invalidate(void);
313321
unsigned char changeBuffer(unsigned int maxStrLen);
314322

0 commit comments

Comments
 (0)