Skip to content

Commit 4ccb4fd

Browse files
committed
perf(realtime): avoid shifting queued transcription audio
1 parent fe408e1 commit 4ccb4fd

1 file changed

Lines changed: 34 additions & 10 deletions

File tree

‎src/realtime-transcription/websocket-session.ts‎

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ class WebSocketRealtimeTranscriptionSession<Event> implements RealtimeTranscript
7272
private closed = false;
7373
private connected = false;
7474
private currentUrl = "";
75-
private queuedAudio: Buffer[] = [];
75+
private queuedAudio: Array<Buffer | undefined> = [];
76+
private queuedAudioHead = 0;
7677
private queuedBytes = 0;
7778
private ready = false;
7879
private readySinceMs: number | undefined;
@@ -140,8 +141,7 @@ class WebSocketRealtimeTranscriptionSession<Event> implements RealtimeTranscript
140141
this.ready = false;
141142
this.readySinceMs = undefined;
142143
this.reconnectSupervisor.cancel();
143-
this.queuedAudio = [];
144-
this.queuedBytes = 0;
144+
this.clearQueuedAudio();
145145
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
146146
this.forceClose();
147147
return;
@@ -380,21 +380,45 @@ class WebSocketRealtimeTranscriptionSession<Event> implements RealtimeTranscript
380380
}
381381

382382
private queueAudio(audio: Buffer): void {
383-
this.queuedAudio.push(Buffer.from(audio));
384-
this.queuedBytes += audio.byteLength;
385-
while (this.queuedBytes > this.maxQueuedBytes && this.queuedAudio.length > 0) {
383+
const queued = Buffer.from(audio);
384+
this.queuedAudio.push(queued);
385+
this.queuedBytes += queued.byteLength;
386+
while (
387+
this.queuedBytes > this.maxQueuedBytes &&
388+
this.queuedAudioHead < this.queuedAudio.length
389+
) {
386390
// Keep the most recent audio when reconnects stall; old buffered audio is
387-
// less useful than avoiding unbounded memory growth.
388-
const dropped = this.queuedAudio.shift();
391+
// less useful than avoiding unbounded memory growth. Advancing a head
392+
// index keeps sustained overflow amortized O(1) instead of shifting the queue.
393+
const dropped = this.queuedAudio[this.queuedAudioHead];
394+
this.queuedAudio[this.queuedAudioHead] = undefined;
395+
this.queuedAudioHead += 1;
389396
this.queuedBytes -= dropped?.byteLength ?? 0;
390397
}
398+
this.compactQueuedAudio();
391399
}
392400

393401
private flushQueuedAudio(): void {
394-
for (const audio of this.queuedAudio) {
395-
this.options.sendAudio(audio, this.transport);
402+
for (let index = this.queuedAudioHead; index < this.queuedAudio.length; index += 1) {
403+
const audio = this.queuedAudio[index];
404+
if (audio) {
405+
this.options.sendAudio(audio, this.transport);
406+
}
407+
}
408+
this.clearQueuedAudio();
409+
}
410+
411+
private compactQueuedAudio(): void {
412+
if (this.queuedAudioHead === 0 || this.queuedAudioHead * 2 < this.queuedAudio.length) {
413+
return;
396414
}
415+
this.queuedAudio = this.queuedAudio.slice(this.queuedAudioHead);
416+
this.queuedAudioHead = 0;
417+
}
418+
419+
private clearQueuedAudio(): void {
397420
this.queuedAudio = [];
421+
this.queuedAudioHead = 0;
398422
this.queuedBytes = 0;
399423
}
400424

0 commit comments

Comments
 (0)