-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Reduce the size of the pipe #49270
Reduce the size of the pipe #49270
Conversation
src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/Pipe.cs
Outdated
Show resolved
Hide resolved
Something is broken but I'm having a hard time running tests locally 😢 |
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.
Before 368 bytes. After 256 bytes
Nice! 30% saving, and options
is likely a shared object by all the pipes so going via the ref should mean its in cache for all pipelines rather than being a different 48 byte block for each Pipe; and since PipeOptions
is immutable, seem safe.
Does that mean SocketAsyncEventArgs
is now top chonk at 320 bytes?
Yep am looking into the per connection memory and SocketAsyncEventArgs is up there. |
src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/BufferSegmentStack.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/BufferSegmentStack.cs
Outdated
Show resolved
Hide resolved
- Use the pipe itself as the synchronization object - Store the options instance as a way to reference shared settings - Added a field to PipeOptions for storing if the Pool is the ArrayPool implementation of the MemoryPool - Shrink PipeAwaitable in the common case - Move the ExecutionContext and SynchronizationContext into a typed called the SchedulingContext. These types are mostly used with async await and it's extremely rare to have to capture any of this state. - Shrink the size of PipeCompletion - Since completion callbacks are deprecated they are rarely set. We remove the pool and the other fields and just store a list (which should be rarely used now). - Reduce the default segment pool size to 4 items = 16K buffered - The original size was optimized to avoid pool resizes but we need to balance idle memory and the potential resize cost of the resize.
f112377
to
7a196dc
Compare
OK I'm done tweaking this thing. |
private readonly bool _useSynchronizationContext; | ||
// The options instance | ||
private readonly PipeOptions _options; | ||
private readonly object _sync = new object(); |
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'm in no way saying it's worth it, but if you really wanted to avoid this new object()
and you wanted to use that buffer segment array you tried to in a previous commit, you could instead still keep this field and just store that array into this field in the ctor. Then you're reusing the same array instance even the buffer stack is resized. There are downsides, though, namely you'd be keeping alive what I assume is a much larger object, and if that array referenced other stuff, anything it referenced.
src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/PipeCompletion.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/PipeCompletionCallbacks.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/PipeOptions.cs
Outdated
Show resolved
Hide resolved
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.
Some build failures, but otherwise LGTM
Is this crossgen issue a known thing? |
Use the pipe itself as the synchronization objectThis makes it possible to share common options across multiple pipe instances and not pay that extra size cost.
Using the ObjectLayoutInspector:
Before
Size: 368 bytes. Paddings: 27 bytes (%7 of empty space)
After
Size: 256 bytes. Paddings: 22 bytes (%8 of empty space)Size: 264 bytes. Paddings: 22 bytes (%8 of empty space)
Simulation of 1M connections using pipes:
Before:
After: