Commit 2c87bd2
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
- tests
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
49 | 55 | | |
50 | 56 | | |
51 | 57 | | |
| |||
0 commit comments