|
5 | 5 | // Created: 2007.08.27 |
6 | 6 |
|
7 | 7 | using System; |
| 8 | +using System.Collections.Concurrent; |
8 | 9 | using System.Collections.Generic; |
9 | 10 | using System.Linq; |
10 | 11 | using System.Reflection; |
| 12 | +using System.Runtime.CompilerServices; |
11 | 13 | using System.Security.Cryptography; |
12 | 14 | using System.Text; |
13 | 15 | using Xtensive.Core; |
@@ -36,12 +38,15 @@ public sealed class NameBuilder |
36 | 38 | private const string ReferenceForeignKeyFormat = "FK_{0}_{1}_{2}"; |
37 | 39 | private const string HierarchyForeignKeyFormat = "FK_{0}_{1}"; |
38 | 40 |
|
39 | | - private readonly Dictionary<Pair<Type, string>, string> fieldNameCache = new Dictionary<Pair<Type, string>, string>(); |
40 | | - private readonly object _lock = new object(); |
| 41 | + private static readonly Func<PropertyInfo, string> fieldNameCacheValueFactory = |
| 42 | + field => field.GetAttribute<OverrideFieldNameAttribute>()?.Name ?? field.Name; |
| 43 | + |
41 | 44 | private readonly int maxIdentifierLength; |
42 | 45 | private readonly NamingConvention namingConvention; |
43 | 46 | private readonly bool isMultidatabase; |
44 | 47 | private readonly string defaultDatabase; |
| 48 | + private readonly ConcurrentDictionary<PropertyInfo, string> fieldNameCache = |
| 49 | + new ConcurrentDictionary<PropertyInfo, string>(); |
45 | 50 |
|
46 | 51 | /// <summary> |
47 | 52 | /// Gets the <see cref="Entity.TypeId"/> column name. |
@@ -181,24 +186,9 @@ public string BuildFieldName(FieldDef field) |
181 | 186 | return result; |
182 | 187 | } |
183 | 188 |
|
| 189 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
184 | 190 | private string BuildFieldNameInternal(PropertyInfo propertyInfo) |
185 | | - { |
186 | | - var key = new Pair<Type, string>(propertyInfo.ReflectedType, propertyInfo.Name); |
187 | | - |
188 | | - lock (fieldNameCache) { |
189 | | - string result; |
190 | | - if (fieldNameCache.TryGetValue(key, out result)) |
191 | | - return result; |
192 | | - var attribute = propertyInfo.GetAttribute<OverrideFieldNameAttribute>(); |
193 | | - if (attribute!=null) { |
194 | | - result = attribute.Name; |
195 | | - fieldNameCache.Add(key, result); |
196 | | - return result; |
197 | | - } |
198 | | - } |
199 | | - |
200 | | - return propertyInfo.Name; |
201 | | - } |
| 191 | + => fieldNameCache.GetOrAdd(propertyInfo, fieldNameCacheValueFactory); |
202 | 192 |
|
203 | 193 | /// <summary> |
204 | 194 | /// Builds the name of the field. |
|
0 commit comments