From 5ca9d03930f593b3cae1944088998b7259d4dd0a Mon Sep 17 00:00:00 2001
From: seonghobae <8172694+seonghobae@users.noreply.github.com>
Date: Mon, 6 Jul 2026 13:59:20 +0000
Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20lowestConfidence?=
=?UTF-8?q?=20finding=20with=20early=20exit?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
💡 What: Replaced the unconditional `.reduce()` call in `lowestConfidence` calculation with a `for...of` loop and an early `break`.
🎯 Why: Finding a minimum value with a known absolute bound (like a 'low' confidence level) can short-circuit, preventing unnecessary operations on remaining elements.
📊 Impact: Transforms an unconditional O(N) array iteration into an operation that can exit in O(1) time when the minimum absolute bound is met.
🔬 Measurement: Verify by executing `npm run test --workspace=apps/desktop` to see the new early exit test case pass.
---
.jules/bolt.md | 4 ++++
apps/desktop/src/App.test.tsx | 35 +++++++++++++++++++++++++++++++++++
apps/desktop/src/App.tsx | 20 ++++++++++++--------
3 files changed, 51 insertions(+), 8 deletions(-)
diff --git a/.jules/bolt.md b/.jules/bolt.md
index 38d4b732..d14c77d3 100644
--- a/.jules/bolt.md
+++ b/.jules/bolt.md
@@ -41,3 +41,7 @@
## 2025-02-15 - Replace Array.from(map.values()).map with a for...of loop
**Learning:** Using `Array.from(map.values()).map(...)` creates an unnecessary intermediate array which wastes memory allocation and garbage collection time, particularly for frequently re-rendered components handling large collections.
**Action:** Use a `for...of` loop over `map.values()` to iterate and push mapped elements directly into the final array for O(1) memory and avoiding intermediate array allocations.
+
+## 2025-02-15 - Optimize finding min/max with known absolute bounds
+**Learning:** For performance optimizations involving finding a minimum or maximum value with a known absolute bound (e.g., finding a 'low' confidence level), unconditional `.reduce()` calls force an O(N) operation over all elements, wasting CPU cycles on large arrays.
+**Action:** Replace unconditional `.reduce()` calls with a `for...of` loop and an early `break` condition when the known absolute bound is found. This transforms the O(N) operation into one that can short-circuit, yielding measurable performance gains.
diff --git a/apps/desktop/src/App.test.tsx b/apps/desktop/src/App.test.tsx
index c039dfba..7240d1da 100644
--- a/apps/desktop/src/App.test.tsx
+++ b/apps/desktop/src/App.test.tsx
@@ -293,6 +293,41 @@ describe("App", () => {
expect(screen.getAllByText(/2 sections/i).length).toBeGreaterThan(0);
});
+ it("summarizes confidence from the lowest-confidence loaded section handling early exit for 'low'", async () => {
+ const baseProject = succeededResult().result;
+ // Clone sections to prevent mutating the shared fixture state and causing flaky tests
+ const loadedProject = {
+ ...baseProject,
+ sections: [
+ {
+ ...baseProject.sections[0],
+ id: "intro-1",
+ label: "intro",
+ confidence: { level: "low" as const, source: "model" as const, notes: "Hard to hear." }
+ },
+ ...baseProject.sections,
+ {
+ ...baseProject.sections[0],
+ id: "chorus-1",
+ label: "chorus",
+ confidence: { level: "high" as const, source: "model" as const, notes: "The chorus form is clear." }
+ }
+ ]
+ };
+ mockLoadProject.mockResolvedValueOnce(loadedProject);
+ render();
+
+ fireEvent.click(screen.getByRole("button", { name: /open project/i }));
+
+ await waitFor(() => {
+ expect(screen.getByText(/^Low$/i)).toBeTruthy();
+ });
+
+ // Dynamically check the number of sections added
+ const sectionCountText = new RegExp(`${loadedProject.sections.length} sections`, "i");
+ expect(screen.getAllByText(sectionCountText).length).toBeGreaterThan(0);
+ });
+
it("selects a local audio source and starts a local-audio analysis job", async () => {
tauriInvoke
.mockResolvedValueOnce(bootstrapResponse())
diff --git a/apps/desktop/src/App.tsx b/apps/desktop/src/App.tsx
index 24f5fb09..3bcb8fe5 100644
--- a/apps/desktop/src/App.tsx
+++ b/apps/desktop/src/App.tsx
@@ -180,15 +180,19 @@ function MetricCard({
function ConfidenceMetric({ song }: { song: RehearsalSong | null }) {
const sectionCount = song?.sections.length ?? 0;
const confidenceOrder = { high: 3, medium: 2, low: 1 } as const;
- const lowestConfidence = song?.sections.reduce(
- (current, section) => {
- if (!current || confidenceOrder[section.confidence.level] < confidenceOrder[current]) {
- return section.confidence.level;
+ let lowestConfidence: RehearsalSong["sections"][number]["confidence"]["level"] | null = null;
+
+ if (song?.sections) {
+ for (const section of song.sections) {
+ if (!lowestConfidence || confidenceOrder[section.confidence.level] < confidenceOrder[lowestConfidence]) {
+ lowestConfidence = section.confidence.level;
}
- return current;
- },
- null
- );
+ if (lowestConfidence === "low") {
+ break; // Early exit: "low" is the absolute minimum possible value
+ }
+ }
+ }
+
const confidence = lowestConfidence ? `${lowestConfidence[0].toUpperCase()}${lowestConfidence.slice(1)}` : "Ready";
const detail = sectionCount > 0 ? `${sectionCount} section${sectionCount === 1 ? "" : "s"}` : "Local analysis";