Skip to content

Commit 7c84967

Browse files
Use frozen collections
1 parent c956136 commit 7c84967

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

src/AspNetCore/WebApi/src/Asp.Versioning.Http/Http/HttpRequestExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@ public static class HttpRequestExtensions
1616
/// <summary>
1717
/// Attempts to get the API version from current request path using the provided patterns.
1818
/// </summary>
19+
/// <typeparam name="TList">The type of <see cref="IReadOnlyList{T}">read-only list</see>.</typeparam>
1920
/// <param name="request">The current <see cref="HttpRequest">HTTP request</see>.</param>
2021
/// <param name="routePatterns">The <see cref="IReadOnlyList{T}">read-only list</see> of
2122
/// <see cref="RoutePattern">patterns</see> to evaluate.</param>
2223
/// <param name="constraintName">The name of the API version route constraint.</param>
2324
/// <param name="apiVersion">The raw API version, if retrieved.</param>
2425
/// <returns>True if the raw API version was retrieved; otherwise, false.</returns>
2526
[EditorBrowsable( EditorBrowsableState.Never )]
26-
public static bool TryGetApiVersionFromPath(
27+
public static bool TryGetApiVersionFromPath<TList>(
2728
this HttpRequest request,
28-
IReadOnlyList<RoutePattern> routePatterns,
29+
TList routePatterns,
2930
string constraintName,
3031
[NotNullWhen( true )] out string? apiVersion )
32+
where TList : IReadOnlyList<RoutePattern>
3133
{
3234
ArgumentNullException.ThrowIfNull( routePatterns );
3335

src/AspNetCore/WebApi/src/Asp.Versioning.Http/Routing/ApiVersionMatcherPolicy.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace Asp.Versioning.Routing;
1010
using Microsoft.Extensions.Logging;
1111
using Microsoft.Extensions.Options;
1212
using System.Buffers;
13+
using System.Collections.Frozen;
1314
using System.Runtime.CompilerServices;
1415
using System.Text.RegularExpressions;
1516
using static Asp.Versioning.ApiVersionMapping;
@@ -157,7 +158,7 @@ public PolicyJumpTable BuildJumpTable( int exitDestination, IReadOnlyList<Policy
157158

158159
return new ApiVersionPolicyJumpTable(
159160
rejection,
160-
destinations,
161+
destinations.ToFrozenDictionary( destinations.Comparer ),
161162
NewPolicyFeature( supported, deprecated ),
162163
routePatterns ?? [],
163164
apiVersionParser,

src/AspNetCore/WebApi/src/Asp.Versioning.Http/Routing/ApiVersionPolicyJumpTable.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace Asp.Versioning.Routing;
66
using Microsoft.AspNetCore.Routing.Matching;
77
using Microsoft.AspNetCore.Routing.Patterns;
88
using Microsoft.Net.Http.Headers;
9+
using System.Collections.Frozen;
910
using System.Runtime.CompilerServices;
1011

1112
internal sealed class ApiVersionPolicyJumpTable : PolicyJumpTable
@@ -14,17 +15,17 @@ internal sealed class ApiVersionPolicyJumpTable : PolicyJumpTable
1415
private readonly bool versionsByUrlOnly;
1516
private readonly bool versionsByMediaTypeOnly;
1617
private readonly RouteDestination rejection;
17-
private readonly IReadOnlyDictionary<ApiVersion, int> destinations;
18+
private readonly FrozenDictionary<ApiVersion, int> destinations;
1819
private readonly ApiVersionPolicyFeature? policyFeature;
19-
private readonly IReadOnlyList<RoutePattern> routePatterns;
20+
private readonly RoutePattern[] routePatterns;
2021
private readonly IApiVersionParser parser;
2122
private readonly ApiVersioningOptions options;
2223

2324
internal ApiVersionPolicyJumpTable(
2425
RouteDestination rejection,
25-
IReadOnlyDictionary<ApiVersion, int> destinations,
26+
FrozenDictionary<ApiVersion, int> destinations,
2627
ApiVersionPolicyFeature? policyFeature,
27-
IReadOnlyList<RoutePattern> routePatterns,
28+
RoutePattern[] routePatterns,
2829
IApiVersionParser parser,
2930
IApiVersionParameterSource source,
3031
ApiVersioningOptions options )
@@ -35,7 +36,7 @@ internal ApiVersionPolicyJumpTable(
3536
this.routePatterns = routePatterns;
3637
this.parser = parser;
3738
this.options = options;
38-
versionsByUrl = routePatterns.Count > 0;
39+
versionsByUrl = routePatterns.Length > 0;
3940
versionsByUrlOnly = source.VersionsByUrl( allowMultipleLocations: false );
4041
versionsByMediaTypeOnly = source.VersionsByMediaType( allowMultipleLocations: false );
4142
}

src/Common/src/Common/MediaTypeApiVersionReaderBuilder.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ namespace Asp.Versioning;
1818

1919
#if !NETFRAMEWORK
2020
using System.Buffers;
21+
using System.Collections.Frozen;
2122
#endif
2223
using System.Runtime.CompilerServices;
2324
using System.Text.RegularExpressions;
2425
#if NETFRAMEWORK
25-
using Str = System.String;
26+
using FrozenSet = System.Collections.Generic.HashSet<System.String>;
27+
using Str = string;
2628
using StrComparer = System.StringComparer;
2729
#else
30+
using FrozenSet = System.Collections.Frozen.FrozenSet<Microsoft.Extensions.Primitives.StringSegment>;
2831
using Str = Microsoft.Extensions.Primitives.StringSegment;
2932
using StrComparer = Microsoft.Extensions.Primitives.StringSegmentComparer;
3033
#endif
@@ -142,9 +145,12 @@ public virtual IApiVersionReader Build() =>
142145
#if NET45
143146
included ?? [],
144147
excluded ?? [],
145-
#else
148+
#elif NETFRAMEWORK
146149
included ?? new( capacity: 0 ),
147150
excluded ?? new( capacity: 0 ),
151+
#else
152+
included?.ToFrozenSet( included.Comparer ) ?? FrozenSet<Str>.Empty,
153+
excluded?.ToFrozenSet( excluded.Comparer ) ?? FrozenSet<Str>.Empty,
148154
#endif
149155
select ?? DefaultSelector,
150156
readers?.ToArray() ?? [] );
@@ -285,15 +291,15 @@ private static string[] ReadMediaTypeParameter(
285291
private sealed class BuiltMediaTypeApiVersionReader : IApiVersionReader
286292
{
287293
private readonly string[] parameters;
288-
private readonly HashSet<Str> included;
289-
private readonly HashSet<Str> excluded;
294+
private readonly FrozenSet included;
295+
private readonly FrozenSet excluded;
290296
private readonly SelectorCallback selector;
291297
private readonly ReaderCallback[] readers;
292298

293299
internal BuiltMediaTypeApiVersionReader(
294300
string[] parameters,
295-
HashSet<Str> included,
296-
HashSet<Str> excluded,
301+
FrozenSet included,
302+
FrozenSet excluded,
297303
SelectorCallback selector,
298304
ReaderCallback[] readers )
299305
{

0 commit comments

Comments
 (0)