Skip to content

Commit ae4037a

Browse files
committed
Minimize code in components
1 parent 304f947 commit ae4037a

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

src/Components/Components/src/Microsoft.AspNetCore.Components.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
<!-- Abstractions -->
3333
<Compile Include="$(SharedSourceRoot)RouteValueDictionaryTrimmerWarning.cs" LinkBase="Reflection" />
3434
<Compile Include="$(SharedSourceRoot)UrlDecoder\UrlDecoder.cs" LinkBase="Routing" />
35-
<Compile Include="$(SharedSourceRoot)PropertyHelper\**\*.cs" LinkBase="Reflection" />
3635

3736
<!-- Infrastructure -->
3837
<Compile Include="$(RoutingSourceRoot)RouteValueEqualityComparer.cs" LinkBase="Routing" />

src/Http/Http.Abstractions/src/Routing/RouteValueDictionary.cs

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,19 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections;
5-
using System.Collections.Concurrent;
65
using System.Diagnostics;
76
using System.Diagnostics.CodeAnalysis;
87
using System.Linq;
9-
using System.Reflection.Metadata;
108
using System.Runtime.CompilerServices;
119
#if !COMPONENTS
10+
using System.Collections.Concurrent;
11+
using System.Reflection.Metadata;
1212
using Microsoft.AspNetCore.Http.Abstractions;
1313
using Microsoft.Extensions.Internal;
1414
using Microsoft.AspNetCore.Routing;
15-
#else
16-
using Microsoft.AspNetCore.Components.Routing;
17-
using Microsoft.AspNetCore.Components.Reflection;
1815
#endif
1916

20-
#if COMPONENTS
21-
[assembly: MetadataUpdateHandler(typeof(RouteValueDictionary.MetadataUpdateHandler))]
22-
#else
17+
#if !COMPONENTS
2318
[assembly: MetadataUpdateHandler(typeof(RouteValueDictionary.MetadataUpdateHandler))]
2419
#endif
2520

@@ -44,10 +39,14 @@ internal class RouteValueDictionary : IDictionary<string, object?>, IReadOnlyDic
4439
{
4540
// 4 is a good default capacity here because that leaves enough space for area/controller/action/id
4641
private const int DefaultCapacity = 4;
42+
#if !COMPONENTS
4743
private static readonly ConcurrentDictionary<Type, PropertyHelper[]> _propertyCache = new ConcurrentDictionary<Type, PropertyHelper[]>();
44+
#endif
4845

4946
internal KeyValuePair<string, object?>[] _arrayStorage;
47+
#if !COMPONENTS
5048
internal PropertyStorage? _propertyStorage;
49+
#endif
5150
private int _count;
5251

5352
#if !COMPONENTS
@@ -232,6 +231,7 @@ private void Initialize(IEnumerable<KeyValuePair<string, object?>> keyValueEnume
232231
[MemberNotNull(nameof(_arrayStorage))]
233232
private void Initialize(RouteValueDictionary dictionary)
234233
{
234+
#if !COMPONENTS
235235
if (dictionary._propertyStorage != null)
236236
{
237237
// PropertyStorage is immutable so we can just copy it.
@@ -240,6 +240,7 @@ private void Initialize(RouteValueDictionary dictionary)
240240
_arrayStorage = Array.Empty<KeyValuePair<string, object?>>();
241241
return;
242242
}
243+
#endif
243244

244245
var count = dictionary._count;
245246
if (count > 0)
@@ -389,14 +390,15 @@ public void Clear()
389390
return;
390391
}
391392

393+
#if !COMPONENTS
392394
if (_propertyStorage != null)
393395
{
394396
_arrayStorage = Array.Empty<KeyValuePair<string, object?>>();
395397
_propertyStorage = null;
396398
_count = 0;
397399
return;
398400
}
399-
401+
#endif
400402
Array.Clear(_arrayStorage, 0, _count);
401403
_count = 0;
402404
}
@@ -421,12 +423,16 @@ public bool ContainsKey(string key)
421423
[MethodImpl(MethodImplOptions.AggressiveInlining)]
422424
private bool ContainsKeyCore(string key)
423425
{
426+
#if !COMPONENTS
424427
if (_propertyStorage == null)
425428
{
426429
return ContainsKeyArray(key);
427430
}
428431

429432
return ContainsKeyProperties(key);
433+
#else
434+
return ContainsKeyArray(key);
435+
#endif
430436
}
431437

432438
/// <inheritdoc />
@@ -599,6 +605,9 @@ public bool TryGetValue(string key, out object? value)
599605
ThrowArgumentNullExceptionForKey();
600606
}
601607

608+
#if COMPONENTS
609+
return TryFindItem(key, out value);
610+
#else
602611
if (_propertyStorage == null)
603612
{
604613
return TryFindItem(key, out value);
@@ -627,6 +636,7 @@ private bool TryGetValueSlow(string key, out object? value)
627636

628637
value = default;
629638
return false;
639+
#endif
630640
}
631641

632642
[DoesNotReturn]
@@ -638,17 +648,25 @@ private static void ThrowArgumentNullExceptionForKey()
638648
[MethodImpl(MethodImplOptions.AggressiveInlining)]
639649
private void EnsureCapacity(int capacity)
640650
{
651+
#if !COMPONENTS
641652
if (_propertyStorage != null || _arrayStorage.Length < capacity)
642653
{
643654
EnsureCapacitySlow(capacity);
644655
}
656+
#else
657+
if (_arrayStorage.Length < capacity)
658+
{
659+
EnsureCapacitySlow(capacity);
660+
}
661+
#endif
645662
}
646663

647664
[UnconditionalSuppressMessage("Trimming", "IL2026",
648665
Justification = "The constructor that would result in _propertyStorage being non-null is annotated with RequiresUnreferencedCodeAttribute. " +
649666
"We do not need to additionally produce an error in this method since it is shared by trimmer friendly code paths.")]
650667
private void EnsureCapacitySlow(int capacity)
651668
{
669+
#if !COMPONENTS
652670
if (_propertyStorage != null)
653671
{
654672
var storage = _propertyStorage;
@@ -668,7 +686,7 @@ private void EnsureCapacitySlow(int capacity)
668686
_propertyStorage = null;
669687
return;
670688
}
671-
689+
#endif
672690
if (_arrayStorage.Length < capacity)
673691
{
674692
capacity = _arrayStorage.Length == 0 ? DefaultCapacity : _arrayStorage.Length * 2;
@@ -745,6 +763,7 @@ private bool ContainsKeyArray(string key)
745763
return false;
746764
}
747765

766+
#if !COMPONENTS
748767
[MethodImpl(MethodImplOptions.AggressiveInlining)]
749768
private bool ContainsKeyProperties(string key)
750769
{
@@ -761,6 +780,7 @@ private bool ContainsKeyProperties(string key)
761780

762781
return false;
763782
}
783+
#endif
764784

765785
/// <inheritdoc />
766786
public struct Enumerator : IEnumerator<KeyValuePair<string, object?>>
@@ -801,13 +821,22 @@ public bool MoveNext()
801821
{
802822
var dictionary = _dictionary;
803823

824+
#if !COMPONENTS
804825
// The uncommon case is that the propertyStorage is in use
805826
if (dictionary._propertyStorage == null && ((uint)_index < (uint)dictionary._count))
806827
{
807828
Current = dictionary._arrayStorage[_index];
808829
_index++;
809830
return true;
810831
}
832+
#else
833+
if (((uint)_index < (uint)dictionary._count))
834+
{
835+
Current = dictionary._arrayStorage[_index];
836+
_index++;
837+
return true;
838+
}
839+
#endif
811840

812841
return MoveNextRare();
813842
}
@@ -818,6 +847,7 @@ public bool MoveNext()
818847
private bool MoveNextRare()
819848
{
820849
var dictionary = _dictionary;
850+
#if !COMPONENTS
821851
if (dictionary._propertyStorage != null && ((uint)_index < (uint)dictionary._count))
822852
{
823853
var storage = dictionary._propertyStorage;
@@ -826,6 +856,7 @@ private bool MoveNextRare()
826856
_index++;
827857
return true;
828858
}
859+
#endif
829860

830861
_index = dictionary._count;
831862
Current = default;
@@ -840,6 +871,7 @@ public void Reset()
840871
}
841872
}
842873

874+
#if !COMPONENTS
843875
[RequiresUnreferencedCode("This API is not trim safe - from PropertyHelper")]
844876
internal sealed class PropertyStorage
845877
{
@@ -889,6 +921,7 @@ internal static void ClearCache(Type[]? _)
889921
_propertyCache.Clear();
890922
}
891923
}
924+
#endif
892925

893926
private sealed class RouteValueDictionaryDebugView(RouteValueDictionary dictionary)
894927
{

0 commit comments

Comments
 (0)