How expensive are events and listeners in terms of RAM / CPU? #15571
Replies: 2 comments 2 replies
|
Short version: I would not register one listener per rendered track if the list can be in the thousands, especially if the whole set churns when switching views. It will probably work, but it is avoidable overhead. In the current API, each So the tradeoff is roughly:
For a music library UI, I would treat Tauri events as the boundary between Rust and the webview, not as the per-row subscription system. A better shape is usually one listener for the current view, or a small number of listeners for frontend stores, then emit payloads like Unique event names make sense for a small, stable set of channels. For thousands of track rows, I would avoid both "thousands of listeners on one event" and "thousands of event names" unless profiling your real view shows it is still fine. |
|
Tauri events are very lightweight — you can safely have thousands of listeners without meaningful RAM or CPU overhead. Here's the complete breakdown: How Tauri events work under the hoodEvents are routed through Tauri's IPC bridge (a postMessage channel between Rust and the WebView). Each
For 1,000–5,000 listeners, you're looking at a few MB of overhead total, which is negligible in any desktop app. Unique names vs. single event with payloadUse a single event name with a payload filter — this is almost always better: // ❌ Avoid: 1000 unique event names
await listen(`track-tags-updated-${trackId}`, handler);
// ✅ Better: single event name, filter in handler
await listen('track-tags-updated', (event) => {
if (event.payload.id === trackId) {
// update this track's UI
}
});Why single-name is faster: Tauri matches event names as strings. With 1,000 unique names, every Mass register/unregister on view switchThis is valid to do, but batch your cleanup: const unlisteners: Array<() => void> = [];
for (const track of tracks) {
const unlisten = await listen('track-tags-updated', (e) => {
if (e.payload.id === track.id) updateTrack(track.id, e.payload);
});
unlisteners.push(unlisten);
}
// On view unmount — clean up all at once
onDestroy(() => unlisteners.forEach(fn => fn()));If using React, put this in a Recommended architecture for a music playerRather than per-component listeners, use a single top-level listener that maintains a reactive store: // store.ts — one listener for the entire app lifetime
const trackStore = new Map<number, TrackData>();
await listen('track-tags-updated', (event) => {
const { id, tags } = event.payload;
trackStore.set(id, { ...trackStore.get(id), ...tags });
// signal reactivity (Svelte store, Jotai atom, Zustand, etc.)
});Each track component reads from the store rather than registering its own listener. This scales to any number of tracks with zero additional listener cost, and eliminates the mass-register/unregister problem entirely. TL;DR
|
Uh oh!
There was an error while loading. Please reload this page.
Hi all! I am writing a music player and trying to link database state with frontend state, and am wondering how many event listeners is reasonable to have registered at a time.
I display music tracks with tags (tags being artist name, track name, rating etc) using per-track display components on the WebView. I would like each track's component to update UI when tags are edited (which happens on the Rust side of things), through emitting events.
Since there are potentially thousands of track components rendered at a time, is it reasonable to have a thousands of listeners, and does that introduce bottlenecks in any way?
Same concern goes for registering listeners: when switching views (i.e. from looking at all tracks, to looking at tracks of a specific playlist), thousands of listeners would
unlistenFn, and thousands more wouldlisten, all at the same time. Is that unwise?If this is OK to do, I am also wondering: is having unique event names VS a single event with payload data the better option? For example, is a thousand listeners listening to
track-tags-updated-id1,track-tags-updated-id2etc. better than a thousand listeners all listening totrack-tags-updatedsending a payload of i.e.{ id: 1 }the better option performance wise?Thank you!
All reactions