Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Small String Optimization #5690

Merged
merged 13 commits into from
Feb 8, 2019
Merged

Conversation

earlephilhower
Copy link
Collaborator

@earlephilhower earlephilhower commented Jan 28, 2019

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 Strings
of up to 12 chars (11 + \0) will not need any space on the heap at all.

No user code changes should be required to work with this optimization.

Earle F. Philhower, III added 2 commits January 28, 2019 14:03

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
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
Strings of 0-3 characters will not need extra malloc()s.

No user code changes should be required to work with this optimization.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
We only have 15, not 16, bits of length, so adjust the check
accordingly.
Copy link
Collaborator

@d-a-v d-a-v left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice feature !

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Memory fragmentation is worse than not saving any RAM per String instance,
so extend the SSO size to 8 bytes (7 chars + '\0'), and making the total
String size the same size as it was before.

Remove any hardcoded SSO size and allow configuration via an enum, and
replace the magic max-capacity with an enum tor clarity.

Add a host test that verifies that no memory is allocated until a
full 8 characters are assigned to a string, as well as checking all
intermediate values.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
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.

Fix DOS endlines present in StreamString, for some reason.
@earlephilhower earlephilhower changed the title Add Small String Optimization, min String size Add Small String Optimization Jan 31, 2019
@earlephilhower earlephilhower added this to the 2.6.0 milestone Jan 31, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Because pointers are 8 bytes (and 8-bytes aligned) on x64, the structure
used to store SSO needs to make sure the terminal \0 won't overwrite
some of that buffer.  Adjust the SSOSIZE dynamically depending on the
size of the struct ptr.

Refactor to add a setBuffer method, like the setConfig, setSSO, etc.

Fix some issues found when using SSO strings and functions which modify
len() dynamically.
@devyte devyte self-requested a review February 7, 2019 03:34
@earlephilhower earlephilhower merged commit 7369133 into esp8266:master Feb 8, 2019
@earlephilhower earlephilhower deleted the sso branch February 8, 2019 17:37
@d-a-v d-a-v mentioned this pull request Feb 11, 2019
TD-er added a commit to TD-er/Arduino that referenced this pull request Mar 18, 2019
A fix for some issue introduced in PR esp8266#5690
See discussion in esp8266#5883
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants