-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
We only have 15, not 16, bits of length, so adjust the check accordingly.
d-a-v
approved these changes
Jan 29, 2019
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice feature !
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.
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.
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
approved these changes
Feb 8, 2019
Merged
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.