Skip to content
This repository has been archived by the owner on Jun 22, 2023. It is now read-only.

Improved performance of the Instance property #39

Merged
merged 4 commits into from
Apr 28, 2018
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
114 changes: 44 additions & 70 deletions src/GeoAPI/GeometryServiceProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
#if COMPAT_BOOTSTRAP_USING_REFLECTION && HAS_SYSTEM_APPDOMAIN_GETASSEMBLIES && HAS_SYSTEM_REFLECTION_ASSEMBLY_GETEXPORTEDTYPES
using System.Reflection;
using System.Threading;
#endif

namespace GeoAPI
{
Expand All @@ -9,90 +11,62 @@ namespace GeoAPI
/// </summary>
public static class GeometryServiceProvider
{
private static IGeometryServices _instance;
private static readonly object LockObject = new object();
private static volatile IGeometryServices s_instance;
private static readonly object s_lock = new object();

/// <summary>
/// Gets or sets the <see cref="IGeometryServices"/> instance.
/// </summary>
public static IGeometryServices Instance
{
get
{
lock (LockObject)
{
return _instance ?? (_instance = ReflectInstance());
}
}
get => s_instance ?? InitializeInstance();
set => s_instance = value ?? throw new ArgumentNullException(nameof(value));
}

set
private static IGeometryServices InitializeInstance()
{
#if COMPAT_BOOTSTRAP_USING_REFLECTION && HAS_SYSTEM_APPDOMAIN_GETASSEMBLIES && HAS_SYSTEM_REFLECTION_ASSEMBLY_GETEXPORTEDTYPES
lock (s_lock)
{
if (value == null)
throw new ArgumentNullException("value", "You must not assign null to Instance!");

lock (LockObject)
var instance = s_instance;
if (instance != null) return instance;
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
_instance = value;
}
}
}
if (assembly.GlobalAssemblyCache && assembly.CodeBase == Assembly.GetExecutingAssembly().CodeBase)
continue;

#if HAS_SYSTEM_REFLECTION_ASSEMBLY_GETEXPORTEDTYPES
private static IEnumerable<Type> GetLoadableTypes(Assembly assembly)
{
if (assembly == null)
return new Type[0];
var assemblyType = assembly.GetType().FullName;
if (assemblyType == "System.Reflection.Emit.AssemblyBuilder" ||
assemblyType == "System.Reflection.Emit.InternalAssemblyBuilder")
continue;

try
{
return assembly.GetExportedTypes();
}
catch (ReflectionTypeLoadException ex)
{
var types = ex.Types;
IList<Type> list = new List<Type>(types.Length);
foreach (var t in types)
if (t != null && t.IsPublic)
Type[] types;

try
{
list.Add(t);
types = assembly.GetExportedTypes();
}
catch (ReflectionTypeLoadException ex)
{
types = ex.Types;
}
catch (Exception)
{
continue;
}
return list;
}
catch
{
return new Type[0];
}
}
#else
private static IEnumerable<Type> GetLoadableTypes(Assembly assembly)
{
throw new NotSupportedException("The compact framework does not support retrieve exported types from assembly");
}
#endif

private static IGeometryServices ReflectInstance()
{
#if COMPAT_BOOTSTRAP_USING_REFLECTION && HAS_SYSTEM_APPDOMAIN_GETASSEMBLIES
var a = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in a)
{
// Take a look at issue 114: http://code.google.com/p/nettopologysuite/issues/detail?id=114
if (assembly is System.Reflection.Emit.AssemblyBuilder) continue;
if (assembly.GetType().FullName == "System.Reflection.Emit.InternalAssemblyBuilder") continue;
if (assembly.GlobalAssemblyCache && assembly.CodeBase == Assembly.GetExecutingAssembly().CodeBase) continue;

foreach (var t in GetLoadableTypes(assembly))
{
if (t.IsInterface) continue;
if (t.IsAbstract) continue;
if (t.IsNotPublic) continue;
if (!typeof(IGeometryServices).IsAssignableFrom(t)) continue;

var constuctors = t.GetConstructors();
foreach (var constructorInfo in constuctors)
var requiredType = typeof(IGeometryServices);
foreach (var type in types)
{
if (constructorInfo.IsPublic && constructorInfo.GetParameters().Length == 0)
return (IGeometryServices)Activator.CreateInstance(t);
if (type.IsNotPublic || type.IsInterface || type.IsAbstract || !requiredType.IsAssignableFrom(type))
continue;

foreach (var constructor in type.GetConstructors())
if (constructor.IsPublic && constructor.GetParameters().Length == 0)
{
Interlocked.CompareExchange(ref s_instance, (IGeometryServices)Activator.CreateInstance(type), null);
return s_instance;
}
}
}
}
Expand Down