mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-22 19:10:41 -10:00
When using async operations on FileStream, each operation ends up allocating several objects. By using a PreallocatedOverlapped, we can avoid the majority of those allocations, as well as avoid having to pin/unpin a buffer on each request. This commit adds an optimization to FileStream that kicks in for the common case when: - The buffer provided to ReadAsync/WriteAsync is smaller than the size of the FileStream's buffer, such that the FileStream's buffer is used. And when... - Operations are not scheduled to run concurrently. When those conditions are met, we use a PreallocatedOverlapped for the operation to avoid a ThreadPoolBoundHandleOverlapped, IOCompletionCallback, and _IOCompletionCallback getting allocated for each operation. This reduces the bytes allocated per operation by ~50% and the number of objects allocated by ~60%. It also saves having to pin/unpin the buffer on each operation. When those conditions aren't met, we simply use the same code that's used today. Commit migrated from https://github.com/dotnet/corefx/commit/c01617841be8b5087ed37f64bb2395afc722e549