diff --git a/Orm/Xtensive.Orm/Orm/Providers/NameBuilder.cs b/Orm/Xtensive.Orm/Orm/Providers/NameBuilder.cs index a341c22a81..b74e73d6e9 100644 --- a/Orm/Xtensive.Orm/Orm/Providers/NameBuilder.cs +++ b/Orm/Xtensive.Orm/Orm/Providers/NameBuilder.cs @@ -5,9 +5,11 @@ // Created: 2007.08.27 using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Reflection; +using System.Runtime.CompilerServices; using System.Security.Cryptography; using System.Text; using Xtensive.Core; @@ -36,12 +38,15 @@ public sealed class NameBuilder private const string ReferenceForeignKeyFormat = "FK_{0}_{1}_{2}"; private const string HierarchyForeignKeyFormat = "FK_{0}_{1}"; - private readonly Dictionary, string> fieldNameCache = new Dictionary, string>(); - private readonly object _lock = new object(); + private static readonly Func fieldNameCacheValueFactory = + field => field.GetAttribute()?.Name ?? field.Name; + private readonly int maxIdentifierLength; private readonly NamingConvention namingConvention; private readonly bool isMultidatabase; private readonly string defaultDatabase; + private readonly ConcurrentDictionary fieldNameCache = + new ConcurrentDictionary(); /// /// Gets the column name. @@ -181,24 +186,9 @@ public string BuildFieldName(FieldDef field) return result; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] private string BuildFieldNameInternal(PropertyInfo propertyInfo) - { - var key = new Pair(propertyInfo.ReflectedType, propertyInfo.Name); - - lock (fieldNameCache) { - string result; - if (fieldNameCache.TryGetValue(key, out result)) - return result; - var attribute = propertyInfo.GetAttribute(); - if (attribute!=null) { - result = attribute.Name; - fieldNameCache.Add(key, result); - return result; - } - } - - return propertyInfo.Name; - } + => fieldNameCache.GetOrAdd(propertyInfo, fieldNameCacheValueFactory); /// /// Builds the name of the field.