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 8
8
using System . IO ;
9
9
using System . Runtime . CompilerServices ;
10
10
using System . Runtime . InteropServices ;
11
+ using Microsoft . Toolkit . HighPerformance . Buffers ;
11
12
using Microsoft . Toolkit . HighPerformance . Streams ;
12
13
using Microsoft . Toolkit . HighPerformance . Streams . Sources ;
13
14
@@ -28,6 +29,15 @@ public static class IBufferWriterExtensions
28
29
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
29
30
public static Stream AsStream ( this IBufferWriter < byte > writer )
30
31
{
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
+
31
41
return new IBufferWriterStream < IBufferWriterOwner > ( new IBufferWriterOwner ( writer ) ) ;
32
42
}
33
43
You can’t perform that action at this time.
0 commit comments