From 38034952e12889388cb7a8260dfb552a787e27fe Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 7 Apr 2026 10:23:38 +0000 Subject: [PATCH 1/5] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20audio=20conc?= =?UTF-8?q?atenation=20performance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces the O(N^2) byte-copy loop with an O(N) optimized `b''.join` strategy for compatible audio segments in `src/nodetool/media/audio/audio_helpers.py`. This optimization efficiently checks if segments share the same underlying parameters, joining their `raw_data` directly and spawning a single new `AudioSegment` at the end while still cleanly falling back to the original method if mismatched parameters occur. Co-authored-by: georgi <19498+georgi@users.noreply.github.com> --- src/nodetool/media/audio/audio_helpers.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/nodetool/media/audio/audio_helpers.py b/src/nodetool/media/audio/audio_helpers.py index 015bae826..4ae5de56d 100644 --- a/src/nodetool/media/audio/audio_helpers.py +++ b/src/nodetool/media/audio/audio_helpers.py @@ -81,6 +81,21 @@ def concatenate_audios(audios: list[AudioSegment]) -> AudioSegment: Returns: AudioSegment: The concatenated audio segment. """ + if not audios: + return AudioSegment.empty() + + # ⚡ Bolt Optimization: Use raw byte joining when audio parameters match + # to avoid O(N^2) copying penalty from += operator in loop. + first = audios[0] + same_params = all( + a.sample_width == first.sample_width and a.frame_rate == first.frame_rate and a.channels == first.channels + for a in audios + ) + + if same_params: + raw_data = b"".join(a.raw_data for a in audios) + return first._spawn(raw_data) + concatenated_audio = AudioSegment.empty() for audio in audios: concatenated_audio += audio From e3f89e8a54eb8436d0fd472cf82c0e44b20eba5f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 7 Apr 2026 10:29:40 +0000 Subject: [PATCH 2/5] Fix CI: Remove non-existent npm cache path from ts-parity-harness The `ts/package-lock.json` path no longer exists, causing the GitHub actions `setup-node` to fail when it attempts to cache NPM dependencies. This commit removes the `cache` configuration to allow the workflow to proceed. Co-authored-by: georgi <19498+georgi@users.noreply.github.com> --- .github/workflows/ts-parity-harness.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ts-parity-harness.yml b/.github/workflows/ts-parity-harness.yml index 6c192fce1..465d6ac5b 100644 --- a/.github/workflows/ts-parity-harness.yml +++ b/.github/workflows/ts-parity-harness.yml @@ -41,8 +41,6 @@ jobs: uses: actions/setup-node@v4 with: node-version: "20" - cache: npm - cache-dependency-path: ts/package-lock.json - name: Install dependencies run: npm ci From da3af2d925170338ba86ac20e69e6cdacde43d7f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 7 Apr 2026 10:49:14 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20audio=20conc?= =?UTF-8?q?atenation=20performance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces the O(N^2) byte-copy loop with an O(N) optimized `b''.join` strategy for compatible audio segments in `src/nodetool/media/audio/audio_helpers.py`. This optimization calculates the target parameters that `pydub`'s `+=` operator would implicitly upgrade to (comparing against an empty segment's defaults), converts all segments to those targets in an O(N) pass, and then performs a fast O(N) byte join using `empty._spawn(..., overrides={...})` to avoid the O(N^2) penalty of repeated `+=` copying while maintaining exactly the same parameters. Co-authored-by: georgi <19498+georgi@users.noreply.github.com> --- .github/workflows/ts-parity-harness.yml | 2 + src/nodetool/media/audio/audio_helpers.py | 47 +++++++++++++++-------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/.github/workflows/ts-parity-harness.yml b/.github/workflows/ts-parity-harness.yml index 465d6ac5b..6c192fce1 100644 --- a/.github/workflows/ts-parity-harness.yml +++ b/.github/workflows/ts-parity-harness.yml @@ -41,6 +41,8 @@ jobs: uses: actions/setup-node@v4 with: node-version: "20" + cache: npm + cache-dependency-path: ts/package-lock.json - name: Install dependencies run: npm ci diff --git a/src/nodetool/media/audio/audio_helpers.py b/src/nodetool/media/audio/audio_helpers.py index 4ae5de56d..a5d19e17f 100644 --- a/src/nodetool/media/audio/audio_helpers.py +++ b/src/nodetool/media/audio/audio_helpers.py @@ -84,22 +84,37 @@ def concatenate_audios(audios: list[AudioSegment]) -> AudioSegment: if not audios: return AudioSegment.empty() - # ⚡ Bolt Optimization: Use raw byte joining when audio parameters match - # to avoid O(N^2) copying penalty from += operator in loop. - first = audios[0] - same_params = all( - a.sample_width == first.sample_width and a.frame_rate == first.frame_rate and a.channels == first.channels - for a in audios - ) - - if same_params: - raw_data = b"".join(a.raw_data for a in audios) - return first._spawn(raw_data) - - concatenated_audio = AudioSegment.empty() - for audio in audios: - concatenated_audio += audio - return concatenated_audio + # ⚡ Bolt Optimization: Calculate the target parameters that pydub's += operator + # would implicitly upgrade to (comparing against an empty segment's defaults), + # convert all segments to those targets, and then do a fast O(N) byte join + # to avoid the O(N^2) penalty of repeated += copying. + empty = AudioSegment.empty() + + target_sample_width = empty.sample_width + target_frame_rate = empty.frame_rate + target_channels = empty.channels + + for a in audios: + target_sample_width = max(target_sample_width, a.sample_width) + target_frame_rate = max(target_frame_rate, a.frame_rate) + target_channels = max(target_channels, a.channels) + + raw_data_list = [] + for a in audios: + if a.sample_width != target_sample_width: + a = a.set_sample_width(target_sample_width) + if a.frame_rate != target_frame_rate: + a = a.set_frame_rate(target_frame_rate) + if a.channels != target_channels: + a = a.set_channels(target_channels) + raw_data_list.append(a.raw_data) + + res = empty._spawn(b"".join(raw_data_list), overrides={ + "sample_width": target_sample_width, + "frame_rate": target_frame_rate, + "channels": target_channels + }) + return res def remove_silence( From 7654a58e06e2e83c7e271b1175ff9404b513ee27 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 7 Apr 2026 11:02:14 +0000 Subject: [PATCH 4/5] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20audio=20conc?= =?UTF-8?q?atenation=20performance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces the O(N^2) byte-copy loop with an O(N) optimized `b''.join` strategy for compatible audio segments in `src/nodetool/media/audio/audio_helpers.py`. This optimization accurately replicates `pydub`'s internal parameter sync logic by taking the `max` of `sample_width`, `frame_rate`, and `channels` across all input segments. It calculates these target values, applies them to format each segment in an O(N) pass, and finally joins the raw bytes natively instead of repeatedly copying and allocating memory via `__add__`. This maintains exactly the same audio output format as the previous version. Co-authored-by: georgi <19498+georgi@users.noreply.github.com> --- src/nodetool/media/audio/audio_helpers.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/nodetool/media/audio/audio_helpers.py b/src/nodetool/media/audio/audio_helpers.py index a5d19e17f..a306b93c7 100644 --- a/src/nodetool/media/audio/audio_helpers.py +++ b/src/nodetool/media/audio/audio_helpers.py @@ -85,16 +85,15 @@ def concatenate_audios(audios: list[AudioSegment]) -> AudioSegment: return AudioSegment.empty() # ⚡ Bolt Optimization: Calculate the target parameters that pydub's += operator - # would implicitly upgrade to (comparing against an empty segment's defaults), + # would implicitly upgrade to by finding the max across all input segments, # convert all segments to those targets, and then do a fast O(N) byte join # to avoid the O(N^2) penalty of repeated += copying. - empty = AudioSegment.empty() + first = audios[0] + target_sample_width = first.sample_width + target_frame_rate = first.frame_rate + target_channels = first.channels - target_sample_width = empty.sample_width - target_frame_rate = empty.frame_rate - target_channels = empty.channels - - for a in audios: + for a in audios[1:]: target_sample_width = max(target_sample_width, a.sample_width) target_frame_rate = max(target_frame_rate, a.frame_rate) target_channels = max(target_channels, a.channels) @@ -109,7 +108,7 @@ def concatenate_audios(audios: list[AudioSegment]) -> AudioSegment: a = a.set_channels(target_channels) raw_data_list.append(a.raw_data) - res = empty._spawn(b"".join(raw_data_list), overrides={ + res = first._spawn(b"".join(raw_data_list), overrides={ "sample_width": target_sample_width, "frame_rate": target_frame_rate, "channels": target_channels From cb667d4d909fbcd949e7cdf91d5935fc373c6e0e Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 7 Apr 2026 11:25:18 +0000 Subject: [PATCH 5/5] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20audio=20conc?= =?UTF-8?q?atenation=20performance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces the O(N^2) byte-copy loop with an O(N) optimized `b''.join` strategy for compatible audio segments in `src/nodetool/media/audio/audio_helpers.py`. This optimization accurately replicates `pydub`'s internal parameter sync logic by taking the `max` of `sample_width`, `frame_rate`, and `channels` across all input segments. It calculates these target values, applies them to format each segment in an O(N) pass, and finally joins the raw bytes natively using `first._spawn(b"".join(raw_data_list), overrides=...)` instead of repeatedly copying and allocating memory via `__add__`. This maintains exactly the same audio output format as the previous version but runs vastly faster. Co-authored-by: georgi <19498+georgi@users.noreply.github.com>