This repository has been archived by the owner on Jun 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
Improved performance of the Instance property #39
Merged
airbreather
merged 4 commits into
NetTopologySuite:develop
from
YohDeadfall:InstancePerf
Apr 28, 2018
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
|
||
namespace GeoAPI | ||
{ | ||
|
@@ -9,90 +9,67 @@ namespace GeoAPI | |
/// </summary> | ||
public static class GeometryServiceProvider | ||
{ | ||
private static IGeometryServices _instance; | ||
private static readonly object LockObject = new object(); | ||
private static volatile IGeometryServices _instance; | ||
private static readonly object _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 => _instance ?? InitializeInstance(); | ||
set | ||
{ | ||
if (value == null) | ||
throw new ArgumentNullException("value", "You must not assign null to Instance!"); | ||
|
||
lock (LockObject) | ||
{ | ||
throw new ArgumentNullException(nameof(value)); | ||
lock (_lock) | ||
_instance = value; | ||
} | ||
} | ||
} | ||
|
||
#if HAS_SYSTEM_REFLECTION_ASSEMBLY_GETEXPORTEDTYPES | ||
private static IEnumerable<Type> GetLoadableTypes(Assembly assembly) | ||
private static IGeometryServices InitializeInstance() | ||
{ | ||
if (assembly == null) | ||
return new Type[0]; | ||
|
||
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) | ||
{ | ||
list.Add(t); | ||
} | ||
return list; | ||
} | ||
catch | ||
#if COMPAT_BOOTSTRAP_USING_REFLECTION && HAS_SYSTEM_APPDOMAIN_GETASSEMBLIES && HAS_SYSTEM_REFLECTION_ASSEMBLY_GETEXPORTEDTYPES | ||
lock (_lock) | ||
{ | ||
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 | ||
var instance = _instance; | ||
if (instance != null) return instance; | ||
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) | ||
{ | ||
if (assembly.GlobalAssemblyCache && assembly.CodeBase == Assembly.GetExecutingAssembly().CodeBase) | ||
continue; | ||
|
||
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; | ||
var assemblyType = assembly.GetType(); | ||
if (assemblyType == typeof(AssemblyBuilder) || | ||
assemblyType.FullName == "System.Reflection.Emit.InternalAssemblyBuilder") | ||
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; | ||
Type[] types; | ||
|
||
var constuctors = t.GetConstructors(); | ||
foreach (var constructorInfo in constuctors) | ||
try | ||
{ | ||
if (constructorInfo.IsPublic && constructorInfo.GetParameters().Length == 0) | ||
return (IGeometryServices)Activator.CreateInstance(t); | ||
types = assembly.GetExportedTypes(); | ||
} | ||
catch (ReflectionTypeLoadException ex) | ||
{ | ||
types = ex.Types; | ||
} | ||
catch (Exception) | ||
{ | ||
continue; | ||
} | ||
|
||
var requiredType = typeof(IGeometryServices); | ||
foreach (var type in types) | ||
{ | ||
if (type.IsNotPublic || type.IsInterface || type.IsAbstract || !requiredType.IsAssignableFrom(type)) | ||
continue; | ||
|
||
foreach (var constructor in type.GetConstructors()) | ||
{ | ||
if (constructor.IsPublic && constructor.GetParameters().Length == 0) | ||
return _instance = instance = (IGeometryServices)Activator.CreateInstance(type); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, fixed. |
||
} | ||
} | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Foreword: I haven't actually thought this through completely.
I don't actually think we need this to be
volatile
, and if we think that this property is going to be "hot", then we actually don't really want it to bevolatile
. Every other read/write of this field occurs immediately after aMonitor.Enter
call, which triggers a full memory barrier itself, so thevolatile
semantics seem to only affect the hot path.So it seems to me that the worst that can happen without
volatile
is that every CPU comes in here with a stale cache and observes an uninitialized value, then they all hit thelock
, then one of them initializes it properly (slow), then every other CPU sees the value from the other thread one-by-one (fast), then for the rest of the application every access is as fast as it can possibly be.It seems like that's also the worst-case for
volatile
, except that at the end, every access for the rest of the application still has to go through a partial memory barrier. I don't actually see a window wherevolatile
helps at all, compared to novolatile
...