[AI Task] [Tizen.Multimedia.Remoting] Cache Enum.GetValues for MediaController capability enums#7713
[AI Task] [Tizen.Multimedia.Remoting] Cache Enum.GetValues for MediaController capability enums#7713JoonghyunCho wants to merge 2 commits into
Conversation
…capability enums Replace per-call Enum.GetValues(typeof(T)), which returns a non-generic Array (boxing each enum value and allocating a new array on every call), with static readonly caches populated once via the generic Enum.GetValues<T>() overload. Applied to the four capability/display-mode conversion sites in MediaController. - s_allPlaybackActions shared by GetPlaybackCapabilities and the PlaybackCapabilityUpdated event handler (a per-server-change hot path). - s_allDisplayModes / s_allDisplayRotations for the DisplayMode and DisplayRotation ToPublicList helpers. Enumeration order and results are unchanged; public API is unaffected. Fixes #7642 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request optimizes performance by caching enum values in static read-only arrays instead of repeatedly calling Enum.GetValues. However, using Enum.GetValues<T>() on the [Flags] enums MediaControlNativeDisplayMode and MediaControlNativeDisplayRotation introduces a correctness bug. Because these enums contain an All member, it evaluates to true in HasFlag checks and results in duplicate entries in the returned lists. It is recommended to explicitly define the arrays of individual flags to avoid this issue.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| private static readonly MediaControlNativeDisplayMode[] s_allDisplayModes | ||
| = Enum.GetValues<MediaControlNativeDisplayMode>(); | ||
|
|
||
| private static readonly MediaControlNativeDisplayRotation[] s_allDisplayRotations | ||
| = Enum.GetValues<MediaControlNativeDisplayRotation>(); |
There was a problem hiding this comment.
Using Enum.GetValues<T>() on MediaControlNativeDisplayMode and MediaControlNativeDisplayRotation introduces a correctness bug because these are [Flags] enums that define an All member (which is a bitwise combination of other members).
The Bug
Enum.GetValues<T>()returns all defined constants, includingAll.- In
ToPublicList(), the loop checksmodes.HasFlag(mode). Ifmodeshas all flags set,modes.HasFlag(All)evaluates totrue. ToPublic()is then called withAll. SinceAllis not handled in theswitchstatement ofToPublic(), it falls back to the default value (MediaControlDisplayMode.LetterBoxorRotation.Rotate0), resulting in duplicate entries in the returned list (e.g.,LetterBoxorRotate0added twice).
Solution
Explicitly define the arrays of individual flags. This completely resolves the duplicate element bug and also improves performance by avoiding reflection (Enum.GetValues) at startup.
private static readonly MediaControlNativeDisplayMode[] s_allDisplayModes = new[]
{
MediaControlNativeDisplayMode.LetterBox,
MediaControlNativeDisplayMode.OriginSize,
MediaControlNativeDisplayMode.FullScreen,
MediaControlNativeDisplayMode.CroppedFull
};
private static readonly MediaControlNativeDisplayRotation[] s_allDisplayRotations = new[]
{
MediaControlNativeDisplayRotation.Rotate0,
MediaControlNativeDisplayRotation.Rotate90,
MediaControlNativeDisplayRotation.Rotate180,
MediaControlNativeDisplayRotation.Rotate270
};|
🤖 [AI Review] Reviewed — no findings. Scope checked:
No 🔴 critical issues, no 🟡 suggestions to flag. Automated review — final merge decision rests with human reviewers. |
Replace Enum.GetValues<T>() with explicit per-flag arrays for the display mode and rotation capability enums. Enum.GetValues included the aggregate All member, causing ToPublicList to emit a duplicate entry (and call ToPublic with All) when all flags were set. Applied-Human-Comments: 3448302429
|
🤖 [AI Review] |
Summary
The
MediaControllercapability / display-mode conversion helpers calledEnum.GetValues(typeof(T))on every invocation. The non-generic overload returns anArray, so each call allocates a fresh array and boxes every enum value. One of the sites (PlaybackCapabilityUpdated) is a hot path that fires on every server-side playback-capability change (multiple times per second during media transitions/scrubbing).This change replaces those calls with
static readonlycaches populated once via the strongly-typed genericEnum.GetValues<T>()overload (.NET 5+), eliminating the per-call array allocation and boxing.Changes
src/Tizen.Multimedia.Remoting/MediaController/MediaController.css_allPlaybackActionsstatic cache; use it inGetPlaybackCapabilities(was line 542).src/Tizen.Multimedia.Remoting/MediaController/MediaController.Events.css_allPlaybackActionscache inCreatePlaybackCapabilityUpdatedEventArgs(was line 207).src/Tizen.Multimedia.Remoting/MediaController/MediaControlEnums.css_allDisplayModes/s_allDisplayRotationsstatic caches inEnumExtensions; use them in theToPublicListhelpers (was lines 531, 595).Mode
Refactoring
Verification
dotnet build Tizen.Multimedia.Remoting— 0 errors; pre-existing warnings only)Fixes #7642