-
Notifications
You must be signed in to change notification settings - Fork 2
perf: optimize audio engine callback removal with Set #219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ysdede
wants to merge
1
commit into
master
Choose a base branch
from
perf/audioengine-sets-230949484791968314
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,19 +36,19 @@ export class AudioEngine implements IAudioEngine { | |
|
|
||
| private currentEnergy: number = 0; | ||
|
|
||
| private segmentCallbacks: Array<(segment: AudioSegment) => void> = []; | ||
| private segmentCallbacks = new Set<(segment: AudioSegment) => void>(); | ||
|
|
||
| // Fixed-window streaming state (v3 token streaming mode) | ||
| private windowCallbacks: Array<{ | ||
| private windowCallbacks = new Set<{ | ||
| windowDuration: number; | ||
| overlapDuration: number; | ||
| triggerInterval: number; | ||
| callback: (audio: Float32Array, startTime: number) => void; | ||
| lastWindowEnd: number; // Frame offset of last window end | ||
| }> = []; | ||
| }>(); | ||
|
|
||
| // Resampled audio chunk callbacks (for mel worker, etc.) | ||
| private audioChunkCallbacks: Array<(chunk: Float32Array) => void> = []; | ||
| private audioChunkCallbacks = new Set<(chunk: Float32Array) => void>(); | ||
|
|
||
| // SMA buffer for energy calculation | ||
| private energyHistory: number[] = []; | ||
|
|
@@ -75,7 +75,7 @@ export class AudioEngine implements IAudioEngine { | |
| }; | ||
|
|
||
| // Subscribers for visualization updates | ||
| private visualizationCallbacks: Array<(data: Float32Array, metrics: AudioMetrics, bufferEndTime: number) => void> = []; | ||
| private visualizationCallbacks = new Set<(data: Float32Array, metrics: AudioMetrics, bufferEndTime: number) => void>(); | ||
| private lastVisualizationNotifyTime: number = 0; | ||
| private readonly VISUALIZATION_NOTIFY_INTERVAL_MS = 33; // ~30fps in foreground | ||
| private readonly VISUALIZATION_NOTIFY_HIDDEN_INTERVAL_MS = 250; // Lower cadence for hidden tab | ||
|
|
@@ -440,9 +440,9 @@ export class AudioEngine implements IAudioEngine { | |
| } | ||
|
|
||
| onSpeechSegment(callback: (segment: AudioSegment) => void): () => void { | ||
| this.segmentCallbacks.push(callback); | ||
| this.segmentCallbacks.add(callback); | ||
| return () => { | ||
| this.segmentCallbacks = this.segmentCallbacks.filter((cb) => cb !== callback); | ||
| this.segmentCallbacks.delete(callback); | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -463,10 +463,10 @@ export class AudioEngine implements IAudioEngine { | |
| callback, | ||
| lastWindowEnd: 0, // Will be set on first chunk | ||
| }; | ||
| this.windowCallbacks.push(entry); | ||
| this.windowCallbacks.add(entry); | ||
|
|
||
| return () => { | ||
| this.windowCallbacks = this.windowCallbacks.filter((e) => e !== entry); | ||
| this.windowCallbacks.delete(entry); | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -476,9 +476,9 @@ export class AudioEngine implements IAudioEngine { | |
| * Returns an unsubscribe function. | ||
| */ | ||
| onAudioChunk(callback: (chunk: Float32Array) => void): () => void { | ||
| this.audioChunkCallbacks.push(callback); | ||
| this.audioChunkCallbacks.add(callback); | ||
| return () => { | ||
| this.audioChunkCallbacks = this.audioChunkCallbacks.filter((cb) => cb !== callback); | ||
| this.audioChunkCallbacks.delete(callback); | ||
| }; | ||
|
Comment on lines
478
to
482
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1. Unsubscribe can skip callbacks During callback dispatch, unsubscribing now mutates the same Set being iterated, which can cause later callbacks in the same notify cycle to be skipped. Previously, unsubscription reassigned a new array (filter) so the in-flight iteration over the old array would continue unaffected. Agent Prompt
|
||
| } | ||
|
|
||
|
|
@@ -933,9 +933,9 @@ export class AudioEngine implements IAudioEngine { | |
| * - Callbacks must not mutate `data` in place. | ||
| */ | ||
| onVisualizationUpdate(callback: (data: Float32Array, metrics: AudioMetrics, bufferEndTime: number) => void): () => void { | ||
| this.visualizationCallbacks.push(callback); | ||
| this.visualizationCallbacks.add(callback); | ||
| return () => { | ||
| this.visualizationCallbacks = this.visualizationCallbacks.filter((cb) => cb !== callback); | ||
| this.visualizationCallbacks.delete(callback); | ||
| }; | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For improved readability and maintainability, consider extracting the inline type for
windowCallbacksinto a named interface. This makes the code cleaner and the type reusable.For example, you could define an interface at the top of the file or before the class:
And then use it here: