File tree Expand file tree Collapse file tree 1 file changed +10
-0
lines changed
Microsoft.Toolkit.HighPerformance/Extensions Expand file tree Collapse file tree 1 file changed +10
-0
lines changed Original file line number Diff line number Diff line change 88using System . IO ;
99using System . Runtime . CompilerServices ;
1010using System . Runtime . InteropServices ;
11+ using Microsoft . Toolkit . HighPerformance . Buffers ;
1112using Microsoft . Toolkit . HighPerformance . Streams ;
1213using Microsoft . Toolkit . HighPerformance . Streams . Sources ;
1314
@@ -28,6 +29,15 @@ public static class IBufferWriterExtensions
2829 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
2930 public static Stream AsStream ( this IBufferWriter < byte > writer )
3031 {
32+ if ( writer . GetType ( ) == typeof ( ArrayPoolBufferWriter < byte > ) )
33+ {
34+ // If the input writer is of type ArrayPoolBufferWriter<byte>, we can use the type
35+ // specific buffer writer owner to let the JIT elide callvirts when accessing it.
36+ var internalWriter = Unsafe . As < ArrayPoolBufferWriter < byte > > ( writer ) ;
37+
38+ return new IBufferWriterStream < ArrayBufferWriterOwner > ( new ArrayBufferWriterOwner ( internalWriter ) ) ;
39+ }
40+
3141 return new IBufferWriterStream < IBufferWriterOwner > ( new IBufferWriterOwner ( writer ) ) ;
3242 }
3343
You can’t perform that action at this time.
0 commit comments