Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache reflection portion of SettingSource lookups #14674

Merged
merged 6 commits into from Sep 9, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 10 additions & 12 deletions osu.Game/Configuration/SettingSourceAttribute.cs
Expand Up @@ -167,18 +167,7 @@ public static IEnumerable<Drawable> CreateSettingsControls(this object obj)
}
}

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<SettingSourceAttribute>(true);

if (attr == null)
continue;

yield return (attr, property);
}
}
public static IEnumerable<(SettingSourceAttribute, PropertyInfo)> GetSettingsSourceProperties<T>(this T obj) => SettingSourceCache<T>.SettingSourceProperties;

public static ICollection<(SettingSourceAttribute, PropertyInfo)> GetOrderedSettingsSourceProperties(this object obj)
=> obj.GetSettingsSourceProperties()
Expand All @@ -196,5 +185,14 @@ private class ModDropdownControl : DropdownControl
protected override DropdownMenu CreateMenu() => base.CreateMenu().With(m => m.MaxHeight = 100);
}
}

private static class SettingSourceCache<T>
{
public static (SettingSourceAttribute, PropertyInfo)[] SettingSourceProperties { get; } =
typeof(T).GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance)
.Select(property => (property.GetCustomAttribute<SettingSourceAttribute>(), property))
.Where(pair => pair.Item1 != null)
.ToArray();
}
}
}