From 58c30901e84572314513f00a16e1b07befd5ca0b Mon Sep 17 00:00:00 2001 From: davotoula Date: Thu, 9 Apr 2026 09:21:30 +0200 Subject: [PATCH] perf: run GIF-to-MP4 conversion on Dispatchers.Default The conversion pipeline is overwhelmingly CPU/GPU bound (Movie decode, GL rendering, MediaCodec encode) and can run for several seconds on a large GIF. Running it on Dispatchers.IO occupies a thread from the large IO pool with no kernel wait, which can starve legitimate IO coroutines when multiple uploads run in parallel. Switching to Dispatchers.Default caps concurrent conversions to the CPU count, which is also desirable given the hardware encoder contention that multiple simultaneous MediaCodec instances would cause. convertInternal remains a plain (non-suspending) function, so EGL thread-affinity is still preserved for the lifetime of the call. Co-Authored-By: Claude Opus 4.6 --- .../amethyst/service/uploads/GifToMp4Converter.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/uploads/GifToMp4Converter.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/uploads/GifToMp4Converter.kt index 6edbab0b7d..d523ebea9d 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/uploads/GifToMp4Converter.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/uploads/GifToMp4Converter.kt @@ -105,7 +105,13 @@ object GifToMp4Converter { uri: Uri, context: Context, ): MediaCompressorResult? = - withContext(Dispatchers.IO) { + // Dispatchers.Default: the bulk of this work is CPU/GPU bound + // (Movie decode, GL rendering, MediaCodec encode). Running on IO + // would occupy a thread from the large IO pool for several seconds + // with no kernel wait, risking starvation of real IO coroutines. + // The brief file read at the start and muxer writes are acceptable + // on Default — they're short relative to the encoding loop. + withContext(Dispatchers.Default) { try { convertInternal(uri, context) } catch (e: CancellationException) {