Skip to content

Commit 2a2d15a

Browse files
committed
Changing to use readonlyspan
1 parent 9a55381 commit 2a2d15a

File tree

8 files changed

+20
-24
lines changed

8 files changed

+20
-24
lines changed

src/Antiforgery/test/DefaultAntiforgeryTokenStoreTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ public void Append(string key, string value)
491491
throw new NotImplementedException();
492492
}
493493

494-
public void Append(IEnumerable<KeyValuePair<string, string>> keyValuePairs, CookieOptions options)
494+
public void Append(ReadOnlySpan<KeyValuePair<string, string>> keyValuePairs, CookieOptions options)
495495
{
496496
throw new NotImplementedException();
497497
}

src/Http/Http.Features/src/IResponseCookies.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Collections.Generic;
56

67
namespace Microsoft.AspNetCore.Http
@@ -30,7 +31,7 @@ public interface IResponseCookies
3031
/// </summary>
3132
/// <param name="keyValuePairs">Key value pair collections whose elements will be added as cookies.</param>
3233
/// <param name="options"><see cref="CookieOptions"/> included in new cookie settings.</param>
33-
void Append(IEnumerable<KeyValuePair<string, string>> keyValuePairs, CookieOptions options);
34+
void Append(ReadOnlySpan<KeyValuePair<string, string>> keyValuePairs, CookieOptions options);
3435

3536
/// <summary>
3637
/// Sets an expired cookie.

src/Http/Http.Features/src/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ Microsoft.AspNetCore.Http.Features.IFeatureCollection.Set<TFeature>(TFeature? in
1515
Microsoft.AspNetCore.Http.Features.IServerVariablesFeature.this[string! variableName].get -> string?
1616
Microsoft.AspNetCore.Http.ISession.TryGetValue(string! key, out byte[]? value) -> bool
1717
Microsoft.AspNetCore.Http.Features.FeatureCollection.FeatureCollection(int initialCapacity) -> void
18-
Microsoft.AspNetCore.Http.IResponseCookies.Append(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, string!>>! keyValuePairs, Microsoft.AspNetCore.Http.CookieOptions! options) -> void
18+
Microsoft.AspNetCore.Http.IResponseCookies.Append(System.ReadOnlySpan<System.Collections.Generic.KeyValuePair<string!, string!>> keyValuePairs, Microsoft.AspNetCore.Http.CookieOptions! options) -> void

src/Http/Http/src/Internal/ResponseCookies.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Linq;
76
using Microsoft.AspNetCore.Http.Features;
87
using Microsoft.Extensions.DependencyInjection;
98
using Microsoft.Extensions.Logging;
@@ -90,7 +89,7 @@ public void Append(string key, string value, CookieOptions options)
9089
}
9190

9291
/// <inheritdoc />
93-
public void Append(IEnumerable<KeyValuePair<string, string>> keyValuePairs, CookieOptions options)
92+
public void Append(ReadOnlySpan<KeyValuePair<string, string>> keyValuePairs, CookieOptions options)
9493
{
9594
if (options == null)
9695
{
@@ -127,7 +126,7 @@ public void Append(IEnumerable<KeyValuePair<string, string>> keyValuePairs, Cook
127126
};
128127

129128
var cookierHeaderValue = setCookieHeaderValue.ToString()[1..];
130-
var cookies = new string[keyValuePairs.Count()];
129+
var cookies = new string[keyValuePairs.Length];
131130
var position = 0;
132131

133132
foreach (var keyValuePair in keyValuePairs)

src/Mvc/Mvc.ViewFeatures/test/CookieTempDataProviderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ IEnumerator IEnumerable.GetEnumerator()
417417
return GetEnumerator();
418418
}
419419

420-
public void Append(IEnumerable<KeyValuePair<string, string>> keyValuePairs, CookieOptions options)
420+
public void Append(ReadOnlySpan<KeyValuePair<string, string>> keyValuePairs, CookieOptions options)
421421
{
422422
foreach (var keyValuePair in keyValuePairs)
423423
{

src/Security/CookiePolicy/src/ResponseCookiesWrapper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,14 @@ public void Append(string key, string value, CookieOptions options)
153153
}
154154
}
155155

156-
public void Append(IEnumerable<KeyValuePair<string, string>> keyValuePairs, CookieOptions options)
156+
public void Append(ReadOnlySpan<KeyValuePair<string, string>> keyValuePairs, CookieOptions options)
157157
{
158158
if (options == null)
159159
{
160160
throw new ArgumentNullException(nameof(options));
161161
}
162162

163-
var nonSuppressedValues = new Dictionary<string, string>();
163+
var nonSuppressedValues = new List<KeyValuePair<string, string>>();
164164

165165
foreach (var keyValuePair in keyValuePairs)
166166
{
@@ -169,15 +169,15 @@ public void Append(IEnumerable<KeyValuePair<string, string>> keyValuePairs, Cook
169169

170170
if (ApplyAppendPolicy(ref key, ref value, options))
171171
{
172-
nonSuppressedValues.Add(key, value);
172+
nonSuppressedValues.Add(KeyValuePair.Create(key, value));
173173
}
174174
else
175175
{
176176
_logger.CookieSuppressed(keyValuePair.Key);
177177
}
178178
}
179179

180-
Cookies.Append(nonSuppressedValues, options);
180+
Cookies.Append(nonSuppressedValues.ToArray(), options);
181181
}
182182

183183
private bool ApplyAppendPolicy(ref string key, ref string value, CookieOptions options)

src/Security/CookiePolicy/test/CookiePolicyTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ public void Append(string key, string value, CookieOptions options)
486486
throw new NotImplementedException();
487487
}
488488

489-
public void Append(IEnumerable<KeyValuePair<string, string>> keyValuePairs, CookieOptions options)
489+
public void Append(ReadOnlySpan<KeyValuePair<string, string>> keyValuePairs, CookieOptions options)
490490
{
491491
throw new NotImplementedException();
492492
}

src/Shared/ChunkingCookieManager/ChunkingCookieManager.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -208,19 +208,17 @@ public void AppendResponseCookie(HttpContext context, string key, string? value,
208208
var dataSizePerCookie = ChunkSize.Value - templateLength - 3; // Budget 3 chars for the chunkid.
209209
var cookieChunkCount = (int)Math.Ceiling(value.Length * 1.0 / dataSizePerCookie);
210210

211-
List<KeyValuePair<string, string>> keyValuePairs = new(cookieChunkCount)
212-
{
213-
KeyValuePair.Create(key, ChunkCountPrefix + cookieChunkCount.ToString(CultureInfo.InvariantCulture))
214-
};
215-
211+
var keyValuePairs = new KeyValuePair<string, string>[cookieChunkCount + 1];
212+
keyValuePairs[0] = KeyValuePair.Create(key, ChunkCountPrefix + cookieChunkCount.ToString(CultureInfo.InvariantCulture));
213+
216214
var offset = 0;
217215
for (var chunkId = 1; chunkId <= cookieChunkCount; chunkId++)
218216
{
219217
var remainingLength = value.Length - offset;
220218
var length = Math.Min(dataSizePerCookie, remainingLength);
221219
var segment = value.Substring(offset, length);
222220
offset += length;
223-
keyValuePairs.Add(KeyValuePair.Create(string.Concat(key, ChunkKeySuffix, chunkId.ToString(CultureInfo.InvariantCulture)), segment));
221+
keyValuePairs[chunkId] = KeyValuePair.Create(string.Concat(key, ChunkKeySuffix, chunkId.ToString(CultureInfo.InvariantCulture)), segment);
224222
}
225223

226224
responseCookies.Append(keyValuePairs, options);
@@ -306,14 +304,12 @@ public void DeleteCookie(HttpContext context, string key, CookieOptions options)
306304

307305
var responseCookies = context.Response.Cookies;
308306

309-
List<KeyValuePair<string, string>> keyValuePairs = new(chunks)
310-
{
311-
KeyValuePair.Create(key, string.Empty)
312-
};
313-
307+
var keyValuePairs = new KeyValuePair<string, string>[chunks + 1];
308+
keyValuePairs[0] = KeyValuePair.Create(key, string.Empty);
309+
314310
for (var i = 1; i <= chunks; i++)
315311
{
316-
keyValuePairs.Add(KeyValuePair.Create(string.Concat(key, "C", i.ToString(CultureInfo.InvariantCulture)), string.Empty));
312+
keyValuePairs[i] = KeyValuePair.Create(string.Concat(key, "C", i.ToString(CultureInfo.InvariantCulture)), string.Empty);
317313
}
318314

319315
responseCookies.Append(keyValuePairs, new CookieOptions()

0 commit comments

Comments
 (0)