1- using System ;
1+ using Pipelines . Sockets . Unofficial ;
2+ using Pipelines . Sockets . Unofficial . Arenas ;
3+ using System ;
24using System . Buffers ;
3- using System . Buffers . Text ;
45using System . Collections . Generic ;
56using System . Diagnostics ;
67using System . IO ;
1516using System . Text ;
1617using System . Threading ;
1718using System . Threading . Tasks ;
18- using Pipelines . Sockets . Unofficial ;
19- using Pipelines . Sockets . Unofficial . Arenas ;
2019
2120namespace StackExchange . Redis
2221{
@@ -449,7 +448,7 @@ void add(string lk, string sk, string? v)
449448 add ( "Outstanding-Responses" , "outstanding" , GetSentAwaitingResponseCount ( ) . ToString ( ) ) ;
450449 add ( "Last-Read" , "last-read" , ( unchecked ( now - lastRead ) / 1000 ) + "s ago" ) ;
451450 add ( "Last-Write" , "last-write" , ( unchecked ( now - lastWrite ) / 1000 ) + "s ago" ) ;
452- if ( unansweredWriteTime != 0 ) add ( "Unanswered-Write" , "unanswered-write" , ( unchecked ( now - unansweredWriteTime ) / 1000 ) + "s ago" ) ;
451+ if ( unansweredWriteTime != 0 ) add ( "Unanswered-Write" , "unanswered-write" , ( unchecked ( now - unansweredWriteTime ) / 1000 ) + "s ago" ) ;
453452 add ( "Keep-Alive" , "keep-alive" , bridge . ServerEndPoint ? . WriteEverySeconds + "s" ) ;
454453 add ( "Previous-Physical-State" , "state" , oldState . ToString ( ) ) ;
455454 add ( "Manager" , "mgr" , bridge . Multiplexer . SocketManager ? . GetState ( ) ) ;
@@ -777,8 +776,7 @@ internal static void WriteBulkString(in RedisValue value, PipeWriter output)
777776
778777 internal void WriteHeader ( RedisCommand command , int arguments , CommandBytes commandBytes = default )
779778 {
780- var bridge = BridgeCouldBeNull ;
781- if ( bridge == null ) throw new ObjectDisposedException ( ToString ( ) ) ;
779+ var bridge = BridgeCouldBeNull ?? throw new ObjectDisposedException ( ToString ( ) ) ;
782780
783781 if ( command == RedisCommand . UNKNOWN )
784782 {
@@ -801,7 +799,7 @@ internal void WriteHeader(RedisCommand command, int arguments, CommandBytes comm
801799 // *{argCount}\r\n = 3 + MaxInt32TextLen
802800 // ${cmd-len}\r\n = 3 + MaxInt32TextLen
803801 // {cmd}\r\n = 2 + commandBytes.Length
804- var span = _ioPipe ! . Output . GetSpan ( commandBytes . Length + 8 + MaxInt32TextLen + MaxInt32TextLen ) ;
802+ var span = _ioPipe ! . Output . GetSpan ( commandBytes . Length + 8 + Format . MaxInt32TextLen + Format . MaxInt32TextLen ) ;
805803 span [ 0 ] = ( byte ) '*' ;
806804
807805 int offset = WriteRaw ( span , arguments + 1 , offset : 1 ) ;
@@ -817,16 +815,12 @@ internal void WriteHeader(RedisCommand command, int arguments, CommandBytes comm
817815 internal static void WriteMultiBulkHeader ( PipeWriter output , long count )
818816 {
819817 // *{count}\r\n = 3 + MaxInt32TextLen
820- var span = output . GetSpan ( 3 + MaxInt32TextLen ) ;
818+ var span = output . GetSpan ( 3 + Format . MaxInt32TextLen ) ;
821819 span [ 0 ] = ( byte ) '*' ;
822820 int offset = WriteRaw ( span , count , offset : 1 ) ;
823821 output . Advance ( offset ) ;
824822 }
825823
826- internal const int
827- MaxInt32TextLen = 11 , // -2,147,483,648 (not including the commas)
828- MaxInt64TextLen = 20 ; // -9,223,372,036,854,775,808 (not including the commas)
829-
830824 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
831825 internal static int WriteCrlf ( Span < byte > span , int offset )
832826 {
@@ -906,25 +900,16 @@ internal static int WriteRaw(Span<byte> span, long value, bool withLengthPrefix
906900 {
907901 // we're going to write it, but *to the wrong place*
908902 var availableChunk = span . Slice ( offset ) ;
909- if ( ! Utf8Formatter . TryFormat ( value , availableChunk , out int formattedLength ) )
910- {
911- throw new InvalidOperationException ( "TryFormat failed" ) ;
912- }
903+ var formattedLength = Format . FormatInt64 ( value , availableChunk ) ;
913904 if ( withLengthPrefix )
914905 {
915906 // now we know how large the prefix is: write the prefix, then write the value
916- if ( ! Utf8Formatter . TryFormat ( formattedLength , availableChunk , out int prefixLength ) )
917- {
918- throw new InvalidOperationException ( "TryFormat failed" ) ;
919- }
907+ var prefixLength = Format . FormatInt32 ( formattedLength , availableChunk ) ;
920908 offset += prefixLength ;
921909 offset = WriteCrlf ( span , offset ) ;
922910
923911 availableChunk = span . Slice ( offset ) ;
924- if ( ! Utf8Formatter . TryFormat ( value , availableChunk , out int finalLength ) )
925- {
926- throw new InvalidOperationException ( "TryFormat failed" ) ;
927- }
912+ var finalLength = Format . FormatInt64 ( value , availableChunk ) ;
928913 offset += finalLength ;
929914 Debug . Assert ( finalLength == formattedLength ) ;
930915 }
@@ -1035,15 +1020,15 @@ private static void WriteUnifiedSpan(PipeWriter writer, ReadOnlySpan<byte> value
10351020 }
10361021 else if ( value . Length <= MaxQuickSpanSize )
10371022 {
1038- var span = writer . GetSpan ( 5 + MaxInt32TextLen + value . Length ) ;
1023+ var span = writer . GetSpan ( 5 + Format . MaxInt32TextLen + value . Length ) ;
10391024 span [ 0 ] = ( byte ) '$' ;
10401025 int bytes = AppendToSpan ( span , value , 1 ) ;
10411026 writer . Advance ( bytes ) ;
10421027 }
10431028 else
10441029 {
10451030 // too big to guarantee can do in a single span
1046- var span = writer . GetSpan ( 3 + MaxInt32TextLen ) ;
1031+ var span = writer . GetSpan ( 3 + Format . MaxInt32TextLen ) ;
10471032 span [ 0 ] = ( byte ) '$' ;
10481033 int bytes = WriteRaw ( span , value . Length , offset : 1 ) ;
10491034 writer . Advance ( bytes ) ;
@@ -1136,7 +1121,7 @@ internal static void WriteUnifiedPrefixedString(PipeWriter writer, byte[]? prefi
11361121 }
11371122 else
11381123 {
1139- var span = writer . GetSpan ( 3 + MaxInt32TextLen ) ;
1124+ var span = writer . GetSpan ( 3 + Format . MaxInt32TextLen ) ;
11401125 span [ 0 ] = ( byte ) '$' ;
11411126 int bytes = WriteRaw ( span , totalLength , offset : 1 ) ;
11421127 writer . Advance ( bytes ) ;
@@ -1228,7 +1213,7 @@ private static void WriteUnifiedPrefixedBlob(PipeWriter writer, byte[]? prefix,
12281213 }
12291214 else
12301215 {
1231- var span = writer . GetSpan ( 3 + MaxInt32TextLen ) ; // note even with 2 max-len, we're still in same text range
1216+ var span = writer . GetSpan ( 3 + Format . MaxInt32TextLen ) ; // note even with 2 max-len, we're still in same text range
12321217 span [ 0 ] = ( byte ) '$' ;
12331218 int bytes = WriteRaw ( span , prefix . LongLength + value . LongLength , offset : 1 ) ;
12341219 writer . Advance ( bytes ) ;
@@ -1249,7 +1234,7 @@ private static void WriteUnifiedInt64(PipeWriter writer, long value)
12491234
12501235 // ${asc-len}\r\n = 3 + MaxInt32TextLen
12511236 // {asc}\r\n = MaxInt64TextLen + 2
1252- var span = writer . GetSpan ( 5 + MaxInt32TextLen + MaxInt64TextLen ) ;
1237+ var span = writer . GetSpan ( 5 + Format . MaxInt32TextLen + Format . MaxInt64TextLen ) ;
12531238
12541239 span [ 0 ] = ( byte ) '$' ;
12551240 var bytes = WriteRaw ( span , value , withLengthPrefix : true , offset : 1 ) ;
@@ -1263,11 +1248,10 @@ private static void WriteUnifiedUInt64(PipeWriter writer, ulong value)
12631248
12641249 // ${asc-len}\r\n = 3 + MaxInt32TextLen
12651250 // {asc}\r\n = MaxInt64TextLen + 2
1266- var span = writer . GetSpan ( 5 + MaxInt32TextLen + MaxInt64TextLen ) ;
1251+ var span = writer . GetSpan ( 5 + Format . MaxInt32TextLen + Format . MaxInt64TextLen ) ;
12671252
1268- Span < byte > valueSpan = stackalloc byte [ MaxInt64TextLen ] ;
1269- if ( ! Utf8Formatter . TryFormat ( value , valueSpan , out var len ) )
1270- throw new InvalidOperationException ( "TryFormat failed" ) ;
1253+ Span < byte > valueSpan = stackalloc byte [ Format . MaxInt64TextLen ] ;
1254+ var len = Format . FormatUInt64 ( value , valueSpan ) ;
12711255 span [ 0 ] = ( byte ) '$' ;
12721256 int offset = WriteRaw ( span , len , withLengthPrefix : false , offset : 1 ) ;
12731257 valueSpan . Slice ( 0 , len ) . CopyTo ( span . Slice ( offset ) ) ;
@@ -1280,7 +1264,7 @@ internal static void WriteInteger(PipeWriter writer, long value)
12801264 //note: client should never write integer; only server does this
12811265
12821266 // :{asc}\r\n = MaxInt64TextLen + 3
1283- var span = writer . GetSpan ( 3 + MaxInt64TextLen ) ;
1267+ var span = writer . GetSpan ( 3 + Format . MaxInt64TextLen ) ;
12841268
12851269 span [ 0 ] = ( byte ) ':' ;
12861270 var bytes = WriteRaw ( span , value , withLengthPrefix : false , offset : 1 ) ;
0 commit comments