From ea9ea25809f80a767aaf0b48a64cd13701119b52 Mon Sep 17 00:00:00 2001 From: Steven Vandevelde Date: Wed, 18 Feb 2026 15:03:16 +0100 Subject: [PATCH] fix: various perf issues --- src/common/element.js | 7 +++- src/components/configurator/input/worker.js | 2 +- src/components/input/opensubsonic/common.js | 1 - .../orchestrator/scoped-tracks/element.js | 28 ++++++++----- .../orchestrator/scoped-tracks/types.d.ts | 2 +- .../orchestrator/scoped-tracks/worker.js | 18 ++++++--- src/components/processor/search/constants.js | 4 ++ src/components/processor/search/worker.js | 40 ++++++++++++------- src/themes/webamp/browser/element.js | 3 +- 9 files changed, 68 insertions(+), 37 deletions(-) diff --git a/src/common/element.js b/src/common/element.js index d06b0013..3e003b68 100644 --- a/src/common/element.js +++ b/src/common/element.js @@ -3,7 +3,12 @@ import { decodeMessage, encodeMessage, RPCChannel } from "@kunkun/kkrpc"; import { html, render } from "lit-html"; import { effect, signal } from "@common/signal.js"; -import { rpc, workerLink, workerProxy, workerTunnel } from "./worker.js"; +import { + rpc, + workerLink, + workerProxy, + workerTunnel, +} from "./worker.js"; import { BrowserPostMessageIo } from "./worker/rpc.js"; /** diff --git a/src/components/configurator/input/worker.js b/src/components/configurator/input/worker.js index f1e15c96..e7ac6c32 100644 --- a/src/components/configurator/input/worker.js +++ b/src/components/configurator/input/worker.js @@ -89,7 +89,7 @@ export async function groupConsult({ data, ports }) { ); return consultations.reduce((acc, c) => { - return { ...acc, ...c }; + return Object.assign(acc, c); }, {}); } diff --git a/src/components/input/opensubsonic/common.js b/src/components/input/opensubsonic/common.js index 9531f46f..7a0f36b4 100644 --- a/src/components/input/opensubsonic/common.js +++ b/src/components/input/opensubsonic/common.js @@ -57,7 +57,6 @@ export function buildURI(server, args) { export async function consultServer(server) { const client = createClient(server); const resp = await client.ping().catch(() => undefined); - return resp?.status?.toLowerCase() === "ok"; } diff --git a/src/components/orchestrator/scoped-tracks/element.js b/src/components/orchestrator/scoped-tracks/element.js index 02ec0e8f..e99bca86 100644 --- a/src/components/orchestrator/scoped-tracks/element.js +++ b/src/components/orchestrator/scoped-tracks/element.js @@ -57,6 +57,7 @@ class ScopedTracksOrchestrator extends BroadcastableDiffuseElement { ); }); + #tracksAvailable = signal(/** @type {Track[]} */ ([])); #tracksSearch = signal(/** @type {Track[]} */ ([])); #tracksFinal = signal(/** @type {Track[]} */ ([])); @@ -118,21 +119,26 @@ class ScopedTracksOrchestrator extends BroadcastableDiffuseElement { this.effect(async () => { const collection = output.tracks.collection(); if ((await this.isLeader()) === false) return; - this.#proxy.supplyAvailable(collection); + const { availableTracks } = await this.#proxy.supplyAvailable(collection); + this.#tracksAvailable.value = availableTracks; }); // Watch search supply this.effect(async () => { const _trigger = search.supplyFingerprint(); + const availableTracks = this.#tracksAvailable.value; const searchTerm = this.#scope.value?.searchTerm(); if ((await this.isLeader()) === false) return; - const searchResults = searchTerm - ? await this.#proxy.searchTracks({ term: searchTerm }) - : untracked(() => output.tracks.collection()); - - this.#tracksSearch.set(searchResults); + if (searchTerm?.length) { + const searchResults = await this.#proxy.searchTracks({ + term: searchTerm, + }); + this.#tracksSearch.set(searchResults); + } else { + this.#tracksSearch.set(availableTracks); + } }); // Watch `#tracksSearch` + Playlist @@ -142,11 +148,11 @@ class ScopedTracksOrchestrator extends BroadcastableDiffuseElement { if ((await this.isLeader()) === false) return; - this.#tracksFinal.set( - playlist - ? await this.#proxy.filterByPlaylist({ tracks, playlist }) - : tracks, - ); + const final = playlist + ? await this.#proxy.filterByPlaylist({ tracks, playlist }) + : tracks; + + this.#tracksFinal.set(final); }); } diff --git a/src/components/orchestrator/scoped-tracks/types.d.ts b/src/components/orchestrator/scoped-tracks/types.d.ts index 9f104331..75033d57 100644 --- a/src/components/orchestrator/scoped-tracks/types.d.ts +++ b/src/components/orchestrator/scoped-tracks/types.d.ts @@ -6,5 +6,5 @@ import type { Schema } from "@components/processor/search/types.d.ts"; export type Actions = { filterByPlaylist(args: { tracks: Track[]; playlist: Playlist }): Promise; searchTracks(params: SearchParams): Promise; - supplyAvailable(tracks: Track[]): Promise; + supplyAvailable(tracks: Track[]): Promise<{ availableTracks: Track[] }>; }; diff --git a/src/components/orchestrator/scoped-tracks/worker.js b/src/components/orchestrator/scoped-tracks/worker.js index a52c280a..3f375cca 100644 --- a/src/components/orchestrator/scoped-tracks/worker.js +++ b/src/components/orchestrator/scoped-tracks/worker.js @@ -32,15 +32,20 @@ export async function supplyAvailable({ data, ports }) { const groups = await input.groupConsult(cachedTracks); /** @type {Track[]} */ - let availableTracks = []; + const availableTracks = []; Object.values(groups).forEach((value) => { if (value.available === false) return; - availableTracks = availableTracks.concat(value.tracks); + for (const track of value.tracks) { + availableTracks.push(track); + } }, []); // Set pool - await search.supply({ tracks: availableTracks }); + search.supply({ tracks: availableTracks }); + + // Fin + return { availableTracks }; } /** @@ -48,9 +53,10 @@ export async function supplyAvailable({ data, ports }) { */ export async function searchTracks({ data, ports }) { /** @type {ProxiedActions} */ - const search = workerProxy(() => ports.search); - - ports.search.start(); + const search = workerProxy(() => { + ports.search.start(); + return ports.search; + }); return await search.search(data); } diff --git a/src/components/processor/search/constants.js b/src/components/processor/search/constants.js index b9110a7f..6b9b527b 100644 --- a/src/components/processor/search/constants.js +++ b/src/components/processor/search/constants.js @@ -1,3 +1,7 @@ +import { xxh32 } from "xxh32"; + +export const EMPTY_FINGERPRINT = xxh32([].join("")).toString(); + /** * Maps directly on the `Track` definition * (ie. `definitions/output/tracks.json`) diff --git a/src/components/processor/search/worker.js b/src/components/processor/search/worker.js index 12a48e7e..5735801c 100644 --- a/src/components/processor/search/worker.js +++ b/src/components/processor/search/worker.js @@ -17,9 +17,9 @@ import { effect, signal } from "@common/signal.js"; // STATE //////////////////////////////////////////// -export const $inserted = signal(/** @type {Set} */ (new Set()), { - eager: true, -}); +/** @type {Set} */ +export let inserted = new Set(); +export let insertedFingerprint = ""; // Communicated state export const $supplyFingerprint = signal( @@ -72,30 +72,40 @@ export async function supply({ tracks }) { // TODO: Generate a hash based on the track itself, // so we can detect changes to tags or other data. - /** @type {string[]} */ - const ids = []; + /** @type {Map} */ + const tracksMap = new Map(); + + for (const track of tracks) { + tracksMap.set(track.id, track); + } - /** @type {Record} */ - const tracksMap = {}; + const ids = Array.from(tracksMap.keys()); + const idsString = ids.sort().join(""); + const fingerprint = xxh32(idsString).toString(); - tracks.forEach((track) => { - ids.push(track.id); - tracksMap[track.id] = track; - }); + if (fingerprint === insertedFingerprint) return; - const currentSet = $inserted.value; + const currentSet = inserted; const newSet = new Set(ids); - $inserted.value = newSet; + inserted = newSet; + insertedFingerprint = fingerprint; const removedIds = currentSet.difference(newSet); const newIds = newSet.difference(currentSet); - const newTracks = Array.from(newIds).map((id) => tracksMap[id]); + + /** @type {Track[]} */ + const newTracks = []; + + for (const id of newIds) { + const track = tracksMap.get(id); + if (track) newTracks.push(track); + } await Orama.removeMultiple(db, Array.from(removedIds)); await Orama.insertMultiple(db, newTracks); - $supplyFingerprint.value = xxh32(ids.sort().join("")).toString(); + $supplyFingerprint.value = fingerprint; } //////////////////////////////////////////// diff --git a/src/themes/webamp/browser/element.js b/src/themes/webamp/browser/element.js index ad948033..882eed4c 100644 --- a/src/themes/webamp/browser/element.js +++ b/src/themes/webamp/browser/element.js @@ -223,6 +223,7 @@ class Browser extends DiffuseElement { const isLoading = this.$output.value?.tracks?.state() !== "loaded"; const tracks = this.$provider.value?.tracks() ?? []; const playlistId = this.$scope.value?.playlistId(); + const searchTerm = this.$scope.value?.searchTerm() ?? ""; // Virtual list const totalTracks = tracks.length; @@ -314,7 +315,7 @@ class Browser extends DiffuseElement { + .setSearchTerm}" value="${searchTerm}" />