Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[AI Task] [Tizen.Multimedia.AudioIO] Remove params array allocation in ValidateState hot path#7645

Open
JoonghyunCho wants to merge 2 commits into
main from
ai-task/issue-7609
Open

[AI Task] [Tizen.Multimedia.AudioIO] Remove params array allocation in ValidateState hot path #7645
JoonghyunCho wants to merge 2 commits into
main from
ai-task/issue-7609

Conversation

@JoonghyunCho

@JoonghyunCho JoonghyunCho commented May 17, 2026

Copy link
Copy Markdown
Member

Summary

AudioCapture.Read(int) and AudioPlayback.Write(byte[]) are streaming hot paths that call ValidateState(...) with 1 or 2 explicit AudioIOState values. The previous params AudioIOState[] desiredStates signature allocated a fresh single-element (or two-element) array per call and then went through LINQ Contains. This PR replaces the params overload with explicit 1-arg and 2-arg overloads, eliminating the per-call allocation and the LINQ dispatch on the success path.

Changes

  • src/Tizen.Multimedia.AudioIO/AudioIO/AudioIOUtil.cs
    • Replaced ValidateState(AudioIOState, params AudioIOState[]) with two overloads: (curState, desired) and (curState, desired1, desired2).
    • Throw branch factored out into private [MethodImpl(MethodImplOptions.NoInlining)] helpers so the success path stays small and inline-friendly.
    • Dropped System.Linq using (no longer needed).
  • src/Tizen.Multimedia.AudioIO/AudioIO/AudioCapture.cs
    • AudioCaptureBase.ValidateState split into 1-arg and 2-arg overloads delegating to AudioIOUtil.
  • src/Tizen.Multimedia.AudioIO/AudioIO/AudioPlayback.cs
    • AudioPlayback.ValidateState split into 1-arg and 2-arg overloads delegating to AudioIOUtil.

All 13 call sites in AudioCapture / AudioPlayback already pass 1 or 2 states, so they bind to the new overloads with no source changes.

Mode

Refactoring

Performance impact (per call)

Item Before After
params array allocation 1 (AudioIOState[1] / AudioIOState[2]) 0
Contains dispatch LINQ Contains direct == (and `
Success-path inlining unlikely (params + LINQ) likely (tiny body)

Significant in AudioCapture.Read / AudioPlayback.Write which can be called many times per second per stream.

API compatibility

  • All affected types/members are internal (AudioIOUtil) or internal/private (the wrappers on AudioCaptureBase / AudioPlayback). No public API change.
  • Exception type unchanged (InvalidOperationException).
  • Exception message format preserved (same "The audio is not in a valid state. Current State : X, Valid State : Y." shape).
  • No native interop change.

Verification

  • Build: passed (dotnet build src/Tizen.Multimedia.AudioIO/Tizen.Multimedia.AudioIO.csproj — 0 errors; pre-existing warnings unchanged).
  • Tests: N/A (existing tests cover the affected methods; semantics unchanged).
  • Benchmark: skipped (no sdb device available in this environment — manual on-device benchmark recommended).

Fixes #7609

gemini-code-assist[bot] reacted with eyes emoji
...teState hot paths
AudioCapture.Read / AudioPlayback.Write are streaming hot loops that
each called ValidateState(...) with 1 or 2 explicit states. The
`params AudioIOState[]` overload allocated a fresh array per call and
went through LINQ Contains.
Split into explicit 1-arg and 2-arg overloads on AudioIOUtil and on
the AudioCaptureBase / AudioPlayback wrappers. The success path is now
inline-friendly with no allocation; the throw branch is factored out
behind [MethodImpl(NoInlining)] helpers.
All 13 callers in AudioCapture / AudioPlayback already pass 1 or 2
states, so the params overload is removed entirely.
No public API change (all types involved are internal/private).
Exception type and message format preserved.
Fixes #7609 

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

Copy link
Copy Markdown

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 state validation in AudioCapture, AudioPlayback, and AudioIOUtil by replacing the params array-based ValidateState method with specific overloads for one or two states, which avoids array allocations and LINQ overhead. The reviewer suggests marking these new validation methods with [MethodImpl(MethodImplOptions.AggressiveInlining)] to further optimize performance in critical hot paths.

Comment on lines +24 to +32
internal static void ValidateState(AudioIOState curState, AudioIOState desired)
{
Debug.Assert(desiredStates.Length > 0);
if (curState == desired)
{
return;
}

if (desiredStates.Contains(curState))
ThrowInvalidState(curState, desired);
}

@gemini-code-assist gemini-code-assist Bot May 17, 2026

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

Since these methods are part of a performance-critical hot path (as mentioned in the PR description), it is recommended to mark them with [MethodImpl(MethodImplOptions.AggressiveInlining)]. This encourages the JIT compiler to inline these small validation checks directly into the calling methods (like Read or Write), reducing method call overhead and potentially enabling further optimizations.

 [MethodImpl(MethodImplOptions.AggressiveInlining)]
 internal static void ValidateState(AudioIOState curState, AudioIOState desired)
 {
 if (curState == desired)
 {
 return;
 }
 ThrowInvalidState(curState, desired);
 }

Comment on lines +34 to 42
internal static void ValidateState(AudioIOState curState, AudioIOState desired1, AudioIOState desired2)
{
if (curState == desired1 || curState == desired2)
{
return;
}

throw new InvalidOperationException($"The audio is not in a valid state. " +
$"Current State : { curState }, Valid State : { string.Join(", ", desiredStates) }.");
ThrowInvalidState(curState, desired1, desired2);
}

@gemini-code-assist gemini-code-assist Bot May 17, 2026

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

Similar to the 1-argument overload, this method should also be marked with [MethodImpl(MethodImplOptions.AggressiveInlining)] to ensure optimal performance in the streaming hot path.

 [MethodImpl(MethodImplOptions.AggressiveInlining)]
 internal static void ValidateState(AudioIOState curState, AudioIOState desired1, AudioIOState desired2)
 {
 if (curState == desired1 || curState == desired2)
 {
 return;
 }
 ThrowInvalidState(curState, desired1, desired2);
 }

Copy link
Copy Markdown
Member Author

🤖 [AI Review]

Reviewed — no findings.

Scope checked:

  • Confirmed all 13 ValidateState call sites in AudioCapture.cs (6) and AudioPlayback.cs (7) pass exactly 1 or 2 AudioIOState values — every call binds cleanly to one of the two new non-params overloads.
  • Verified AudioIOUtil is internal static and the new wrappers on AudioCaptureBase / AudioPlayback are internal / private — no public API surface change, no XML doc requirement.
  • Compared exception message shape before vs. after: 2-arg path produces "Valid State : A, B." matching the prior string.Join(", ", ...) output exactly; 1-arg path produces "Valid State : A.". InvalidOperationException type preserved.
  • [MethodImpl(MethodImplOptions.NoInlining)] is correctly applied to the throw helpers (keeps the cold path out of the hot caller), and the dropped System.Linq + System.Diagnostics usings are not referenced elsewhere in the file.
  • The removed Debug.Assert(desiredStates.Length > 0) is no longer needed since both overloads require at least one explicit argument by signature.

No 🔴 critical issues, no 🟡 suggestions to flag.


Automated review — final merge decision rests with human reviewers.

Add [MethodImpl(MethodImplOptions.AggressiveInlining)] to both
AudioIOUtil.ValidateState overloads so the JIT inlines these tiny
state checks into the streaming hot path (Read/Write).
Applied-Human-Comments: 3254517301,3254517303

Copy link
Copy Markdown
Member Author

🤖 [AI Review]
Addressed review feedback in commit 9c1ac21. Summary: added [MethodImpl(MethodImplOptions.AggressiveInlining)] to both ValidateState overloads so the JIT can inline these checks into the Read/Write hot path.

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

Reviewers

@hsgwon hsgwon Awaiting requested review from hsgwon hsgwon is a code owner

1 more reviewer

@gemini-code-assist gemini-code-assist[bot] gemini-code-assist[bot] left review comments

Reviewers whose approvals may not affect merge requirements

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

[AI Refactoring] AudioIO ValidateState 의 params 배열 할당 제거 (Read/Write 핫 루프) [Scope: src/Tizen.Multimedia.AudioIO] (2026年05月03日)

1 participant

AltStyle によって変換されたページ (->オリジナル) /