Skip to content

Eliminate per-frame enum reflection/boxing in combo and property-grid enum rows - #397

Merged
matt-edmondson merged 2 commits into
mainfrom
copilot/fix-enum-combo-allocations
Sep 13, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
copilot/fix-enum-combo-allocations

Conversation

Copilot AI commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Enum-backed combo paths were doing Enum.GetNames/Enum.GetValues in render-time code, causing repeated allocations every frame; Combo<TEnum> also used a non-generic Array path that introduced avoidable boxing. This change moves enum metadata to per-closed-type static caches and rewires affected call sites without changing public APIs or enum-selection behavior.

  • Render-path allocation removal

    • Added internal EnumCache<TEnum> with static Values and Names, initialized once per enum type.
    • Replaced per-frame enum reflection calls in:
      • ImGuiWidgets.Combo<TEnum>
      • PropertyGrid.Enum<TEnum>
      • EnumCombo name enumeration path
  • Boxing-free enum selection in Combo<TEnum>

    • Switched from non-generic Array/GetValue usage to typed array indexing.
    • Preserves existing selection semantics while removing the boxed GetValue conversion path.
  • Behavior preserved for out-of-range enum values

    • Kept PropertyGrid.Enum<TEnum> guard (index >= 0 && index < values.Length) so cast/out-of-set values (including flags combinations) still render as “no selection” rather than faulting.
  • Focused cache behavior coverage

    • Added tests asserting per-type cache reuse (same instance returned on repeated reads) for enum names/values paths.
internal static class EnumCache<TEnum> where TEnum : Enum
{
	internal static readonly TEnum[] Values = (TEnum[])Enum.GetValues(typeof(TEnum));
	internal static readonly string[] Names = Enum.GetNames(typeof(TEnum));
}

// Hot path now reads cached arrays:
TEnum[] values = EnumCache<TEnum>.Values;
string[] names = EnumCache<TEnum>.Names;
int index = Array.IndexOf(values, selectedValue);

Co-authored-by: matt-edmondson <19528727+matt-edmondson@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix enum combo rows to reduce allocations on every frame Eliminate per-frame enum reflection/boxing in combo and property-grid enum rows Sep 13, 2026
@matt-edmondson
matt-edmondson marked this pull request as ready for review September 13, 2026 12:17
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enum combo rows reflect and allocate on every frame

2 participants