Skip to content

Optimize StaticProxyFactory GetProxy and GetFieldInterceptionProxy methods #1968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions src/NHibernate/Proxy/StaticProxyFactory.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Engine;
using NHibernate.Intercept;
using NHibernate.Type;

namespace NHibernate.Proxy
{
public sealed class StaticProxyFactory : AbstractProxyFactory
{
private static readonly ConcurrentDictionary<ProxyCacheEntry, Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy>>
Cache = new ConcurrentDictionary<ProxyCacheEntry, Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy>>();
private static readonly ConcurrentDictionary<ProxyCacheEntry, Func<NHibernateProxyFactoryInfo, IFieldInterceptorAccessor>>
FieldInterceptorCache = new ConcurrentDictionary<ProxyCacheEntry, Func<NHibernateProxyFactoryInfo, IFieldInterceptorAccessor>>();
private static readonly ConcurrentDictionary<System.Type, Func<NHibernateProxyFactoryInfo, IFieldInterceptorAccessor>>
FieldInterceptorCache = new ConcurrentDictionary<System.Type, Func<NHibernateProxyFactoryInfo, IFieldInterceptorAccessor>>();

private static readonly INHibernateLogger Log = NHibernateLogger.For(typeof(StaticProxyFactory));

private NHibernateProxyFactoryInfo _proxyFactoryInfo;
private ProxyCacheEntry _cacheEntry;

public override INHibernateProxy GetProxy(object id, ISessionImplementor session)
{
try
{
var cacheEntry = new ProxyCacheEntry(IsClassProxy ? PersistentClass : typeof(object), Interfaces);
var proxyActivator = Cache.GetOrAdd(cacheEntry, pke => CreateProxyActivator(pke));
var proxyActivator = Cache.GetOrAdd(_cacheEntry, pke => CreateProxyActivator(pke));
return proxyActivator(
new LiteLazyInitializer(EntityName, id, session, PersistentClass),
new NHibernateProxyFactoryInfo(EntityName, PersistentClass, Interfaces, GetIdentifierMethod, SetIdentifierMethod, ComponentIdType));
_proxyFactoryInfo);
}
catch (Exception ex)
{
Expand All @@ -32,6 +37,26 @@ public override INHibernateProxy GetProxy(object id, ISessionImplementor session
}
}

public override void PostInstantiate(
string entityName,
System.Type persistentClass,
ISet<System.Type> interfaces,
MethodInfo getIdentifierMethod,
MethodInfo setIdentifierMethod,
IAbstractComponentType componentIdType)
{
base.PostInstantiate(entityName, persistentClass, interfaces, getIdentifierMethod, setIdentifierMethod, componentIdType);

_proxyFactoryInfo = new NHibernateProxyFactoryInfo(
EntityName,
PersistentClass,
Interfaces,
GetIdentifierMethod,
SetIdentifierMethod,
ComponentIdType);
_cacheEntry = new ProxyCacheEntry(IsClassProxy ? PersistentClass : typeof(object), Interfaces);
}

private Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy> CreateProxyActivator(ProxyCacheEntry pke)
{
var proxyBuilder = new NHibernateProxyBuilder(GetIdentifierMethod, SetIdentifierMethod, ComponentIdType, OverridesEquals);
Expand All @@ -51,15 +76,13 @@ public override object GetFieldInterceptionProxy(object instanceToWrap)

public object GetFieldInterceptionProxy()
{
var cacheEntry = new ProxyCacheEntry(PersistentClass, System.Type.EmptyTypes);
var proxyActivator = FieldInterceptorCache.GetOrAdd(cacheEntry, CreateFieldInterceptionProxyActivator);
return proxyActivator(
new NHibernateProxyFactoryInfo(EntityName, PersistentClass, Interfaces, GetIdentifierMethod, SetIdentifierMethod, ComponentIdType));
var proxyActivator = FieldInterceptorCache.GetOrAdd(PersistentClass, CreateFieldInterceptionProxyActivator);
return proxyActivator(_proxyFactoryInfo);
}

private Func<NHibernateProxyFactoryInfo, IFieldInterceptorAccessor> CreateFieldInterceptionProxyActivator(ProxyCacheEntry pke)
private Func<NHibernateProxyFactoryInfo, IFieldInterceptorAccessor> CreateFieldInterceptionProxyActivator(System.Type baseType)
{
var type = FieldInterceptorProxyBuilder.CreateProxyType(pke.BaseType);
var type = FieldInterceptorProxyBuilder.CreateProxyType(baseType);
var ctor = type.GetConstructor(new[] { typeof(NHibernateProxyFactoryInfo) });
var pf = Expression.Parameter(typeof(NHibernateProxyFactoryInfo));
return Expression.Lambda<Func<NHibernateProxyFactoryInfo, IFieldInterceptorAccessor>>(Expression.New(ctor, pf), pf).Compile();
Expand Down