Use Inheritance instead of Composition to Reduce allocation in Request Decompression Middleware
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
I've opened this issue to discuss the idea that I've mentioned in [here](https://github.com/dotnet/aspnetcore/pull/40279#discussion_r810507794) and [here](https://github.com/dotnet/aspnetcore/pull/40279#discussion_r816191725) now that Request Decompression Middleware is merged.
At the moment `IDecompressionProvider` is creating a decompression stream and the middleware creates a `SizeLimitedStream` which wraps the other one. We could reduce this two streams into one if the `IDecompressionProvider` returns a stream that also handle size limit check, and the middleware only create `SizeLimitedStream` if the returned stream from the `IDecompressionProvider` is not marked with `IDontNeedWrapper` interface.
```diff
+internal interface IDontNeedWrapper {}
+internal sealed class GZipRequestDecompressionBody : GZipStream, IDontNeedWrapper {}
+internal sealed class DeflateRequestDecompressionBody : DeflateStream , IDontNeedWrapper {}
+internal sealed class BrotliRequestDecompressionBody : BrotliStream, IDontNeedWrapper {}
```
The new types for this change are all private, so there's no new public types. By doing this we would reduce one allocation per requests when request body is compressed by `GZip`, `Deflate` or `Brotli`.
Contributor guide
Assessment
This issue has not been assessed yet.