|
2 | 2 | // Licensed under the MIT license.
|
3 | 3 |
|
4 | 4 | using System;
|
5 |
| -using System.Collections.Concurrent; |
| 5 | +using System.Collections.Generic; |
6 | 6 | using System.Reflection;
|
7 |
| -using System.Runtime.CompilerServices; |
8 | 7 | using Microsoft.ClearScript.Util;
|
9 | 8 |
|
10 | 9 | namespace Microsoft.ClearScript
|
11 | 10 | {
|
12 | 11 | internal static partial class CustomAttributes
|
13 | 12 | {
|
14 |
| - private static readonly ConcurrentDictionary<(ICustomAttributeProvider, Type), object> keyCache = new ConcurrentDictionary<(ICustomAttributeProvider, Type), object>(); |
15 |
| - private static readonly ConditionalWeakTable<object, object> attributeCache = new ConditionalWeakTable<object, object>(); |
| 13 | + private static readonly object cacheLock = new object(); |
16 | 14 |
|
17 | 15 | public static T[] GetOrLoad<T>(ICustomAttributeProvider resource, bool inherit) where T : Attribute
|
18 | 16 | {
|
19 |
| - return (T[])attributeCache.GetValue(GetKey<T>(resource), _ => HostSettings.CustomAttributeLoader.LoadCustomAttributes<T>(resource, inherit) ?? ArrayHelpers.GetEmptyArray<T>()); |
| 17 | + lock (cacheLock) |
| 18 | + { |
| 19 | + return GetOrLoad<T>(cache.GetOrCreateValue(resource), resource, inherit); |
| 20 | + } |
20 | 21 | }
|
21 | 22 |
|
22 | 23 | public static bool Has<T>(ICustomAttributeProvider resource, bool inherit) where T : Attribute
|
23 | 24 | {
|
24 |
| - return GetOrLoad<T>(resource, inherit).Length > 0; |
| 25 | + lock (cacheLock) |
| 26 | + { |
| 27 | + return Has<T>(cache.GetOrCreateValue(resource), resource, inherit); |
| 28 | + } |
25 | 29 | }
|
26 | 30 |
|
27 |
| - private static object GetKey<T>(ICustomAttributeProvider resource) where T : Attribute |
| 31 | + private static T[] GetOrLoad<T>(CacheEntry entry, ICustomAttributeProvider resource, bool inherit) where T : Attribute |
28 | 32 | {
|
29 |
| - return keyCache.GetOrAdd((resource, typeof(T)), _ => new object()); |
| 33 | + if (entry.TryGet<T>(out var attrs)) |
| 34 | + { |
| 35 | + return attrs; |
| 36 | + } |
| 37 | + |
| 38 | + attrs = GetOrLoad<T>(GetIsBypass(entry, resource), resource, inherit); |
| 39 | + entry.Add(attrs); |
| 40 | + |
| 41 | + return attrs; |
| 42 | + } |
| 43 | + |
| 44 | + private static bool Has<T>(CacheEntry entry, ICustomAttributeProvider resource, bool inherit) where T : Attribute |
| 45 | + { |
| 46 | + return GetOrLoad<T>(entry, resource, inherit).Length > 0; |
| 47 | + } |
| 48 | + |
| 49 | + private static T[] GetOrLoad<T>(bool isBypass, ICustomAttributeProvider resource, bool inherit) where T : Attribute |
| 50 | + { |
| 51 | + var loader = isBypass ? HostSettings.DefaultCustomAttributeLoader : HostSettings.CustomAttributeLoader; |
| 52 | + return loader.LoadCustomAttributes<T>(resource, inherit) ?? ArrayHelpers.GetEmptyArray<T>(); |
| 53 | + } |
| 54 | + |
| 55 | + private static bool Has<T>(bool isBypass, ICustomAttributeProvider resource, bool inherit) where T : Attribute |
| 56 | + { |
| 57 | + return GetOrLoad<T>(isBypass, resource, inherit).Length > 0; |
| 58 | + } |
| 59 | + |
| 60 | + private static bool GetIsBypass(ICustomAttributeProvider resource) |
| 61 | + { |
| 62 | + // ReSharper disable once InconsistentlySynchronizedField |
| 63 | + return GetIsBypass(cache.GetOrCreateValue(resource), resource); |
| 64 | + } |
| 65 | + |
| 66 | + private static bool GetIsBypass(CacheEntry entry, ICustomAttributeProvider resource) |
| 67 | + { |
| 68 | + if (!entry.IsBypass.HasValue) |
| 69 | + { |
| 70 | + entry.IsBypass = GetIsBypassInternal(resource); |
| 71 | + } |
| 72 | + |
| 73 | + return entry.IsBypass.Value; |
| 74 | + } |
| 75 | + |
| 76 | + private static bool GetIsBypassInternal(ICustomAttributeProvider resource) |
| 77 | + { |
| 78 | + if (Has<BypassCustomAttributeLoaderAttribute>(true, resource, false)) |
| 79 | + { |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + var parent = GetParent(resource); |
| 84 | + if (parent != null) |
| 85 | + { |
| 86 | + return GetIsBypass(parent); |
| 87 | + } |
| 88 | + |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + private static ICustomAttributeProvider GetParent(ICustomAttributeProvider resource) |
| 93 | + { |
| 94 | + if (resource is ParameterInfo parameter) |
| 95 | + { |
| 96 | + return parameter.Member; |
| 97 | + } |
| 98 | + |
| 99 | + if (resource is Type type) |
| 100 | + { |
| 101 | + return (type.DeclaringType as ICustomAttributeProvider) ?? type.Module; |
| 102 | + } |
| 103 | + |
| 104 | + if (resource is MemberInfo member) |
| 105 | + { |
| 106 | + return member.DeclaringType; |
| 107 | + } |
| 108 | + |
| 109 | + if (resource is Module module) |
| 110 | + { |
| 111 | + return module.Assembly; |
| 112 | + } |
| 113 | + |
| 114 | + return null; |
| 115 | + } |
| 116 | + |
| 117 | + #region Nested type: CacheEntry |
| 118 | + |
| 119 | + // ReSharper disable ClassNeverInstantiated.Local |
| 120 | + |
| 121 | + private sealed class CacheEntry |
| 122 | + { |
| 123 | + private readonly Dictionary<Type, object> map = new Dictionary<Type, object>(); |
| 124 | + |
| 125 | + public bool? IsBypass { get; set; } |
| 126 | + |
| 127 | + public void Add<T>(T[] attrs) |
| 128 | + { |
| 129 | + map.Add(typeof(T), attrs); |
| 130 | + } |
| 131 | + |
| 132 | + public bool TryGet<T>(out T[] attrs) |
| 133 | + { |
| 134 | + if (map.TryGetValue(typeof(T), out var attrsObject)) |
| 135 | + { |
| 136 | + attrs = attrsObject as T[]; |
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + attrs = null; |
| 141 | + return false; |
| 142 | + } |
30 | 143 | }
|
| 144 | + |
| 145 | + // ReSharper restore ClassNeverInstantiated.Local |
| 146 | + |
| 147 | + #endregion |
| 148 | + } |
| 149 | + |
| 150 | + [AttributeUsage(AttributeTargets.All, Inherited = false)] |
| 151 | + internal sealed class BypassCustomAttributeLoaderAttribute : Attribute |
| 152 | + { |
31 | 153 | }
|
32 | 154 | }
|
0 commit comments