Skip to content

[AI Task] [Tizen.Multimedia.Camera] Remove params allocation from Camera.ValidateState#7670

Open
JoonghyunCho wants to merge 1 commit into
mainfrom
ai-task/issue-7623
Open

[AI Task] [Tizen.Multimedia.Camera] Remove params allocation from Camera.ValidateState#7670
JoonghyunCho wants to merge 1 commit into
mainfrom
ai-task/issue-7623

Conversation

@JoonghyunCho
Copy link
Copy Markdown
Member

Summary

Split Camera.ValidateState(params CameraState[]) into explicit 1-arg and 2-arg overloads to eliminate the per-call CameraState[] allocation plus LINQ Contains enumerator at every camera state guard.

Changes

  • src/Tizen.Multimedia.Camera/Camera/Camera.cs
    • Replace internal void ValidateState(params CameraState[] required) with two non-allocating overloads ((CameraState) and (CameraState, CameraState)).
    • Extract the exception path into a private static ThrowInvalidState helper that keeps the original params-based formatting so the thrown message is byte-identical.
  • All 10 existing call sites (8 in Camera.cs, 2 in Camera.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

  • Build: passed (dotnet build on src/Tizen.Multimedia.Camera, 0 errors)
  • Tests: N/A
  • Benchmark: skipped (no sdb device available in this run; manual verification recommended on target)

Fixes #7623

…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
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +240 to +242
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) }.");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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) }.");

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

Projects

None yet

1 participant