Skip to content

Commit 2c87bd2

Browse files
VSadovCopilot
andauthored
Reduce reader/writer contention in Pipelines (dotnet#130884)
The change deals with excessive contentions coming from Pipelines in some benchmarks when reader and writer contend for the lock used to protect shared state of the Pipe. Changes: * move some operations that may take nontrivial time while not requiring locking outside of the lock. That is mostly renting/returning byte buffers. That could end up in allocations or coordinating with other threads accessing the pool, but by itself does not need to lock the Pipe. * make the internal per-Pipe pool of segment objects a FIFO, so that writer (which returns segments) and reader (which takes then out) operate at different ends of the pool - to reduce cache line sharing in rent/return operations. * switch the Pipe lock from monitor lock to Threading.Lock. This lock often inflates anyways. * schedule continuations to local ThreadPool queues (vs. global) - to keep logical "pipelining" running on the same physical thread, if possible. For example, writer thread, if it looks for more work after writing, would be preferred to take care of asynchronous reading and processing of written data, thus further reducing contention and improving locality of access. === effect on JSON asp.net benchmark running on a 56-core machine: * Contentions: ``` diff - Max Lock Contention (#/s) | 483 + Max Lock Contention (#/s) | 66 ``` * RPS: ``` diff - Requests/sec | 2,094,374 + Requests/sec | 2,148,465 ``` * Throughput: ```diff - Read throughput (MB/s) | 291.61 + Read throughput (MB/s) | 299.14 ``` --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 17c839c commit 2c87bd2

5 files changed

Lines changed: 243 additions & 126 deletions

File tree

src/libraries/System.IO.Pipelines/src/System.IO.Pipelines.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ System.IO.Pipelines.PipeReader</PackageDescription>
4646
<Compile Include="System\IO\Pipelines\ThrowHelper.cs" />
4747
<Compile Include="$(CommonPath)System\IO\StreamHelpers.CopyValidation.cs"
4848
Link="Common\System\IO\StreamHelpers.CopyValidation.cs" />
49+
<Compile Include="$(CommonPath)System\Collections\Concurrent\SingleProducerSingleConsumerQueue.cs"
50+
Link="Common\System\Collections\Concurrent\SingleProducerSingleConsumerQueue.cs" />
51+
<Compile Include="$(CommonPath)System\Collections\Concurrent\IProducerConsumerQueue.cs"
52+
Link="Common\System\Collections\Concurrent\IProducerConsumerQueue.cs" />
53+
<Compile Include="$(CommonPath)Internal\Padding.cs"
54+
Link="Common\Internal\Padding.cs" />
4955
</ItemGroup>
5056

5157
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">

0 commit comments

Comments
 (0)