Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Increased WriteBuffers starting capacity to 64 bytes. #101790

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

Merged
merged 4 commits into from
Apr 13, 2022

Conversation

gaaclarke
Copy link
Member

@gaaclarke gaaclarke commented Apr 12, 2022

By selecting a larger starting buffer size we get a 28% speed increase on StandardMessageCodec_heterogenous_list and a 13% speed increase on StandardMessageCodec_heterogenous_map.

The reason this is faster is that the buffer was so small that we were resizing the buffer a lot and the resizes are exponentially more expensive since they are copying the last region that was copied. By starting at a larger size we are removing that feedback.

It probably has the best performance gains for payloads that are between 8 bytes and 256 bytes. Smaller than that and we won't be resizing, larger than that and the savings becomes insignificant to larger resizing. It didn't noticeably effect payloads <= 8bytes

Do not land until #101767 lands.

This should also be covered by previous tests, it's just a performance change.

before:

flutter: :::JSON::: {"StandardMessageCodec_null":0.00137,"StandardMessageCodec_int":0.26432,"StandardMessageCodec_string":0.59417,"StandardMessageCodec_heterogenous_list":1.21914,"StandardMessageCodec_heterogenous_map":2.28828}

after:

flutter: :::JSON::: {"StandardMessageCodec_null":0.00096,"StandardMessageCodec_int":0.26681,"StandardMessageCodec_string":0.52084,"StandardMessageCodec_heterogenous_list":0.87832,"StandardMessageCodec_heterogenous_map":1.97065}

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@flutter-dashboard flutter-dashboard bot added the framework flutter/packages/flutter repository. See also f: labels. label Apr 12, 2022
@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@gaaclarke gaaclarke requested a review from jonahwilliams April 12, 2022 22:21
@@ -13,10 +13,12 @@ import 'dart:typed_data';
/// The byte order used is [Endian.host] throughout.
class WriteBuffer {
/// Creates an interface for incrementally building a [ByteData] instance.
factory WriteBuffer() {
/// [startCapacity] determines the start size of the [WriteBuffer]. The closer
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: initial capacity in bytes. Also assert for >= 0

Copy link
Member Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

Choose a reason for hiding this comment

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

I would assert >= 0, 0 is still a valid initial buffer size

Copy link
Contributor

@jonahwilliams jonahwilliams left a comment

Choose a reason for hiding this comment

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

LGTM

@jonahwilliams
Copy link
Contributor

For testing, how about making sure we can set the capacity and that it throws an assert if < 0 and doesn't if >= 0?

@gaaclarke
Copy link
Member Author

For testing, how about making sure we can set the capacity and that it throws an assert if < 0 and doesn't if >= 0?

done

@@ -16,6 +16,7 @@ class WriteBuffer {
/// [startCapacity] determines the start size of the [WriteBuffer]. The closer
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// [startCapacity] determines the start size of the [WriteBuffer]. The closer
/// [startCapacity] determines the initial capacity of the [WriteBuffer]. The closer

@@ -124,5 +124,8 @@ void main() {
write.done();
expect(() => write.done(), throwsStateError);
});
test('empty WriteBuffer', () {
expect(() => WriteBuffer(startCapacity: 0), throwsAssertionError);
Copy link
Contributor

Choose a reason for hiding this comment

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

add expect(() => WriteBuffer(startCapacity: 1), returnsNormally);

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@gaaclarke
Copy link
Member Author

We should probably do this for the MethodChannel usage of WriteBuffers too. Since it's late I'll just let this one land and I'll make a new PR tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants