[AI Task] [Tizen.Multimedia.Camera] Remove params allocation from Camera.ValidateState#7670
[AI Task] [Tizen.Multimedia.Camera] Remove params allocation from Camera.ValidateState#7670JoonghyunCho wants to merge 1 commit into
Conversation
…free overloads ValidateState was declared with `params CameraState[] required` and used `required.Contains(curState)` for the check. Every call allocated a new 1- or 2-element array plus a LINQ enumerator, even though all 10 call sites pass exactly 1 or 2 constants. Replaces the single params method with explicit 1-arg and 2-arg overloads that compare with `!=` directly. The error path is hoisted into a private helper that keeps the original `params` formatting so the thrown message is unchanged. Existing call sites bind to the new overloads with no source change. Fixes #7623
There was a problem hiding this comment.
Code Review
This pull request refactors the ValidateState method in Camera.cs to avoid array allocations by replacing the params array parameter with specific overloads for one or two CameraState arguments, and extracting the exception throwing logic into a helper method. The review feedback suggests marking the exception-throwing helper method with [MethodImpl(MethodImplOptions.NoInlining)] to optimize JIT inlining for the validation methods.
| private static void ThrowInvalidState(CameraState curState, params CameraState[] required) => | ||
| throw new InvalidOperationException($"The camera is not in a valid state. " + | ||
| $"Current State : { curState }, Valid State : { string.Join(", ", required) }."); |
There was a problem hiding this comment.
To ensure that the hot-path validation methods (ValidateState) are highly eligible for JIT inlining, it is a recommended practice to mark the exception-throwing helper method with [MethodImpl(MethodImplOptions.NoInlining)]. This keeps the caller's code size small and avoids the overhead of the exception-throwing logic being considered during inlining decisions.
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
private static void ThrowInvalidState(CameraState curState, params CameraState[] required) =>
throw new InvalidOperationException($"The camera is not in a valid state. " +
$"Current State : { curState }, Valid State : { string.Join(", ", required) }.");
Summary
Split
Camera.ValidateState(params CameraState[])into explicit 1-arg and 2-arg overloads to eliminate the per-callCameraState[]allocation plus LINQContainsenumerator at every camera state guard.Changes
src/Tizen.Multimedia.Camera/Camera/Camera.csinternal void ValidateState(params CameraState[] required)with two non-allocating overloads ((CameraState)and(CameraState, CameraState)).private static ThrowInvalidStatehelper that keeps the originalparams-based formatting so the thrown message is byte-identical.Camera.cs, 2 inCamera.Properties.cs) pass exactly 1 or 2 constants and now bind to the new overloads without any change at the call site.Mode
Refactoring
Verification
dotnet buildonsrc/Tizen.Multimedia.Camera, 0 errors)Fixes #7623