This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 126
/
CandidateSet.cs
289 lines (249 loc) · 11 KB
/
CandidateSet.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.CompilerServices;
using Microsoft.AspNetCore.Http;
namespace Microsoft.AspNetCore.Routing.Matching
{
/// <summary>
/// Represents a set of <see cref="Endpoint"/> candidates that have been matched
/// by the routing system. Used by implementations of <see cref="EndpointSelector"/>
/// and <see cref="IEndpointSelectorPolicy"/>.
/// </summary>
public sealed class CandidateSet
{
private const int BitVectorSize = 32;
// Cannot be readonly because we need to modify it in place.
private BitVector32 _validity;
private readonly BitArray _largeCapactityValidity;
// We inline storage for 4 candidates here to avoid allocations in common
// cases. There's no real reason why 4 is important, it just seemed like
// a plausible number.
private CandidateState _state0;
private CandidateState _state1;
private CandidateState _state2;
private CandidateState _state3;
private CandidateState[] _additionalCandidates;
/// <summary>
/// <para>
/// Initializes a new instances of the <see cref="CandidateSet"/> class with the provided <paramref name="endpoints"/>,
/// <paramref name="values"/>, and <paramref name="scores"/>.
/// </para>
/// <para>
/// The constructor is provided to enable unit tests of implementations of <see cref="EndpointSelector"/>
/// and <see cref="IEndpointSelectorPolicy"/>.
/// </para>
/// </summary>
/// <param name="endpoints">The list of endpoints, sorted in descending priority order.</param>
/// <param name="values">The list of <see cref="RouteValueDictionary"/> instances.</param>
/// <param name="scores">The list of endpoint scores. <see cref="CandidateState.Score"/>.</param>
public CandidateSet(Endpoint[] endpoints, RouteValueDictionary[] values, int[] scores)
{
if (endpoints == null)
{
throw new ArgumentNullException(nameof(endpoints));
}
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
if (scores == null)
{
throw new ArgumentNullException(nameof(scores));
}
if (endpoints.Length != values.Length || endpoints.Length != scores.Length)
{
throw new ArgumentException($"The provided {nameof(endpoints)}, {nameof(values)}, and {nameof(scores)} must have the same length.");
}
Count = endpoints.Length;
switch (endpoints.Length)
{
case 0:
return;
case 1:
_state0 = new CandidateState(endpoints[0], values[0], scores[0]);
break;
case 2:
_state0 = new CandidateState(endpoints[0], values[0], scores[0]);
_state1 = new CandidateState(endpoints[1], values[1], scores[1]);
break;
case 3:
_state0 = new CandidateState(endpoints[0], values[0], scores[0]);
_state1 = new CandidateState(endpoints[1], values[1], scores[1]);
_state2 = new CandidateState(endpoints[2], values[2], scores[2]);
break;
case 4:
_state0 = new CandidateState(endpoints[0], values[0], scores[0]);
_state1 = new CandidateState(endpoints[1], values[1], scores[1]);
_state2 = new CandidateState(endpoints[2], values[2], scores[2]);
_state3 = new CandidateState(endpoints[3], values[3], scores[3]);
break;
default:
_state0 = new CandidateState(endpoints[0], values[0], scores[0]);
_state1 = new CandidateState(endpoints[1], values[1], scores[1]);
_state2 = new CandidateState(endpoints[2], values[2], scores[2]);
_state3 = new CandidateState(endpoints[3], values[3], scores[3]);
_additionalCandidates = new CandidateState[endpoints.Length - 4];
for (var i = 4; i < endpoints.Length; i++)
{
_additionalCandidates[i - 4] = new CandidateState(endpoints[i], values[i], scores[i]);
}
break;
}
// Initialize validity to valid by default.
if (Count < BitVectorSize)
{
// Sets the bit for each candidate that exists (bits > Count will be 0).
_validity = new BitVector32(unchecked((int)~(0xFFFFFFFFu << Count)));
}
else
{
_largeCapactityValidity = new BitArray(Count, defaultValue: true);
}
}
internal CandidateSet(Candidate[] candidates)
{
Count = candidates.Length;
switch (candidates.Length)
{
case 0:
return;
case 1:
_state0 = new CandidateState(candidates[0].Endpoint, candidates[0].Score);
break;
case 2:
_state0 = new CandidateState(candidates[0].Endpoint, candidates[0].Score);
_state1 = new CandidateState(candidates[1].Endpoint, candidates[1].Score);
break;
case 3:
_state0 = new CandidateState(candidates[0].Endpoint, candidates[0].Score);
_state1 = new CandidateState(candidates[1].Endpoint, candidates[1].Score);
_state2 = new CandidateState(candidates[2].Endpoint, candidates[2].Score);
break;
case 4:
_state0 = new CandidateState(candidates[0].Endpoint, candidates[0].Score);
_state1 = new CandidateState(candidates[1].Endpoint, candidates[1].Score);
_state2 = new CandidateState(candidates[2].Endpoint, candidates[2].Score);
_state3 = new CandidateState(candidates[3].Endpoint, candidates[3].Score);
break;
default:
_state0 = new CandidateState(candidates[0].Endpoint, candidates[0].Score);
_state1 = new CandidateState(candidates[1].Endpoint, candidates[1].Score);
_state2 = new CandidateState(candidates[2].Endpoint, candidates[2].Score);
_state3 = new CandidateState(candidates[3].Endpoint, candidates[3].Score);
_additionalCandidates = new CandidateState[candidates.Length - 4];
for (var i = 4; i < candidates.Length; i++)
{
_additionalCandidates[i - 4] = new CandidateState(candidates[i].Endpoint, candidates[i].Score);
}
break;
}
// Initialize validity to valid by default.
if (Count < BitVectorSize)
{
// Sets the bit for each candidate that exists (bits > Count will be 0).
_validity = new BitVector32(unchecked((int)~(0xFFFFFFFFu << Count)));
}
else
{
_largeCapactityValidity = new BitArray(Count, defaultValue: true);
}
}
/// <summary>
/// Gets the count of candidates in the set.
/// </summary>
public int Count { get; }
/// <summary>
/// Gets the <see cref="CandidateState"/> associated with the candidate <see cref="Endpoint"/>
/// at <paramref name="index"/>.
/// </summary>
/// <param name="index">The candidate index.</param>
/// <returns>
/// A reference to the <see cref="CandidateState"/>. The result is returned by reference.
/// </returns>
public ref CandidateState this[int index]
{
// Note that this is a ref-return because of performance.
// We don't want to copy these fat structs if it can be avoided.
// PERF: Force inlining
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
// Friendliness for inlining
if ((uint)index >= Count)
{
ThrowIndexArgumentOutOfRangeException();
}
switch (index)
{
case 0:
return ref _state0;
case 1:
return ref _state1;
case 2:
return ref _state2;
case 3:
return ref _state3;
default:
return ref _additionalCandidates[index - 4];
}
}
}
/// <summary>
/// Gets a value which indicates where the <see cref="Http.Endpoint"/> is considered
/// a valid candiate for the current request.
/// </summary>
/// <param name="index">The candidate index.</param>
/// <returns>
/// <c>true</c> if the candidate at position <paramref name="index"/> is considered value
/// for the current request, otherwise <c>false</c>.
/// </returns>
public bool IsValidCandidate(int index)
{
// Friendliness for inlining
if ((uint)index >= Count)
{
ThrowIndexArgumentOutOfRangeException();
}
if (Count < BitVectorSize)
{
// Get the n-th bit
return _validity[0x00000001 << index];
}
else
{
return _largeCapactityValidity[index];
}
}
/// <summary>
/// Sets the validitity of the candidate at the provided index.
/// </summary>
/// <param name="index">The candidate index.</param>
/// <param name="value">
/// The value to set. If <c>true</c> the candidate is considered valid for the current request.
/// </param>
public void SetValidity(int index, bool value)
{
// Friendliness for inlining
if ((uint)index >= Count)
{
ThrowIndexArgumentOutOfRangeException();
}
if (Count < BitVectorSize)
{
// Set the n-th bit
_validity[0x00000001 << index] = value;
}
else
{
_largeCapactityValidity[index] = value;
}
}
private static void ThrowIndexArgumentOutOfRangeException()
{
throw new ArgumentOutOfRangeException("index");
}
}
}