Repository navigation
Remove FileStream long pinning, and simplify synchronization - #51462
Conversation
|
Tagging subscribers to this area: @carlossanlop Issue DetailsThis PR has two changes for FileStream... The first is to remove the permanent pinning performed by BufferedFileStreamStrategy. Previously, on Windows when FileStream would allocate its buffer, it would also pin it as part of creating a PreallocatedOverlapped. Recent changes removed the PreallocatedOverlapped wrapping of the buffer, and replaced it with a buffer from the pinned-object heap. But POH allocations are considered to be gen2, which can then have a negative app-wide impact if lots of FileStreams are created and thrown away quickly. Instead, this PR just removes that pinning entirely. BufferedFileStreamStrategy's buffer, like any other buffer, will just be pinned for the duration of each individual read/write async operation, using Memory.Pin. I do not see any degradation in the benchmarks as a result. cc: @jkotas The second change is to significantly simplify how the async completion is handled. There's currently a complicated set of interlocked operations that transition the promise through multiple stages, to account for the possibility of race conditions between various operations. We can instead get rid of all of that, and just use an event to handle the rare case where additional configuration of an async operation after that async operation was just initiated (e.g. registering for cancellation) doesn't race with concurrent completion of that same operation. @adamsitnik, I don't see regressions from these changes, but the benchmarks also appear to be quite noisy, at least on my machine. If you could give them a go, that'd be helpful. Thanks.
|
0547102 to
f3eafd7
Compare
There are no regressions and the number of Gen 2 collections have dropped (as expected): Details
Some of the benchmarks (
|
carlossanlop
left a comment
There was a problem hiding this comment.
Looks good so far. Pending answering the dispose question and benchmark results cc @adamsitnik
f3eafd7 to
3225998
Compare
adamsitnik
left a comment
There was a problem hiding this comment.
LGTM, thank you @stephentoub !
carlossanlop
left a comment
There was a problem hiding this comment.
LGTM. The only comment is that I think having a Dispose method without implementing IDisposable seems a bit weird, but I don't think it is a merge blocker.
|
@stephentoub should we backport this PR to the preview4 branch? My main concern are the arrays allocated from the pinned heap and the Gen 2 collections that can cause. |
|
I don't think it meets the bar. |
This PR has two changes for FileStream...
The first is to remove the permanent pinning performed by BufferedFileStreamStrategy. Previously, on Windows when FileStream would allocate its buffer, it would also pin it as part of creating a PreallocatedOverlapped. Recent changes removed the PreallocatedOverlapped wrapping of the buffer, and replaced it with a buffer from the pinned-object heap. But POH allocations are considered to be gen2, which can then have a negative app-wide impact if lots of FileStreams are created and thrown away quickly. Instead, this PR just removes that pinning entirely. BufferedFileStreamStrategy's buffer, like any other buffer, will just be pinned for the duration of each individual read/write async operation, using Memory.Pin. I do not see any degradation in the benchmarks as a result. cc: @jkotas
The second change is to significantly simplify how the async completion is handled. There's currently a complicated set of interlocked operations that transition the promise through multiple stages, to account for the possibility of race conditions between various operations. We can instead get rid of all of that, and just use an event to handle the rare case where additional configuration of an async operation after that async operation was just initiated (e.g. registering for cancellation) doesn't race with concurrent completion of that same operation.
@adamsitnik, I don't see regressions from these changes, but the benchmarks also appear to be quite noisy, at least on my machine. If you could give them a go, that'd be helpful. Thanks.