11using System ;
2+ using System . Buffers ;
23using System . Collections . Generic ;
34using System . Text ;
45using QuickFix . Util ;
@@ -10,16 +11,11 @@ namespace QuickFix.Store;
1011/// </summary>
1112public class FileStore : IMessageStore
1213{
13- private class MsgDef
14+ private readonly struct MsgDef ( long index , int size )
1415 {
15- public long Index { get ; }
16- public int Size { get ; }
16+ public long Index { get ; } = index ;
1717
18- public MsgDef ( long index , int size )
19- {
20- Index = index ;
21- Size = size ;
22- }
18+ public int Size { get ; } = size ;
2319 }
2420
2521 private readonly string _seqNumsFileName ;
@@ -198,13 +194,19 @@ public void Get(SeqNumType startSeqNum, SeqNumType endSeqNum, List<string> messa
198194 {
199195 for ( SeqNumType i = startSeqNum ; i <= endSeqNum ; i ++ )
200196 {
201- if ( _offsets . ContainsKey ( i ) )
197+ if ( _offsets . TryGetValue ( i , out MsgDef msgDef ) )
202198 {
203- _msgFile . Seek ( _offsets [ i ] . Index , System . IO . SeekOrigin . Begin ) ;
204- byte [ ] msgBytes = new byte [ _offsets [ i ] . Size ] ;
205- _msgFile . Read ( msgBytes , 0 , msgBytes . Length ) ;
206-
207- messages . Add ( CharEncoding . SelectedEncoding . GetString ( msgBytes ) ) ;
199+ _msgFile . Seek ( msgDef . Index , System . IO . SeekOrigin . Begin ) ;
200+ byte [ ] msgBytes = ArrayPool < byte > . Shared . Rent ( msgDef . Size ) ;
201+ try
202+ {
203+ _msgFile . ReadExactly ( new Span < byte > ( msgBytes , 0 , msgDef . Size ) ) ;
204+ messages . Add ( CharEncoding . SelectedEncoding . GetString ( new ReadOnlySpan < byte > ( msgBytes , 0 , msgDef . Size ) ) ) ;
205+ }
206+ finally
207+ {
208+ ArrayPool < byte > . Shared . Return ( msgBytes ) ;
209+ }
208210 }
209211 }
210212
@@ -221,20 +223,19 @@ public bool Set(SeqNumType msgSeqNum, string msg)
221223 _msgFile . Seek ( 0 , System . IO . SeekOrigin . End ) ;
222224
223225 long offset = _msgFile . Position ;
224- byte [ ] msgBytes = CharEncoding . GetBytes ( msg ) ;
225- int size = msgBytes . Length ;
226+
227+ using ValueDisposable _ = CharEncoding . GetBytes ( msg . AsSpan ( ) , out ReadOnlySpan < byte > msgBytes ) ;
226228
227229 StringBuilder b = new StringBuilder ( ) ;
228- b . Append ( msgSeqNum ) . Append ( ',' ) . Append ( offset ) . Append ( ',' ) . Append ( size ) ;
230+ b . Append ( msgSeqNum ) . Append ( ',' ) . Append ( offset ) . Append ( ',' ) . Append ( msgBytes . Length ) ;
229231 _headerFile . WriteLine ( b . ToString ( ) ) ;
230232 _headerFile . Flush ( ) ;
231233
232- _offsets [ msgSeqNum ] = new MsgDef ( offset , size ) ;
234+ _offsets [ msgSeqNum ] = new MsgDef ( offset , msgBytes . Length ) ;
233235
234- _msgFile . Write ( msgBytes , 0 , size ) ;
236+ _msgFile . Write ( msgBytes ) ;
235237 _msgFile . Flush ( ) ;
236238
237-
238239 return true ;
239240 }
240241
@@ -299,7 +300,7 @@ public void Dispose()
299300 Dispose ( true ) ;
300301 GC . SuppressFinalize ( this ) ;
301302 }
302- private bool _disposed = false ;
303+ private bool _disposed ;
303304 protected virtual void Dispose ( bool disposing )
304305 {
305306 if ( _disposed ) return ;
0 commit comments