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

Apply String move semantics to reduce memory re-allocations #2119

Merged
merged 9 commits into from
Oct 23, 2020

Conversation

mikee47
Copy link
Contributor

@mikee47 mikee47 commented Oct 19, 2020

  • Apply coding style to WString
  • Add String::getBuffer and String::setBuffer to support general move operations
  • Optimise readString method for MemoryDataStream
  • Add MemoryDataStream(String&&) constructor: heap allocation required only if String is using SSO mode.
  • Provide MemoryDataStream::readString override to perform move when entire content of stream is requested.
  • Add overloads to various classes to support r-values.
  • Use overload instead of optional length parameter for String constructor and setString().

@mikee47 mikee47 force-pushed the feature/string-optimisations branch 3 times, most recently from 712b7f6 to ee5c89c Compare October 19, 2020 22:25
@slaff slaff added this to the 4.2.0 milestone Oct 20, 2020
@slaff
Copy link
Contributor

slaff commented Oct 20, 2020

@mikee47 Maybe you can port also this change from Arduino: esp8266/Arduino@c24109f ?

Comment on lines 96 to 111
// Are we're after the entire contents of this stream?
if(readPos != 0 || maxLen < size) {
// No: do a regular read
return IDataSourceStream::readString(maxLen);
}

// Move content into the String object
ensureCapacity(size + 1); // Ensure size < capacity
String s;
if(s.setBuffer({buffer, capacity, size})) {
buffer = nullptr;
readPos = 0;
size = 0;
capacity = 0;
}
return s;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm on the fence about this behaviour. It's probably correct in 99% of situations, but the safer option might be to add an overload such as size_t readString(String& s);

Copy link
Contributor

Choose a reason for hiding this comment

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

but the safer option might be to add an overload such as size_t readString(String& s);

Got for it as long as the safer option does not impact compatibility, memory usage and speed.

@mikee47 mikee47 force-pushed the feature/string-optimisations branch 2 times, most recently from 61ff1d9 to 7e63e3c Compare October 20, 2020 16:05
@slaff
Copy link
Contributor

slaff commented Oct 21, 2020

@mikee47 Ping me when this PR is ready.

Provide overrides in `FileStream` and `DataSourceStream` classes.

IDataSourceStream::readBytes() calls readMemoryBlock(), then seek(), by default.
Classes can override one or both of these methods for more efficient operation.

Applications should use whichever is more appropriate.

virtualise readString() and update stream position for consistency with `Stream` method

**maxLen parameter not optional**
…ve operations

Add MemoryDataStream(String&&) constructor: heap allocation required only
if String is using SSO mode.

Add overloads to various classes to support r-values.
@mikee47 mikee47 force-pushed the feature/string-optimisations branch from 7e63e3c to 1f6a558 Compare October 22, 2020 22:05
@mikee47 mikee47 force-pushed the feature/string-optimisations branch from 1f6a558 to 741a8ca Compare October 22, 2020 22:07
@mikee47 mikee47 force-pushed the feature/string-optimisations branch from 741a8ca to d2c284c Compare October 22, 2020 22:20
@mikee47
Copy link
Contributor Author

mikee47 commented Oct 22, 2020

@slaff OK, I'm done with this now!

@slaff slaff removed the 3 - Review label Oct 23, 2020
@slaff slaff merged commit 1a3805e into SmingHub:develop Oct 23, 2020
@mikee47 mikee47 deleted the feature/string-optimisations branch October 23, 2020 07:59
@slaff slaff mentioned this pull request Dec 3, 2020
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.

2 participants