@@ -111,30 +111,29 @@ private void ResetState()
111111 _length = 0 ;
112112 }
113113
114- internal Memory < byte > GetMemory ( int minimumSize )
114+ internal Memory < byte > GetMemory ( int sizeHint )
115115 {
116116 if ( _writerCompletion . IsCompleted )
117117 {
118118 ThrowHelper . ThrowInvalidOperationException_NoWritingAllowed ( ) ;
119119 }
120120
121- if ( minimumSize < 0 )
121+ if ( sizeHint < 0 )
122122 {
123123 ThrowHelper . ThrowArgumentOutOfRangeException ( ExceptionArgument . minimumSize ) ;
124124 }
125125
126126 lock ( _sync )
127127 {
128- BufferSegment segment = _writingHead ?? AllocateWriteHeadUnsynchronized ( minimumSize ) ;
128+ BufferSegment segment = _writingHead ?? AllocateWriteHeadUnsynchronized ( sizeHint ) ;
129129
130130 int bytesLeftInBuffer = segment . WritableBytes ;
131131
132132 // If inadequate bytes left or if the segment is readonly
133- if ( bytesLeftInBuffer == 0 || bytesLeftInBuffer < minimumSize || segment . ReadOnly )
133+ if ( bytesLeftInBuffer == 0 || bytesLeftInBuffer < sizeHint || segment . ReadOnly )
134134 {
135135 BufferSegment nextSegment = CreateSegmentUnsynchronized ( ) ;
136-
137- nextSegment . SetMemory ( _pool . Rent ( Math . Max ( _minimumSegmentSize , minimumSize ) ) ) ;
136+ nextSegment . SetMemory ( _pool . Rent ( GetSegmentSize ( sizeHint ) ) ) ;
138137
139138 segment . SetNext ( nextSegment ) ;
140139
@@ -145,7 +144,7 @@ internal Memory<byte> GetMemory(int minimumSize)
145144 return _writingHead . AvailableMemory . Slice ( _writingHead . End , _writingHead . WritableBytes ) ;
146145 }
147146
148- private BufferSegment AllocateWriteHeadUnsynchronized ( int count )
147+ private BufferSegment AllocateWriteHeadUnsynchronized ( int sizeHint )
149148 {
150149 BufferSegment segment = null ;
151150
@@ -154,7 +153,7 @@ private BufferSegment AllocateWriteHeadUnsynchronized(int count)
154153 // Try to return the tail so the calling code can append to it
155154 int remaining = _commitHead . WritableBytes ;
156155
157- if ( count <= remaining && remaining > 0 )
156+ if ( sizeHint <= remaining && remaining > 0 )
158157 {
159158 // Free tail space of the right amount, use that
160159 segment = _commitHead ;
@@ -165,7 +164,7 @@ private BufferSegment AllocateWriteHeadUnsynchronized(int count)
165164 {
166165 // No free tail space, allocate a new segment
167166 segment = CreateSegmentUnsynchronized ( ) ;
168- segment . SetMemory ( _pool . Rent ( Math . Max ( _minimumSegmentSize , count ) ) ) ;
167+ segment . SetMemory ( _pool . Rent ( GetSegmentSize ( sizeHint ) ) ) ;
169168 }
170169
171170 if ( _commitHead == null )
@@ -186,6 +185,15 @@ private BufferSegment AllocateWriteHeadUnsynchronized(int count)
186185 return segment ;
187186 }
188187
188+ private int GetSegmentSize ( int sizeHint )
189+ {
190+ // First we need to handle case where hint is smaller than minimum segment size
191+ var adjustedToMinimumSize = Math . Max ( _minimumSegmentSize , sizeHint ) ;
192+ // After that adjust it to fit into pools max buffer size
193+ var adjustedToMaximumSize = Math . Min ( _pool . MaxBufferSize , adjustedToMinimumSize ) ;
194+ return adjustedToMaximumSize ;
195+ }
196+
189197 private BufferSegment CreateSegmentUnsynchronized ( )
190198 {
191199 if ( _pooledSegmentCount > 0 )
@@ -338,26 +346,26 @@ internal void AdvanceReader(SequencePosition consumed, SequencePosition examined
338346 lock ( _sync )
339347 {
340348 var examinedEverything = false ;
341- if ( examined . Segment == _commitHead )
349+ if ( examined . GetObject ( ) == _commitHead )
342350 {
343- examinedEverything = _commitHead != null ? examined . Index == _commitHeadIndex - _commitHead . Start : examined . Index == 0 ;
351+ examinedEverything = _commitHead != null ? examined . GetInteger ( ) == _commitHeadIndex - _commitHead . Start : examined . GetInteger ( ) == 0 ;
344352 }
345353
346- if ( consumed . Segment != null )
354+ if ( consumed . GetObject ( ) != null )
347355 {
348356 if ( _readHead == null )
349357 {
350358 ThrowHelper . ThrowInvalidOperationException_AdvanceToInvalidCursor ( ) ;
351359 return ;
352360 }
353361
354- var consumedSegment = ( BufferSegment ) consumed . Segment ;
362+ var consumedSegment = ( BufferSegment ) consumed . GetObject ( ) ;
355363
356364 returnStart = _readHead ;
357365 returnEnd = consumedSegment ;
358366
359367 // Check if we crossed _maximumSizeLow and complete backpressure
360- long consumedBytes = new ReadOnlySequence < byte > ( returnStart , _readHeadIndex , consumedSegment , consumed . Index ) . Length ;
368+ long consumedBytes = new ReadOnlySequence < byte > ( returnStart , _readHeadIndex , consumedSegment , consumed . GetInteger ( ) ) . Length ;
361369 long oldLength = _length ;
362370 _length -= consumedBytes ;
363371
@@ -370,7 +378,7 @@ internal void AdvanceReader(SequencePosition consumed, SequencePosition examined
370378 // Check if we consumed entire last segment
371379 // if we are going to return commit head we need to check that there is no writing operation that
372380 // might be using tailspace
373- if ( consumed . Index == returnEnd . Length && _writingHead != returnEnd )
381+ if ( consumed . GetInteger ( ) == returnEnd . Length && _writingHead != returnEnd )
374382 {
375383 BufferSegment nextBlock = returnEnd . NextSegment ;
376384 if ( _commitHead == returnEnd )
@@ -386,7 +394,7 @@ internal void AdvanceReader(SequencePosition consumed, SequencePosition examined
386394 else
387395 {
388396 _readHead = consumedSegment ;
389- _readHeadIndex = consumed . Index ;
397+ _readHeadIndex = consumed . GetInteger ( ) ;
390398 }
391399 }
392400
0 commit comments