Plugin Performance
Plugins start and run callbacks in the same process as the player. Slow work can delay the UI and playback events, especially when several plugins start automatically with the player.
Keep Startup Short
Use start() for the registrations and lightweight state setup a plugin needs to enable. The player currently starts plugins on the UI thread. Reading many files, waiting for the network, scanning directories, parsing large files, or performing complex calculations synchronously can make the interface unresponsive. Keep this work out of constructors, property initializers, and static initializers as well.
Choose a thread based on the work:
- Run file access, network requests, and other blocking operations on an I/O dispatcher or pool, such as Kotlin's
Dispatchers.IOor a Java I/O thread pool - Run parsing, indexing, sorting, and other CPU-intensive work on a background computation dispatcher, such as
Dispatchers.Defaultor a bounded thread pool - Switch to the UI framework's required thread when accessing the UI or its state, and hand off prepared results where possible
Return from start() while optional background work runs. Provide basic functionality up front and update state when results arrive. Waiting with join(), Future.get(), Thread.sleep(), or runBlocking during startup or in a UI callback can still block the UI thread.
Registration timing
Register shortcuts on the thread running start(). Hooks must also follow their registration lifecycle and permission requirements. Complete the required registrations on the appropriate thread before starting independent, time-consuming work in the background. See Plugin Permissions and Direct Hook API.
Manage Background Tasks
Create a plugin-owned task scope or executor each time the plugin is enabled. In stop(), cancel its tasks, stop accepting work, and release resources owned by the plugin. Plugins can be enabled and disabled repeatedly. Use a fresh scope or executor each time, and prevent results from an earlier run from overwriting current state.
Cancellation may take time when a task is blocked on I/O or a third-party call. Set network timeouts and check for cancellation in long loops. Before applying results, check that the plugin is still in the same enabled session and that the results are still relevant. Synchronize the check and state update to avoid races with shutdown. Keep any waiting in stop() bounded.
In Kotlin, use the appropriate dispatcher for blocking calls even in a suspend function. If you catch exceptions, propagate CancellationException and handle other failures from background tasks. With Java thread pools, handle task failures, cancellation, and interruption, and shut down the pool when the plugin stops.
Keep Event and Hook Callbacks Lightweight
The thread used for a callback depends on its API. Method hooks run on the target method's thread, and UI hooks run during Compose composition. Keep both lightweight. Avoid scanning files, parsing large inputs, or making network requests on every frequent callback, such as playback position updates.
- Prepare reusable results ahead of time and limit callback work to necessary reads and state updates
- Coalesce suitable updates by keeping the latest value, throttling, or batching. Preserve individual events when each one matters
- Keep hook arguments within the synchronous invocation that owns them. For asynchronous work, copy only the data you need and are allowed to keep
- Complete callbacks that return a result synchronously on a thread allowed by their contract. The two lyrics-loading callbacks already run on I/O threads, and their work still needs time limits
Configuration listeners can also run off the UI thread. Switch to the thread required by the framework before updating the UI. See Plugin Configuration and Direct Hook API for callback lifetimes, threading, and registration rules.
Measure and Verify
Measure total start() time, individual slow initialization steps, and high-frequency callbacks locally. Java and Kotlin can use System.nanoTime() for elapsed time. Avoid logging every high-frequency callback, as logging can skew your results.
Test whether the player responds promptly after startup, and compare behavior before and after enabling the plugin. Test both first startup and re-enabling, as well as slow networks, missing files, failed tasks, immediate disablement, and repeated enable and disable cycles. Background tasks should cancel or discard stale results, leaving no stray threads, listeners, or duplicate registrations.
The host may also load classes and install hooks during startup, especially when it uses a capability for the first time. Plugin timings can narrow down where time is spent. Use a profiler and repeat measurements if a single startup log leaves gaps.
See also Developer Quickstart, Plugin Configuration, Plugin Permissions, and Direct Hook API.
