@@ -183,44 +183,47 @@ public void Append(string key, string value)
183183 _accumulator = new AdaptiveCapacityDictionary < string , StringValues > ( StringComparer . OrdinalIgnoreCase ) ;
184184 }
185185
186- if ( _accumulator . TryGetValue ( key , out var values ) )
186+ if ( ! _accumulator . TryGetValue ( key , out var values ) )
187187 {
188- if ( values . Count == 0 )
189- {
190- // Marker entry for this key to indicate entry already in expanding list dictionary
191- _expandingAccumulator [ key ] . Add ( value ) ;
192- }
193- else if ( values . Count == 1 )
194- {
195- _accumulator [ key ] = StringValues . Concat ( values , value ) ;
196- }
197- else
198- {
199- // Add zero count entry and move to data to expanding list dictionary
200- _accumulator [ key ] = default ;
188+ // First value for this key
189+ _accumulator [ key ] = new StringValues ( value ) ;
190+ }
191+ else
192+ {
193+ AppendToExpandingAccumulator ( key , value , values ) ;
194+ }
201195
202- if ( _expandingAccumulator is null )
203- {
204- _expandingAccumulator = new AdaptiveCapacityDictionary < string , List < string > > ( capacity : 5 , StringComparer . OrdinalIgnoreCase ) ;
205- }
196+ ValueCount ++ ;
197+ }
206198
207- // Already 3 (2 existing + the new one) entries so use starting allocated as 6; then use List's expansion
208- // mechanism for more
209- var list = new List < string > ( capacity : 6 ) ;
199+ private void AppendToExpandingAccumulator ( string key , string value , StringValues values )
200+ {
201+ // When there are some values for the same key, so switch to expanding accumulator, and
202+ // add a zero count marker in the accumulator to indicate that switch.
210203
211- list . AddRange ( values ) ;
212- list . Add ( value ) ;
204+ if ( values . Count != 0 )
205+ {
206+ _accumulator [ key ] = default ;
213207
214- _expandingAccumulator [ key ] = list ;
208+ if ( _expandingAccumulator is null )
209+ {
210+ _expandingAccumulator = new AdaptiveCapacityDictionary < string , List < string > > ( capacity : 5 , StringComparer . OrdinalIgnoreCase ) ;
215211 }
212+
213+ // Already 3 (2 existing + the new one) entries so use starting allocated as 6; then use List's expansion
214+ // mechanism for more
215+ var list = new List < string > ( capacity : 6 ) ;
216+
217+ list . AddRange ( values ) ;
218+ list . Add ( value ) ;
219+
220+ _expandingAccumulator [ key ] = list ;
216221 }
217222 else
218223 {
219- // First value for this key
220- _accumulator [ key ] = new StringValues ( value ) ;
224+ // The marker indicates we are in the expanding accumulator, so just append to the list.
225+ _expandingAccumulator [ key ] . Add ( value ) ;
221226 }
222-
223- ValueCount ++ ;
224227 }
225228
226229 /// <summary>
0 commit comments