1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ using System . Collections . Concurrent ;
5
+ using Microsoft . AspNetCore . Components . HotReload ;
6
+
4
7
namespace Microsoft . AspNetCore . Components ;
5
8
6
9
internal sealed class ChangeDetection
7
10
{
11
+ private static readonly ConcurrentDictionary < Type , bool > _immutableObjectTypesCache = new ( ) ;
12
+
13
+ static ChangeDetection ( )
14
+ {
15
+ if ( HotReloadManager . Default . MetadataUpdateSupported )
16
+ {
17
+ HotReloadManager . Default . OnDeltaApplied += _immutableObjectTypesCache . Clear ;
18
+ }
19
+ }
20
+
8
21
public static bool MayHaveChanged < T1 , T2 > ( T1 oldValue , T2 newValue )
9
22
{
10
23
var oldIsNotNull = oldValue != null ;
@@ -30,17 +43,18 @@ public static bool MayHaveChanged<T1, T2>(T1 oldValue, T2 newValue)
30
43
return false ;
31
44
}
32
45
33
- // The contents of this list need to trade off false negatives against computation
34
- // time. So we don't want a huge list of types to check (or would have to move to
35
- // a hashtable lookup, which is differently expensive). It's better not to include
36
- // uncommon types here even if they are known to be immutable.
37
46
// This logic assumes that no new System.TypeCode enum entries have been declared since 7.0, or at least that any new ones
38
47
// represent immutable types. If System.TypeCode changes, review this logic to ensure it is still correct.
39
48
// Supported immutable types : bool, byte, sbyte, short, ushort, int, uint, long, ulong, char, double,
40
49
// string, DateTime, decimal, Guid, Enum, EventCallback, EventCallback<>.
41
50
// For performance reasons, the following immutable types are not supported: IntPtr, UIntPtr, Type.
42
51
private static bool IsKnownImmutableType ( Type type )
43
52
=> Type . GetTypeCode ( type ) != TypeCode . Object
44
- || type == typeof ( Guid )
53
+ || _immutableObjectTypesCache . GetOrAdd ( type , IsImmutableObjectTypeCore ) ;
54
+
55
+ private static bool IsImmutableObjectTypeCore ( Type type )
56
+ => type == typeof ( Guid )
57
+ || type == typeof ( DateOnly )
58
+ || type == typeof ( TimeOnly )
45
59
|| typeof ( IEventCallback ) . IsAssignableFrom ( type ) ;
46
60
}
0 commit comments