From 7e4b75fa9298255398453b867a3e8c03817a24b4 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Tue, 23 Jun 2026 11:09:13 +0200 Subject: [PATCH 01/11] [bgen] Cache attribute query results per (provider, type) pair GetCustomAttributes was being called repeatedly on the same providers. Each call would re-invoke GetCustomAttributesData, iterate all attributes, and re-instantiate matching ones. Cache results in a Dictionary<(ICustomAttributeProvider, Type), object> so subsequent queries for the same provider+type return instantly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/bgen/AttributeManager.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/bgen/AttributeManager.cs b/src/bgen/AttributeManager.cs index 4e3b3acbb927..00e31630f92f 100644 --- a/src/bgen/AttributeManager.cs +++ b/src/bgen/AttributeManager.cs @@ -21,6 +21,10 @@ public class AttributeManager { // Cache raw GetCustomAttributesData() results per provider to avoid repeated reflection allocations. readonly Dictionary> rawAttributeCache = new (); + // Cache attribute query results per (provider, attribute type) to avoid + // repeated reflection and attribute conversion work. + readonly Dictionary<(ICustomAttributeProvider, System.Type), object> attributeCache = new (); + TypeCache TypeCache { get; } public AttributeManager (TypeCache typeCache) @@ -500,7 +504,16 @@ T [] FilterAttributes (IList? attributes, ICustomAttribu public virtual T [] GetCustomAttributes (ICustomAttributeProvider? provider) where T : System.Attribute { - return FilterAttributes (GetAttributes (provider), provider); + if (provider is null) + return Array.Empty (); + + var key = (provider, typeof (T)); + if (attributeCache.TryGetValue (key, out var cached)) + return (T []) cached; + + var result = FilterAttributes (GetAttributes (provider), provider); + attributeCache [key] = result; + return result; } [return: NotNullIfNotNull (nameof (provider))] From 184fabb27904840e5d6fe2fb7c66a3b8ccba5750 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Tue, 23 Jun 2026 11:09:30 +0200 Subject: [PATCH 02/11] [bgen] Cache HasAttribute results Add a Dictionary<(ICustomAttributeProvider, Type), bool> cache for HasAttribute queries. This method is called thousands of times per platform with the same arguments. Caching the boolean result avoids repeated GetCustomAttributesData() + type comparison work. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/bgen/AttributeManager.cs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/bgen/AttributeManager.cs b/src/bgen/AttributeManager.cs index 00e31630f92f..b579a51b37d1 100644 --- a/src/bgen/AttributeManager.cs +++ b/src/bgen/AttributeManager.cs @@ -25,6 +25,9 @@ public class AttributeManager { // repeated reflection and attribute conversion work. readonly Dictionary<(ICustomAttributeProvider, System.Type), object> attributeCache = new (); + // Cache HasAttribute results to avoid repeated GetCustomAttributesData() calls. + readonly Dictionary<(ICustomAttributeProvider, System.Type), bool> hasAttributeCache = new (); + TypeCache TypeCache { get; } public AttributeManager (TypeCache typeCache) @@ -551,21 +554,32 @@ public virtual bool HasAttribute (ICustomAttributeProvider? provider) where T if (provider is null) return false; + var key = (provider, typeof (T)); + if (hasAttributeCache.TryGetValue (key, out var cached)) + return cached; + var attributeType = ConvertTypeToMeta (typeof (T)); var attribs = GetAttributes (provider); - if (attribs is null || attribs.Count == 0) + if (attribs is null || attribs.Count == 0) { + hasAttributeCache [key] = false; return false; + } for (int i = 0; i < attribs.Count; i++) { var attrib = attribs [i]; // == when comparing types uses reference equality, which is what we want here. var currentType = ConvertTypeToMeta (attrib.GetAttributeType ()); - if (currentType == attributeType) + if (currentType == attributeType) { + hasAttributeCache [key] = true; return true; - if (currentType.IsSubclassOf (attributeType)) + } + if (currentType.IsSubclassOf (attributeType)) { + hasAttributeCache [key] = true; return true; + } } + hasAttributeCache [key] = false; return false; } From 07cbcb006d282a8b51131cedcb7d3662f42c1a17 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Tue, 23 Jun 2026 11:09:58 +0200 Subject: [PATCH 03/11] [bgen] Cache common platform attributes as singletons in AttributeFactory Cache no-version/no-message IntroducedAttribute and UnavailableAttribute instances as static singletons (one per platform). Return cached instances from CreateNoVersionSupportedAttribute, CreateUnsupportedAttribute, and CloneFromOtherPlatform when applicable, avoiding repeated heap allocations for identical platform attributes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/bgen/AttributeFactory.cs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/bgen/AttributeFactory.cs b/src/bgen/AttributeFactory.cs index fe9dd2551e59..4c1e9e89b2d6 100644 --- a/src/bgen/AttributeFactory.cs +++ b/src/bgen/AttributeFactory.cs @@ -36,14 +36,31 @@ public static T CreateNewAttribute (PlatformName platform, string? message = return CreateNewAttribute (args.GetCtorTypes (), args.GetCtorValues ()); } + static readonly IntroducedAttribute [] noVersionSupportedCache = new IntroducedAttribute [] { + new (PlatformName.iOS), + new (PlatformName.TvOS), + new (PlatformName.MacOSX), + new (PlatformName.MacCatalyst), + }; + + static readonly UnavailableAttribute [] unsupportedCache = new UnavailableAttribute [] { + new (PlatformName.iOS), + new (PlatformName.MacCatalyst), + new (PlatformName.MacOSX), + new (PlatformName.TvOS), + }; + public static IntroducedAttribute CreateNoVersionSupportedAttribute (PlatformName platform) { switch (platform) { case PlatformName.iOS: + return noVersionSupportedCache [0]; case PlatformName.TvOS: + return noVersionSupportedCache [1]; case PlatformName.MacOSX: + return noVersionSupportedCache [2]; case PlatformName.MacCatalyst: - return new (platform); + return noVersionSupportedCache [3]; case PlatformName.WatchOS: throw new InvalidOperationException ("CreateNoVersionSupportedAttribute for WatchOS never makes sense"); default: @@ -55,10 +72,13 @@ public static UnavailableAttribute CreateUnsupportedAttribute (PlatformName plat { switch (platform) { case PlatformName.iOS: + return unsupportedCache [0]; case PlatformName.MacCatalyst: + return unsupportedCache [1]; case PlatformName.MacOSX: + return unsupportedCache [2]; case PlatformName.TvOS: - return new (platform); + return unsupportedCache [3]; case PlatformName.WatchOS: throw new InvalidOperationException ("CreateUnsupportedAttribute for WatchOS never makes sense"); default: @@ -68,6 +88,16 @@ public static UnavailableAttribute CreateUnsupportedAttribute (PlatformName plat public static AvailabilityBaseAttribute CloneFromOtherPlatform (AvailabilityBaseAttribute attr, PlatformName platform) { + if (attr.Version is null && string.IsNullOrEmpty (attr.Message)) { + // For no-version, no-message attributes, return the cached singletons + switch (attr.AvailabilityKind) { + case AvailabilityKind.Introduced: + return CreateNoVersionSupportedAttribute (platform); + case AvailabilityKind.Unavailable: + return CreateUnsupportedAttribute (platform); + } + } + if (attr.Version is null) { switch (attr.AvailabilityKind) { case AvailabilityKind.Introduced: From 4219a8cf006af8c2d91bf543435299562258378a Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Tue, 23 Jun 2026 11:10:39 +0200 Subject: [PATCH 04/11] [bgen] Cache GetAllParentAttributes results per MemberInfo Cache GetAllParentAttributes results per MemberInfo context. Since callers only read from the returned list (never mutate it), the cached list can be safely shared across multiple callers with the same context. This avoids re-walking the parent chain and re-querying attributes for the same member on repeated calls. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/bgen/Generator.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/bgen/Generator.cs b/src/bgen/Generator.cs index f1221a33dafb..4299e209b1d4 100644 --- a/src/bgen/Generator.cs +++ b/src/bgen/Generator.cs @@ -2548,17 +2548,24 @@ static MemberInfo FindContainingContext (MemberInfo? mi) // The Foo property and the Klass both could have unique or duplicate attributes // We collect them all, starting with the inner most first in the list. // Later on CopyValidAttributes will handle only copying the first valid one down + readonly Dictionary> parentAttributeCache = new (); List GetAllParentAttributes (MemberInfo context) { + if (parentAttributeCache.TryGetValue (context, out var cached)) + return cached; + var parentAvailability = new List (); + var current = context; while (true) { - parentAvailability.AddRange (AttributeManager.GetCustomAttributes (context)); - var parentContext = FindContainingContext (context); - if (context == parentContext) { - return parentAvailability; + parentAvailability.AddRange (AttributeManager.GetCustomAttributes (current)); + var parentContext = FindContainingContext (current); + if (current == parentContext) { + break; } - context = parentContext; + current = parentContext; } + parentAttributeCache [context] = parentAvailability; + return parentAvailability; } public bool PrintPlatformAttributes (MemberInfo? mi, Type? inlinedType = null) From 6552d26f0646ff3d15c15c57d9ab4e4a694f321b Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Tue, 23 Jun 2026 11:11:01 +0200 Subject: [PATCH 05/11] [bgen] Cache constructor types, ConstructorInfo, and Version objects in AttributeFactory - Cache Type[] arrays for attribute constructors to avoid repeated allocations - Cache ConstructorInfo lookups per (type, argTypes) to avoid repeated reflection - Cache Version objects in AvailabilityBaseAttribute to avoid creating new instances for the same version values Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../AttributeFactory.ConstructorArguments.cs | 17 +++++---- src/bgen/AttributeFactory.cs | 12 +++++-- src/bgen/Attributes.cs | 36 +++++++++++++++---- 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/src/bgen/AttributeFactory.ConstructorArguments.cs b/src/bgen/AttributeFactory.ConstructorArguments.cs index 3d149fe181b2..a207632b6d05 100644 --- a/src/bgen/AttributeFactory.ConstructorArguments.cs +++ b/src/bgen/AttributeFactory.ConstructorArguments.cs @@ -5,6 +5,10 @@ public static partial class AttributeFactory { public static readonly Type PlatformEnum = typeof (PlatformName); + static readonly Type [] ctorTypes_platform_message = new [] { PlatformEnum, typeof (string) }; + static readonly Type [] ctorTypes_platform_major_minor_message = new [] { PlatformEnum, typeof (int), typeof (int), typeof (string) }; + static readonly Type [] ctorTypes_platform_major_minor_build_message = new [] { PlatformEnum, typeof (int), typeof (int), typeof (int), typeof (string) }; + public readonly struct ConstructorArguments { readonly PlatformName platform; readonly int? major; @@ -52,13 +56,12 @@ public ConstructorArguments (PlatformName platformIn, string? messageIn) public Type [] GetCtorTypes () { - if (major is null || minor is null) { - return new [] { PlatformEnum, typeof (string) }; - } + if (major is null || minor is null) + return ctorTypes_platform_message; if (build is null) - return new [] { PlatformEnum, typeof (int), typeof (int), typeof (string) }; - return new [] { PlatformEnum, typeof (int), typeof (int), typeof (int), typeof (string) }; + return ctorTypes_platform_major_minor_message; + return ctorTypes_platform_major_minor_build_message; } public static bool TryGetCtorArguments (object [] constructorArguments, PlatformName platform, [NotNullWhen (true)] out object? []? ctorValues, [NotNullWhen (true)] out Type []? ctorTypes) @@ -71,7 +74,7 @@ public static bool TryGetCtorArguments (object [] constructorArguments, Platform if (constructorArguments [0] is byte && constructorArguments [1] is byte) { ctorValues = new object? [] { (byte) platform, (int) (byte) constructorArguments [0], (int) (byte) constructorArguments [1], null }; - ctorTypes = new [] { PlatformEnum, typeof (int), typeof (int), typeof (string) }; + ctorTypes = ctorTypes_platform_major_minor_message; return true; } @@ -81,7 +84,7 @@ public static bool TryGetCtorArguments (object [] constructorArguments, Platform constructorArguments [1] is byte && constructorArguments [2] is byte) { ctorValues = new object? [] { (byte) platform, (int) (byte) constructorArguments [0], (int) (byte) constructorArguments [1], (int) (byte) constructorArguments [2], null }; - ctorTypes = new [] { PlatformEnum, typeof (int), typeof (int), typeof (int), typeof (string) }; + ctorTypes = ctorTypes_platform_major_minor_build_message; return true; } return false; diff --git a/src/bgen/AttributeFactory.cs b/src/bgen/AttributeFactory.cs index 4c1e9e89b2d6..a433e61cfc03 100644 --- a/src/bgen/AttributeFactory.cs +++ b/src/bgen/AttributeFactory.cs @@ -1,17 +1,23 @@ using System.Collections.Generic; using System.Linq; +using System.Reflection; #nullable enable public static partial class AttributeFactory { + static readonly Dictionary<(Type, Type []), ConstructorInfo> constructorCache = new (); + public static T CreateNewAttribute (Type [] ctorTypes, object? [] ctorValues) where T : Attribute { var attribType = typeof (T); - var ctor = attribType.GetConstructor (ctorTypes); - if (ctor is null) - throw ErrorHelper.CreateError (1058, attribType.FullName); + if (!constructorCache.TryGetValue ((attribType, ctorTypes), out var ctor)) { + ctor = attribType.GetConstructor (ctorTypes); + if (ctor is null) + throw ErrorHelper.CreateError (1058, attribType.FullName); + constructorCache [(attribType, ctorTypes)] = ctor; + } return (T) ctor.Invoke (ctorValues); } diff --git a/src/bgen/Attributes.cs b/src/bgen/Attributes.cs index 8e47763419eb..3542dcc4e488 100644 --- a/src/bgen/Attributes.cs +++ b/src/bgen/Attributes.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.ComponentModel; using System.Reflection; using System.Text; @@ -894,6 +895,29 @@ public enum AvailabilityKind { AllowMultiple = true )] public abstract class AvailabilityBaseAttribute : Attribute { + + static readonly Dictionary<(int, int, int), Version> versionCache = new (); + + internal static Version GetCachedVersion (int major, int minor) + { + var key = (major, minor, -1); + if (!versionCache.TryGetValue (key, out var version)) { + version = new Version (major, minor); + versionCache [key] = version; + } + return version; + } + + internal static Version GetCachedVersion (int major, int minor, int build) + { + var key = (major, minor, build); + if (!versionCache.TryGetValue (key, out var version)) { + version = new Version (major, minor, build); + versionCache [key] = version; + } + return version; + } + /// The type of availability information this attribute contains. /// The type of availability information this attribute contains. /// @@ -1032,7 +1056,7 @@ public IntroducedAttribute (PlatformName platform, string? message = null) /// The minor version. /// Additional information related to the availability information. public IntroducedAttribute (PlatformName platform, int majorVersion, int minorVersion, string? message = null) - : base (AvailabilityKind.Introduced, platform, new Version (majorVersion, minorVersion), message) + : base (AvailabilityKind.Introduced, platform, GetCachedVersion (majorVersion, minorVersion), message) { } @@ -1043,7 +1067,7 @@ public IntroducedAttribute (PlatformName platform, int majorVersion, int minorVe /// The subminor version. /// Additional information related to the availability information. public IntroducedAttribute (PlatformName platform, int majorVersion, int minorVersion, int subminorVersion, string? message = null) - : base (AvailabilityKind.Introduced, platform, new Version (majorVersion, minorVersion, subminorVersion), message) + : base (AvailabilityKind.Introduced, platform, GetCachedVersion (majorVersion, minorVersion, subminorVersion), message) { } } @@ -1064,7 +1088,7 @@ public DeprecatedAttribute (PlatformName platform, string? message = null) /// The minor version. /// Additional information related to the availability information. public DeprecatedAttribute (PlatformName platform, int majorVersion, int minorVersion, string? message = null) - : base (AvailabilityKind.Deprecated, platform, new Version (majorVersion, minorVersion), message) + : base (AvailabilityKind.Deprecated, platform, GetCachedVersion (majorVersion, minorVersion), message) { } @@ -1075,7 +1099,7 @@ public DeprecatedAttribute (PlatformName platform, int majorVersion, int minorVe /// The subminor version. /// Additional information related to the availability information. public DeprecatedAttribute (PlatformName platform, int majorVersion, int minorVersion, int subminorVersion, string? message = null) - : base (AvailabilityKind.Deprecated, platform, new Version (majorVersion, minorVersion, subminorVersion), message) + : base (AvailabilityKind.Deprecated, platform, GetCachedVersion (majorVersion, minorVersion, subminorVersion), message) { } } @@ -1096,7 +1120,7 @@ public ObsoletedAttribute (PlatformName platform, string? message = null) /// The minor version. /// Additional information related to the availability information. public ObsoletedAttribute (PlatformName platform, int majorVersion, int minorVersion, string? message = null) - : base (AvailabilityKind.Obsoleted, platform, new Version (majorVersion, minorVersion), message) + : base (AvailabilityKind.Obsoleted, platform, GetCachedVersion (majorVersion, minorVersion), message) { } @@ -1107,7 +1131,7 @@ public ObsoletedAttribute (PlatformName platform, int majorVersion, int minorVer /// The subminor version. /// Additional information related to the availability information. public ObsoletedAttribute (PlatformName platform, int majorVersion, int minorVersion, int subminorVersion, string? message = null) - : base (AvailabilityKind.Obsoleted, platform, new Version (majorVersion, minorVersion, subminorVersion), message) + : base (AvailabilityKind.Obsoleted, platform, GetCachedVersion (majorVersion, minorVersion, subminorVersion), message) { } } From 2836ecee6ecc0a0f26bbd34e80d1b04ba2fc63bb Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Tue, 23 Jun 2026 11:11:20 +0200 Subject: [PATCH 06/11] [bgen] Cache getter/setter ExportAttribute results per PropertyInfo Cache the ExportAttribute lookups for property getters and setters to avoid repeated attribute queries on the same PropertyInfo. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/bgen/Generator.cs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/bgen/Generator.cs b/src/bgen/Generator.cs index 4299e209b1d4..b7450adcf612 100644 --- a/src/bgen/Generator.cs +++ b/src/bgen/Generator.cs @@ -1263,12 +1263,21 @@ public ExportAttribute GetOneExportAttribute (MemberInfo mo) return rv; } + readonly Dictionary getterExportCache = new (); + readonly Dictionary setterExportCache = new (); + public ExportAttribute? GetSetterExportAttribute (PropertyInfo pinfo) { + if (setterExportCache.TryGetValue (pinfo, out var cached)) + return cached; var ea = AttributeManager.GetCustomAttribute (pinfo.GetSetMethod ()); - if (ea is not null && ea.Selector is not null) + if (ea is not null && ea.Selector is not null) { + setterExportCache [pinfo] = ea; return ea; - return AttributeManager.GetCustomAttribute (pinfo)?.ToSetter (pinfo); + } + var result = AttributeManager.GetCustomAttribute (pinfo)?.ToSetter (pinfo); + setterExportCache [pinfo] = result; + return result; } public ExportAttribute GetOneSetterExportAttribute (PropertyInfo pinfo) @@ -1281,10 +1290,16 @@ public ExportAttribute GetOneSetterExportAttribute (PropertyInfo pinfo) public ExportAttribute? GetGetterExportAttribute (PropertyInfo pinfo) { + if (getterExportCache.TryGetValue (pinfo, out var cached)) + return cached; var ea = AttributeManager.GetCustomAttribute (pinfo.GetGetMethod ()); - if (ea is not null && ea.Selector is not null) + if (ea is not null && ea.Selector is not null) { + getterExportCache [pinfo] = ea; return ea; - return AttributeManager.GetCustomAttribute (pinfo)?.ToGetter (pinfo); + } + var result = AttributeManager.GetCustomAttribute (pinfo)?.ToGetter (pinfo); + getterExportCache [pinfo] = result; + return result; } public ExportAttribute GetOneGetterExportAttribute (PropertyInfo pinfo) From f9fa2c0030ca6cad30b024cda2c4ba28afc8876d Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Tue, 23 Jun 2026 11:36:03 +0200 Subject: [PATCH 07/11] Remove Version caching from AvailabilityBaseAttribute Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/bgen/Attributes.cs | 36 ++++++------------------------------ 1 file changed, 6 insertions(+), 30 deletions(-) diff --git a/src/bgen/Attributes.cs b/src/bgen/Attributes.cs index 3542dcc4e488..8e47763419eb 100644 --- a/src/bgen/Attributes.cs +++ b/src/bgen/Attributes.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.ComponentModel; using System.Reflection; using System.Text; @@ -895,29 +894,6 @@ public enum AvailabilityKind { AllowMultiple = true )] public abstract class AvailabilityBaseAttribute : Attribute { - - static readonly Dictionary<(int, int, int), Version> versionCache = new (); - - internal static Version GetCachedVersion (int major, int minor) - { - var key = (major, minor, -1); - if (!versionCache.TryGetValue (key, out var version)) { - version = new Version (major, minor); - versionCache [key] = version; - } - return version; - } - - internal static Version GetCachedVersion (int major, int minor, int build) - { - var key = (major, minor, build); - if (!versionCache.TryGetValue (key, out var version)) { - version = new Version (major, minor, build); - versionCache [key] = version; - } - return version; - } - /// The type of availability information this attribute contains. /// The type of availability information this attribute contains. /// @@ -1056,7 +1032,7 @@ public IntroducedAttribute (PlatformName platform, string? message = null) /// The minor version. /// Additional information related to the availability information. public IntroducedAttribute (PlatformName platform, int majorVersion, int minorVersion, string? message = null) - : base (AvailabilityKind.Introduced, platform, GetCachedVersion (majorVersion, minorVersion), message) + : base (AvailabilityKind.Introduced, platform, new Version (majorVersion, minorVersion), message) { } @@ -1067,7 +1043,7 @@ public IntroducedAttribute (PlatformName platform, int majorVersion, int minorVe /// The subminor version. /// Additional information related to the availability information. public IntroducedAttribute (PlatformName platform, int majorVersion, int minorVersion, int subminorVersion, string? message = null) - : base (AvailabilityKind.Introduced, platform, GetCachedVersion (majorVersion, minorVersion, subminorVersion), message) + : base (AvailabilityKind.Introduced, platform, new Version (majorVersion, minorVersion, subminorVersion), message) { } } @@ -1088,7 +1064,7 @@ public DeprecatedAttribute (PlatformName platform, string? message = null) /// The minor version. /// Additional information related to the availability information. public DeprecatedAttribute (PlatformName platform, int majorVersion, int minorVersion, string? message = null) - : base (AvailabilityKind.Deprecated, platform, GetCachedVersion (majorVersion, minorVersion), message) + : base (AvailabilityKind.Deprecated, platform, new Version (majorVersion, minorVersion), message) { } @@ -1099,7 +1075,7 @@ public DeprecatedAttribute (PlatformName platform, int majorVersion, int minorVe /// The subminor version. /// Additional information related to the availability information. public DeprecatedAttribute (PlatformName platform, int majorVersion, int minorVersion, int subminorVersion, string? message = null) - : base (AvailabilityKind.Deprecated, platform, GetCachedVersion (majorVersion, minorVersion, subminorVersion), message) + : base (AvailabilityKind.Deprecated, platform, new Version (majorVersion, minorVersion, subminorVersion), message) { } } @@ -1120,7 +1096,7 @@ public ObsoletedAttribute (PlatformName platform, string? message = null) /// The minor version. /// Additional information related to the availability information. public ObsoletedAttribute (PlatformName platform, int majorVersion, int minorVersion, string? message = null) - : base (AvailabilityKind.Obsoleted, platform, GetCachedVersion (majorVersion, minorVersion), message) + : base (AvailabilityKind.Obsoleted, platform, new Version (majorVersion, minorVersion), message) { } @@ -1131,7 +1107,7 @@ public ObsoletedAttribute (PlatformName platform, int majorVersion, int minorVer /// The subminor version. /// Additional information related to the availability information. public ObsoletedAttribute (PlatformName platform, int majorVersion, int minorVersion, int subminorVersion, string? message = null) - : base (AvailabilityKind.Obsoleted, platform, GetCachedVersion (majorVersion, minorVersion, subminorVersion), message) + : base (AvailabilityKind.Obsoleted, platform, new Version (majorVersion, minorVersion, subminorVersion), message) { } } From 0fc6a52170c141cd9607a4895fef63a2b09db93e Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Tue, 23 Jun 2026 16:15:52 +0200 Subject: [PATCH 08/11] Move constructorCache from static to instance variable Move the constructorCache dictionary from the static AttributeFactory class to the instance-based AttributeManager class. This eliminates the static mutable state that could be problematic when tests run in parallel. The cache is passed through to AttributeFactory and AttributeConversionManager methods as a parameter. Convenience overloads without the parameter are kept for test compatibility (they use a static fallback cache). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/bgen/AttributeConversionManager.cs | 14 ++++---- src/bgen/AttributeFactory.cs | 45 +++++++++++++++++++++----- src/bgen/AttributeManager.cs | 43 ++++++++++++------------ 3 files changed, 67 insertions(+), 35 deletions(-) diff --git a/src/bgen/AttributeConversionManager.cs b/src/bgen/AttributeConversionManager.cs index 1e2cf6ec5a6c..c56824ff88fc 100644 --- a/src/bgen/AttributeConversionManager.cs +++ b/src/bgen/AttributeConversionManager.cs @@ -5,7 +5,7 @@ #nullable enable public static class AttributeConversionManager { - public static Attribute ConvertPlatformAttribute (CustomAttributeData attribute, PlatformName platform) + public static Attribute ConvertPlatformAttribute (AttributeManager attributeManager, CustomAttributeData attribute, PlatformName platform) { var constructorArguments = new object [attribute.ConstructorArguments.Count]; for (int i = 0; i < attribute.ConstructorArguments.Count; ++i) @@ -23,7 +23,7 @@ public static Attribute ConvertPlatformAttribute (CustomAttributeData attribute, if (AttributeFactory.ConstructorArguments.TryGetCtorArguments ( constructorArguments, platform, out var ctorValues, out var ctorTypes)) { - return AttributeFactory.CreateNewAttribute (ctorTypes!, ctorValues!); + return AttributeFactory.CreateNewAttribute (attributeManager, ctorTypes!, ctorValues!); } throw new NotImplementedException (unknownFormatError ()); @@ -99,7 +99,7 @@ static ParsedAvailabilityInfo DetermineOldAvailabilityVersion (CustomAttributeNa throw new NotImplementedException ($"Unknown version format \"{enumName}\" in DetermineOldAvailabilityVersion"); } - public static IEnumerable ConvertAvailability (CustomAttributeData attribute) + public static IEnumerable ConvertAvailability (AttributeManager attributeManager, CustomAttributeData attribute) { string? message = null; if (attribute.NamedArguments is null) @@ -112,22 +112,22 @@ public static IEnumerable ConvertAvailability (CustomAttributeData at switch (arg.MemberName) { case "Introduced": { ParsedAvailabilityInfo availInfo = DetermineOldAvailabilityVersion (arg); - yield return AttributeFactory.CreateNewAttribute (availInfo.Platform, availInfo.Major, availInfo.Minor, message: message); + yield return AttributeFactory.CreateNewAttribute (attributeManager, availInfo.Platform, availInfo.Major, availInfo.Minor, message: message); continue; } case "Deprecated": { ParsedAvailabilityInfo availInfo = DetermineOldAvailabilityVersion (arg); - yield return AttributeFactory.CreateNewAttribute (availInfo.Platform, availInfo.Major, availInfo.Minor, message: message); + yield return AttributeFactory.CreateNewAttribute (attributeManager, availInfo.Platform, availInfo.Major, availInfo.Minor, message: message); continue; } case "Obsoleted": { ParsedAvailabilityInfo availInfo = DetermineOldAvailabilityVersion (arg); - yield return AttributeFactory.CreateNewAttribute (availInfo.Platform, availInfo.Major, availInfo.Minor, message: message); + yield return AttributeFactory.CreateNewAttribute (attributeManager, availInfo.Platform, availInfo.Major, availInfo.Minor, message: message); continue; } case "Unavailable": { ParsedAvailabilityInfo availInfo = DetermineOldAvailabilityVersion (arg); - yield return AttributeFactory.CreateNewAttribute (availInfo.Platform, message: message); + yield return AttributeFactory.CreateNewAttribute (attributeManager, availInfo.Platform, message: message); continue; } case "Message": diff --git a/src/bgen/AttributeFactory.cs b/src/bgen/AttributeFactory.cs index a433e61cfc03..e03eca9e3bd2 100644 --- a/src/bgen/AttributeFactory.cs +++ b/src/bgen/AttributeFactory.cs @@ -6,11 +6,10 @@ public static partial class AttributeFactory { - static readonly Dictionary<(Type, Type []), ConstructorInfo> constructorCache = new (); - - public static T CreateNewAttribute (Type [] ctorTypes, object? [] ctorValues) + public static T CreateNewAttribute (AttributeManager attributeManager, Type [] ctorTypes, object? [] ctorValues) where T : Attribute { + var constructorCache = attributeManager.constructorCache; var attribType = typeof (T); if (!constructorCache.TryGetValue ((attribType, ctorTypes), out var ctor)) { ctor = attribType.GetConstructor (ctorTypes); @@ -22,26 +21,56 @@ public static T CreateNewAttribute (Type [] ctorTypes, object? [] ctorValues) return (T) ctor.Invoke (ctorValues); } - public static T CreateNewAttribute (PlatformName platform, int major, int minor, string? message = null) + public static T CreateNewAttribute (AttributeManager attributeManager, PlatformName platform, int major, int minor, string? message = null) where T : Attribute { var args = new ConstructorArguments (platform, major, minor, message); - return CreateNewAttribute (args.GetCtorTypes (), args.GetCtorValues ()); + return CreateNewAttribute (attributeManager, args.GetCtorTypes (), args.GetCtorValues ()); } - public static T CreateNewAttribute (PlatformName platform, int major, int minor, int build, string? message = null) + public static T CreateNewAttribute (AttributeManager attributeManager, PlatformName platform, int major, int minor, int build, string? message = null) where T : Attribute { var args = new ConstructorArguments (platform, major, minor, build, message); - return CreateNewAttribute (args.GetCtorTypes (), args.GetCtorValues ()); + return CreateNewAttribute (attributeManager, args.GetCtorTypes (), args.GetCtorValues ()); + } + + public static T CreateNewAttribute (AttributeManager attributeManager, PlatformName platform, string? message = null) where T : Attribute + { + var args = new ConstructorArguments (platform, message); + return CreateNewAttribute (attributeManager, args.GetCtorTypes (), args.GetCtorValues ()); + } + + // Convenience overloads for callers that don't have an AttributeManager instance (e.g. tests). + public static T CreateNewAttribute (PlatformName platform, int major, int minor, string? message = null) + where T : Attribute + { + var args = new ConstructorArguments (platform, major, minor, message); + return CreateNewAttribute (defaultConstructorCache, args.GetCtorTypes (), args.GetCtorValues ()); } public static T CreateNewAttribute (PlatformName platform, string? message = null) where T : Attribute { var args = new ConstructorArguments (platform, message); - return CreateNewAttribute (args.GetCtorTypes (), args.GetCtorValues ()); + return CreateNewAttribute (defaultConstructorCache, args.GetCtorTypes (), args.GetCtorValues ()); + } + + static T CreateNewAttribute (Dictionary<(Type, Type []), ConstructorInfo> constructorCache, Type [] ctorTypes, object? [] ctorValues) + where T : Attribute + { + var attribType = typeof (T); + if (!constructorCache.TryGetValue ((attribType, ctorTypes), out var ctor)) { + ctor = attribType.GetConstructor (ctorTypes); + if (ctor is null) + throw ErrorHelper.CreateError (1058, attribType.FullName); + constructorCache [(attribType, ctorTypes)] = ctor; + } + + return (T) ctor.Invoke (ctorValues); } + static readonly Dictionary<(Type, Type []), ConstructorInfo> defaultConstructorCache = new (); + static readonly IntroducedAttribute [] noVersionSupportedCache = new IntroducedAttribute [] { new (PlatformName.iOS), new (PlatformName.TvOS), diff --git a/src/bgen/AttributeManager.cs b/src/bgen/AttributeManager.cs index b579a51b37d1..b0a94906eb6a 100644 --- a/src/bgen/AttributeManager.cs +++ b/src/bgen/AttributeManager.cs @@ -28,6 +28,9 @@ public class AttributeManager { // Cache HasAttribute results to avoid repeated GetCustomAttributesData() calls. readonly Dictionary<(ICustomAttributeProvider, System.Type), bool> hasAttributeCache = new (); + // Cache ConstructorInfo lookups per (attribute type, constructor arg types) to avoid repeated reflection. + internal readonly Dictionary<(Type, Type []), ConstructorInfo> constructorCache = new (); + TypeCache TypeCache { get; } public AttributeManager (TypeCache typeCache) @@ -282,7 +285,7 @@ Type ConvertTypeToMeta (System.Type type) return rv; } - static IEnumerable ConvertOldAttributes (CustomAttributeData attribute) + IEnumerable ConvertOldAttributes (CustomAttributeData attribute) { switch (attribute.GetAttributeType ().Namespace) { case null: // Root namespace such as PlatformAvailabilityShadow.cs @@ -297,33 +300,33 @@ Type ConvertTypeToMeta (System.Type type) switch (attribute.GetAttributeType ().Name) { case "SinceAttribute": case "iOSAttribute": - return AttributeConversionManager.ConvertPlatformAttribute (attribute, PlatformName.iOS).Yield (); + return AttributeConversionManager.ConvertPlatformAttribute (this, attribute, PlatformName.iOS).Yield (); case "MacAttribute": - return AttributeConversionManager.ConvertPlatformAttribute (attribute, PlatformName.MacOSX).Yield (); + return AttributeConversionManager.ConvertPlatformAttribute (this, attribute, PlatformName.MacOSX).Yield (); case "WatchAttribute": - return AttributeConversionManager.ConvertPlatformAttribute (attribute, PlatformName.WatchOS).Yield (); + return AttributeConversionManager.ConvertPlatformAttribute (this, attribute, PlatformName.WatchOS).Yield (); case "TVAttribute": - return AttributeConversionManager.ConvertPlatformAttribute (attribute, PlatformName.TvOS).Yield (); + return AttributeConversionManager.ConvertPlatformAttribute (this, attribute, PlatformName.TvOS).Yield (); case "MacCatalystAttribute": - return AttributeConversionManager.ConvertPlatformAttribute (attribute, PlatformName.MacCatalyst).Yield (); + return AttributeConversionManager.ConvertPlatformAttribute (this, attribute, PlatformName.MacCatalyst).Yield (); case "LionAttribute": - return AttributeFactory.CreateNewAttribute (PlatformName.MacOSX, 10, 7).Yield (); + return AttributeFactory.CreateNewAttribute (this, PlatformName.MacOSX, 10, 7).Yield (); case "MountainLionAttribute": - return AttributeFactory.CreateNewAttribute (PlatformName.MacOSX, 10, 8).Yield (); + return AttributeFactory.CreateNewAttribute (this, PlatformName.MacOSX, 10, 8).Yield (); case "MavericksAttribute": - return AttributeFactory.CreateNewAttribute (PlatformName.MacOSX, 10, 9).Yield (); + return AttributeFactory.CreateNewAttribute (this, PlatformName.MacOSX, 10, 9).Yield (); case "NoMacAttribute": - return AttributeFactory.CreateNewAttribute (PlatformName.MacOSX).Yield (); + return AttributeFactory.CreateNewAttribute (this, PlatformName.MacOSX).Yield (); case "NoiOSAttribute": - return AttributeFactory.CreateNewAttribute (PlatformName.iOS).Yield (); + return AttributeFactory.CreateNewAttribute (this, PlatformName.iOS).Yield (); case "NoWatchAttribute": - return AttributeFactory.CreateNewAttribute (PlatformName.WatchOS).Yield (); + return AttributeFactory.CreateNewAttribute (this, PlatformName.WatchOS).Yield (); case "NoTVAttribute": - return AttributeFactory.CreateNewAttribute (PlatformName.TvOS).Yield (); + return AttributeFactory.CreateNewAttribute (this, PlatformName.TvOS).Yield (); case "NoMacCatalystAttribute": - return AttributeFactory.CreateNewAttribute (PlatformName.MacCatalyst).Yield (); + return AttributeFactory.CreateNewAttribute (this, PlatformName.MacCatalyst).Yield (); case "AvailabilityAttribute": - return AttributeConversionManager.ConvertAvailability (attribute); + return AttributeConversionManager.ConvertAvailability (this, attribute); case "ExperimentalAttribute": var earg = attribute.ConstructorArguments [0].Value as string ?? ""; return new System.Diagnostics.CodeAnalysis.ExperimentalAttribute (earg).Yield (); @@ -331,15 +334,15 @@ Type ConvertTypeToMeta (System.Type type) var sarg = attribute.ConstructorArguments [0].Value as string ?? ""; (var sp, var sv) = ParseOSPlatformAttribute (sarg); if (sv is null) - return AttributeFactory.CreateNewAttribute (sp).Yield (); + return AttributeFactory.CreateNewAttribute (this, sp).Yield (); else - return AttributeFactory.CreateNewAttribute (sp, sv.Major, sv.Minor).Yield (); + return AttributeFactory.CreateNewAttribute (this, sp, sv.Major, sv.Minor).Yield (); case "UnsupportedOSPlatformAttribute": var uarg = attribute.ConstructorArguments [0].Value as string ?? ""; (var up, var uv) = ParseOSPlatformAttribute (uarg); // might have been available for a while... if (uv is null) - return AttributeFactory.CreateNewAttribute (up).Yield (); + return AttributeFactory.CreateNewAttribute (this, up).Yield (); else return Enumerable.Empty (); case "ObsoletedOSPlatformAttribute": @@ -347,9 +350,9 @@ Type ConvertTypeToMeta (System.Type type) (var op, var ov) = ParseOSPlatformAttribute (oarg); // might have been available for a while... if (ov is null) - return AttributeFactory.CreateNewAttribute (op).Yield (); + return AttributeFactory.CreateNewAttribute (this, op).Yield (); else - return AttributeFactory.CreateNewAttribute (op, ov.Major, ov.Minor).Yield (); + return AttributeFactory.CreateNewAttribute (this, op, ov.Major, ov.Minor).Yield (); default: return Enumerable.Empty (); } From f1dfcf91f9f34c52d3007c3b181af4a40c780fea Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Tue, 23 Jun 2026 19:49:24 +0200 Subject: [PATCH 09/11] Remove obsoleted convenience overloads and their static fallback cache Remove the two convenience CreateNewAttribute overloads (that didn't take an AttributeManager parameter) and the static defaultConstructorCache they used. Remove the corresponding tests that called those overloads. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/bgen/AttributeFactory.cs | 30 -------------------- tests/bgen/AttributeFactoryTests.cs | 44 ----------------------------- 2 files changed, 74 deletions(-) diff --git a/src/bgen/AttributeFactory.cs b/src/bgen/AttributeFactory.cs index e03eca9e3bd2..ade99ce6022a 100644 --- a/src/bgen/AttributeFactory.cs +++ b/src/bgen/AttributeFactory.cs @@ -41,36 +41,6 @@ public static T CreateNewAttribute (AttributeManager attributeManager, Platfo return CreateNewAttribute (attributeManager, args.GetCtorTypes (), args.GetCtorValues ()); } - // Convenience overloads for callers that don't have an AttributeManager instance (e.g. tests). - public static T CreateNewAttribute (PlatformName platform, int major, int minor, string? message = null) - where T : Attribute - { - var args = new ConstructorArguments (platform, major, minor, message); - return CreateNewAttribute (defaultConstructorCache, args.GetCtorTypes (), args.GetCtorValues ()); - } - - public static T CreateNewAttribute (PlatformName platform, string? message = null) where T : Attribute - { - var args = new ConstructorArguments (platform, message); - return CreateNewAttribute (defaultConstructorCache, args.GetCtorTypes (), args.GetCtorValues ()); - } - - static T CreateNewAttribute (Dictionary<(Type, Type []), ConstructorInfo> constructorCache, Type [] ctorTypes, object? [] ctorValues) - where T : Attribute - { - var attribType = typeof (T); - if (!constructorCache.TryGetValue ((attribType, ctorTypes), out var ctor)) { - ctor = attribType.GetConstructor (ctorTypes); - if (ctor is null) - throw ErrorHelper.CreateError (1058, attribType.FullName); - constructorCache [(attribType, ctorTypes)] = ctor; - } - - return (T) ctor.Invoke (ctorValues); - } - - static readonly Dictionary<(Type, Type []), ConstructorInfo> defaultConstructorCache = new (); - static readonly IntroducedAttribute [] noVersionSupportedCache = new IntroducedAttribute [] { new (PlatformName.iOS), new (PlatformName.TvOS), diff --git a/tests/bgen/AttributeFactoryTests.cs b/tests/bgen/AttributeFactoryTests.cs index ebc3b2a4ff06..f263347237e9 100644 --- a/tests/bgen/AttributeFactoryTests.cs +++ b/tests/bgen/AttributeFactoryTests.cs @@ -8,50 +8,6 @@ namespace GeneratorTests { [TestFixture] [Parallelizable (ParallelScope.All)] public class AttributeFactoryTests { - static void AssertAttributeCreation (Func callback, PlatformName platform, - int major, int minor, string? message = null) where T : AvailabilityBaseAttribute - { - var typeName = typeof (T).Name; - var attr = callback (platform, major, minor, message) as T; - Assert.That (attr, Is.Not.Null, $"{typeName} attribute type"); - Assert.That (attr.Platform, Is.EqualTo (platform), $"{typeName} Platform"); - Assert.That (attr.Version!.Major, Is.EqualTo (major), $"{typeName} Major"); - Assert.That (attr.Version!.Minor, Is.EqualTo (minor), $"{typeName} Minor"); - Assert.That (attr.Message, Is.EqualTo (message)); - } - - static void AssertAttributeCreationNotVersion (Func callback, PlatformName platform, - string? message = null) where T : AvailabilityBaseAttribute - { - var typeName = typeof (T).Name; - var attr = callback (platform, message) as T; - Assert.That (attr, Is.Not.Null, $"{typeName} attribute type"); - Assert.That (attr.Platform, Is.EqualTo (platform), $"{typeName} Platform"); - Assert.That (attr.Message, Is.EqualTo (message)); - } - - - // simple tests, but we want to test it - [TestCase (PlatformName.iOS, 13, 4, "message")] - [TestCase (PlatformName.iOS, 12, 4, null)] - public void CreateAttributeTest (PlatformName platform, int major, int minor, string? message) - { - // call several times with diff types - AssertAttributeCreation (AttributeFactory.CreateNewAttribute, platform, major, minor, message); - AssertAttributeCreation (AttributeFactory.CreateNewAttribute, platform, major, minor, message); - AssertAttributeCreation (AttributeFactory.CreateNewAttribute, platform, major, minor, message); - } - - [TestCase (PlatformName.iOS, "message")] - [TestCase (PlatformName.iOS, null)] - public void CreateAttributeNoVersionTest (PlatformName platform, string? message) - { - // call several times with diff types - AssertAttributeCreationNotVersion (AttributeFactory.CreateNewAttribute, platform, message); - AssertAttributeCreationNotVersion (AttributeFactory.CreateNewAttribute, platform, message); - AssertAttributeCreationNotVersion (AttributeFactory.CreateNewAttribute, platform, message); - AssertAttributeCreationNotVersion (AttributeFactory.CreateNewAttribute, platform, message); - } [TestCase (PlatformName.iOS)] [TestCase (PlatformName.MacCatalyst)] From d7cebc49de88cd25b58da321d1e2e910f5d4b898 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Wed, 24 Jun 2026 16:53:05 +0200 Subject: [PATCH 10/11] [bgen] Don't mutate AvailabilityBaseAttribute.Version in GenerateSupported Instead of setting Version = null when it's <= the platform minimum (which mutates a potentially cached attribute instance), skip writing the version in GeneratePlatformNameAndVersion without modifying the attribute. This makes it safe to cache and reuse attribute instances. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/bgen/Attributes.cs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/bgen/Attributes.cs b/src/bgen/Attributes.cs index 8e47763419eb..06dd32bb3e9c 100644 --- a/src/bgen/Attributes.cs +++ b/src/bgen/Attributes.cs @@ -955,19 +955,12 @@ void GenerateDeprecated (StringBuilder builder) void GenerateSupported (StringBuilder builder) { -#if BGENERATOR - // If the version is less than or equal to the min version for the platform in question, - // the version is redundant, so just skip it. - if (Version is not null && Version <= Xamarin.SdkVersions.GetMinVersion (Platform.AsApplePlatform ())) - Version = null; -#endif - builder.Append ("[SupportedOSPlatform (\""); - GeneratePlatformNameAndVersion (builder); + GeneratePlatformNameAndVersion (builder, skipMinVersion: true); builder.AppendLine ("\")]"); } - void GeneratePlatformNameAndVersion (StringBuilder builder) + void GeneratePlatformNameAndVersion (StringBuilder builder, bool skipMinVersion = false) { switch (Platform) { case PlatformName.iOS: @@ -989,8 +982,17 @@ void GeneratePlatformNameAndVersion (StringBuilder builder) throw new NotSupportedException ($"Unknown platform: {Platform}"); } - if (Version is not null) - builder.Append (Version.ToString (Version.Build >= 0 ? 3 : 2)); + if (Version is null) + return; + +#if BGENERATOR + // If the version is less than or equal to the min version for the platform in question, + // the version is redundant, so just skip it. + if (skipMinVersion && Version <= Xamarin.SdkVersions.GetMinVersion (Platform.AsApplePlatform ())) + return; +#endif + + builder.Append (Version.ToString (Version.Build >= 0 ? 3 : 2)); } /// Returns a human readable version of the availability attribute. From 48dd6663dd02fd0ca73f32ec9bb21bf975d094e5 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Thu, 25 Jun 2026 08:44:01 +0200 Subject: [PATCH 11/11] [bgen] Don't create availability attributes with below-minimum versions When cloning an attribute to another platform, if the version is at or below the target platform's minimum, create a no-version attribute instead of clamping to the minimum version. For Introduced attributes this means no version at all (since GenerateSupported would have stripped it anyway). For Deprecated/Obsoleted, use the minimum version. This avoids creating attributes whose version would later need to be stripped, and together with the earlier change to not mutate Version in GenerateSupported, makes attribute caching fully safe. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/bgen/AttributeFactory.cs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/bgen/AttributeFactory.cs b/src/bgen/AttributeFactory.cs index ade99ce6022a..5ddf43bbc47f 100644 --- a/src/bgen/AttributeFactory.cs +++ b/src/bgen/AttributeFactory.cs @@ -123,8 +123,26 @@ public static AvailabilityBaseAttribute CloneFromOtherPlatform (AvailabilityBase // So determine if the build is -1, and use the 2 or 3 param ctor... var version = attr.Version; var minimum = Xamarin.SdkVersions.GetMinVersion (platform.AsApplePlatform ()); - if (version < minimum) - version = minimum; + if (version <= minimum) { + // If the version is at or below the platform minimum, it's redundant, + // so create a no-version attribute instead of clamping. + switch (attr.AvailabilityKind) { + case AvailabilityKind.Introduced: + return string.IsNullOrEmpty (attr.Message) + ? CreateNoVersionSupportedAttribute (platform) + : new IntroducedAttribute (platform, message: attr.Message); + case AvailabilityKind.Deprecated: + return new DeprecatedAttribute (platform, minimum.Major, minimum.Minor, message: attr.Message); + case AvailabilityKind.Obsoleted: + return new ObsoletedAttribute (platform, minimum.Major, minimum.Minor, message: attr.Message); + case AvailabilityKind.Unavailable: + return string.IsNullOrEmpty (attr.Message) + ? CreateUnsupportedAttribute (platform) + : new UnavailableAttribute (platform, message: attr.Message); + default: + throw new NotImplementedException (); + } + } if (version.Build == -1) { switch (attr.AvailabilityKind) {