-
Notifications
You must be signed in to change notification settings - Fork 28.9k
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
Conversation
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. |
@@ -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 |
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.
nit: initial capacity in bytes. Also assert for >= 0
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.
done
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.
I would assert >= 0, 0
is still a valid initial buffer size
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.
LGTM
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 |
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.
/// [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); |
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.
add expect(() => WriteBuffer(startCapacity: 1), returnsNormally);
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.
done
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. |
By selecting a larger starting buffer size we get a 28% speed increase on
StandardMessageCodec_heterogenous_list
and a 13% speed increase onStandardMessageCodec_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:
after:
Pre-launch Checklist
///
).If you need help, consider asking for advice on the #hackers-new channel on Discord.