From 2fec0ea791d2f837768d81961995eccd26f5d66d Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Thu, 15 Aug 2024 09:55:08 +0100 Subject: [PATCH] Cleanup and use concurrent dictionary (#77) --- src/Speckle.Core/Kits/HostAppVersion.cs | 21 -- src/Speckle.Core/Kits/HostApplication.cs | 13 -- src/Speckle.Core/Kits/HostApplications.cs | 220 ------------------ .../SerializationUtilities/CallsiteCache.cs | 44 ++-- 4 files changed, 20 insertions(+), 278 deletions(-) delete mode 100644 src/Speckle.Core/Kits/HostAppVersion.cs delete mode 100644 src/Speckle.Core/Kits/HostApplication.cs delete mode 100644 src/Speckle.Core/Kits/HostApplications.cs diff --git a/src/Speckle.Core/Kits/HostAppVersion.cs b/src/Speckle.Core/Kits/HostAppVersion.cs deleted file mode 100644 index f3e8c9ad..00000000 --- a/src/Speckle.Core/Kits/HostAppVersion.cs +++ /dev/null @@ -1,21 +0,0 @@ -namespace Speckle.Core.Kits; - -public enum HostAppVersion -{ - v3, - v6, - v7, - v8, - v2019, - v2020, - v2021, - v2022, - v2023, - v2024, - v2025, - v25, - v26, - v715, - v716, - v717 -} diff --git a/src/Speckle.Core/Kits/HostApplication.cs b/src/Speckle.Core/Kits/HostApplication.cs deleted file mode 100644 index 200783b4..00000000 --- a/src/Speckle.Core/Kits/HostApplication.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace Speckle.Core.Kits; - -public readonly struct HostApplication -{ - public string Name { get; } - public string Slug { get; } - - public HostApplication(string name, string slug) - { - Name = name; - Slug = slug; - } -} diff --git a/src/Speckle.Core/Kits/HostApplications.cs b/src/Speckle.Core/Kits/HostApplications.cs deleted file mode 100644 index fc38d763..00000000 --- a/src/Speckle.Core/Kits/HostApplications.cs +++ /dev/null @@ -1,220 +0,0 @@ -using System.Diagnostics.Contracts; - -namespace Speckle.Sdk.Host; - -/// -/// List of Host Applications - their slugs should match our ghost tags and ci/cd slugs -/// -public static class HostApplications -{ - public static string GetVersion(HostAppVersion version) => version.ToString().TrimStart('v'); - - public static readonly HostApplication Rhino = new("Rhino", "rhino"), - Grasshopper = new("Grasshopper", "grasshopper"), - Revit = new("Revit", "revit"), - Dynamo = new("Dynamo", "dynamo"), - Unity = new("Unity", "unity"), - GSA = new("GSA", "gsa"), - Civil = new("Civil 3D", "civil3d"), - Civil3D = new("Civil 3D", "civil3d"), - AutoCAD = new("AutoCAD", "autocad"), - MicroStation = new("MicroStation", "microstation"), - OpenRoads = new("OpenRoads", "openroads"), - OpenRail = new("OpenRail", "openrail"), - OpenBuildings = new("OpenBuildings", "openbuildings"), - ETABS = new("ETABS", "etabs"), - SAP2000 = new("SAP2000", "sap2000"), - CSiBridge = new("CSiBridge", "csibridge"), - SAFE = new("SAFE", "safe"), - TeklaStructures = new("Tekla Structures", "teklastructures"), - Dxf = new("DXF Converter", "dxf"), - Excel = new("Excel", "excel"), - Unreal = new("Unreal", "unreal"), - PowerBI = new("Power BI", "powerbi"), - Blender = new("Blender", "blender"), - QGIS = new("QGIS", "qgis"), - ArcGIS = new("ArcGIS", "arcgis"), - SketchUp = new("SketchUp", "sketchup"), - Archicad = new("Archicad", "archicad"), - TopSolid = new("TopSolid", "topsolid"), - Python = new("Python", "python"), - NET = new(".NET", "net"), - Navisworks = new("Navisworks", "navisworks"), - AdvanceSteel = new("Advance Steel", "advancesteel"), - Other = new("Other", "other"); - - /// - /// Gets a HostApplication form a string. It could be the versioned name or a string coming from a process running. - /// - /// String with the name of the app - /// - [Pure] - public static HostApplication GetHostAppFromString(string? appname) - { - if (appname == null) - { - return Other; - } - - appname = appname.ToLowerInvariant().Replace(" ", ""); - if (appname.Contains("dynamo")) - { - return Dynamo; - } - - if (appname.Contains("revit")) - { - return Revit; - } - - if (appname.Contains("autocad")) - { - return AutoCAD; - } - if (appname.Contains("civil3d")) - { - return Civil3D; - } - if (appname.Contains("civil")) - { - return Civil; - } - - if (appname.Contains("rhino")) - { - return Rhino; - } - - if (appname.Contains("grasshopper")) - { - return Grasshopper; - } - - if (appname.Contains("unity")) - { - return Unity; - } - - if (appname.Contains("gsa")) - { - return GSA; - } - - if (appname.Contains("microstation")) - { - return MicroStation; - } - - if (appname.Contains("openroads")) - { - return OpenRoads; - } - - if (appname.Contains("openrail")) - { - return OpenRail; - } - - if (appname.Contains("openbuildings")) - { - return OpenBuildings; - } - - if (appname.Contains("etabs")) - { - return ETABS; - } - - if (appname.Contains("sap")) - { - return SAP2000; - } - - if (appname.Contains("csibridge")) - { - return CSiBridge; - } - - if (appname.Contains("safe")) - { - return SAFE; - } - - if (appname.Contains("teklastructures")) - { - return TeklaStructures; - } - - if (appname.Contains("dxf")) - { - return Dxf; - } - - if (appname.Contains("excel")) - { - return Excel; - } - - if (appname.Contains("unreal")) - { - return Unreal; - } - - if (appname.Contains("powerbi")) - { - return PowerBI; - } - - if (appname.Contains("blender")) - { - return Blender; - } - - if (appname.Contains("qgis")) - { - return QGIS; - } - - if (appname.Contains("arcgis")) - { - return ArcGIS; - } - - if (appname.Contains("sketchup")) - { - return SketchUp; - } - - if (appname.Contains("archicad")) - { - return Archicad; - } - - if (appname.Contains("topsolid")) - { - return TopSolid; - } - - if (appname.Contains("python")) - { - return Python; - } - - if (appname.Contains("net")) - { - return NET; - } - - if (appname.Contains("navisworks")) - { - return Navisworks; - } - - if (appname.Contains("advancesteel")) - { - return AdvanceSteel; - } - - return new HostApplication(appname, appname); - } -} diff --git a/src/Speckle.Sdk/Serialisation/SerializationUtilities/CallsiteCache.cs b/src/Speckle.Sdk/Serialisation/SerializationUtilities/CallsiteCache.cs index 37023478..4b0e3f24 100644 --- a/src/Speckle.Sdk/Serialisation/SerializationUtilities/CallsiteCache.cs +++ b/src/Speckle.Sdk/Serialisation/SerializationUtilities/CallsiteCache.cs @@ -1,4 +1,5 @@ -using System.Runtime.CompilerServices; +using System.Collections.Concurrent; +using System.Runtime.CompilerServices; using Microsoft.CSharp.RuntimeBinder; namespace Speckle.Sdk.Serialisation.SerializationUtilities; @@ -11,33 +12,28 @@ internal static class CallSiteCache // And also // https://github.com/mgravell/fast-member/blob/master/FastMember/CallSiteCache.cs // by Marc Gravell, https://github.com/mgravell - private static readonly Dictionary>> s_setters = new(); + private static readonly ConcurrentDictionary>> s_setters = + new(); public static void SetValue(string propertyName, object target, object? value) { - lock (s_setters) - { - CallSite>? site; - - lock (s_setters) + var site = s_setters.GetOrAdd( + propertyName, + name => { - if (!s_setters.TryGetValue(propertyName, out site)) - { - var binder = Binder.SetMember( - CSharpBinderFlags.None, - propertyName, - typeof(CallSiteCache), - new List - { - CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), - CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) - } - ); - s_setters[propertyName] = site = CallSite>.Create(binder); - } + var binder = Binder.SetMember( + CSharpBinderFlags.None, + name, + typeof(CallSiteCache), + new List + { + CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), + CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) + } + ); + return CallSite>.Create(binder); } - - site.Target.Invoke(site, target, value); - } + ); + site.Target.Invoke(site, target, value); } }