From 81c9d831f496702d2536b7fb27c04b96a9f3a24f Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 8 Sep 2021 13:26:07 +0900 Subject: [PATCH] Cache reflection portion of `SettingSource` lookups Fixes the mod-related overhead portion of https://github.com/ppy/osu/issues/14638. There's still a significant performance issue that will be addressed separately. --- .../Configuration/SettingSourceAttribute.cs | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/osu.Game/Configuration/SettingSourceAttribute.cs b/osu.Game/Configuration/SettingSourceAttribute.cs index f373e59417..4fff81b587 100644 --- a/osu.Game/Configuration/SettingSourceAttribute.cs +++ b/osu.Game/Configuration/SettingSourceAttribute.cs @@ -167,18 +167,7 @@ namespace osu.Game.Configuration } } - public static IEnumerable<(SettingSourceAttribute, PropertyInfo)> GetSettingsSourceProperties(this object obj) - { - foreach (var property in obj.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance)) - { - var attr = property.GetCustomAttribute(true); - - if (attr == null) - continue; - - yield return (attr, property); - } - } + public static IEnumerable<(SettingSourceAttribute, PropertyInfo)> GetSettingsSourceProperties(this T obj) => SettingSourceCache.SettingSourceProperties; public static ICollection<(SettingSourceAttribute, PropertyInfo)> GetOrderedSettingsSourceProperties(this object obj) => obj.GetSettingsSourceProperties() @@ -196,5 +185,14 @@ namespace osu.Game.Configuration protected override DropdownMenu CreateMenu() => base.CreateMenu().With(m => m.MaxHeight = 100); } } + + private static class SettingSourceCache + { + public static (SettingSourceAttribute, PropertyInfo)[] SettingSourceProperties { get; } = + typeof(T).GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance) + .Select(property => (property.GetCustomAttribute(), property)) + .Where(pair => pair.Item1 != null) + .ToArray(); + } } }