From 0343daf64ac0dba631629f87ea1f41d1ac4294aa Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Mon, 9 Nov 2026 18:56:22 +0200 Subject: [PATCH] Compress at most two images at once when posting (#12362) Co-authored-by: Claude Opus 5.5 --- src/lib/async/limit.test.ts | 68 +++++++++++++++++++++++++++++++++ src/lib/async/limit.ts | 33 ++++++++++++++++ src/lib/media/image/compress.ts | 38 +++++++++++++++--- 3 files changed, 133 insertions(+), 6 deletions(-) create mode 100644 src/lib/async/limit.test.ts create mode 100644 src/lib/async/limit.ts diff --git a/src/lib/async/limit.test.ts b/src/lib/async/limit.test.ts new file mode 100644 index 000000000..63ebbcfcf --- /dev/null +++ b/src/lib/async/limit.test.ts @@ -0,0 +1,68 @@ +import {describe, expect, it} from '@jest/globals' + +import {createLimit} from './limit' + +function deferred() { + let resolve!: (value: T) => void + let reject!: (reason: unknown) => void + const promise = new Promise((res, rej) => { + resolve = res + reject = rej + }) + return {promise, resolve, reject} +} + +async function flush() { + for (let i = 0; i < 10; i++) { + await Promise.resolve() + } +} + +describe('createLimit', () => { + it('runs at most `concurrency` tasks at once, in order', async () => { + const limit = createLimit(2) + const tasks = [0, 1, 2, 3, 4].map(() => deferred()) + const started: number[] = [] + + const results = Promise.all( + tasks.map((task, i) => + limit(() => { + started.push(i) + return task.promise + }), + ), + ) + + await flush() + expect(started).toEqual([0, 1]) + + tasks[1].resolve(1) + await flush() + expect(started).toEqual([0, 1, 2]) + + tasks[0].resolve(0) + await flush() + expect(started).toEqual([0, 1, 2, 3]) + + tasks[2].resolve(2) + tasks[3].resolve(3) + await flush() + expect(started).toEqual([0, 1, 2, 3, 4]) + + tasks[4].resolve(4) + await expect(results).resolves.toEqual([0, 1, 2, 3, 4]) + }) + + it('frees the slot when a task rejects', async () => { + const limit = createLimit(1) + const first = deferred() + const error = new Error('failed') + + const a = limit(() => first.promise) + const b = limit(() => Promise.resolve('b')) + + first.reject(error) + await expect(a).rejects.toBe(error) + await expect(b).resolves.toBe('b') + }) +}) diff --git a/src/lib/async/limit.ts b/src/lib/async/limit.ts new file mode 100644 index 000000000..a223b4a8d --- /dev/null +++ b/src/lib/async/limit.ts @@ -0,0 +1,33 @@ +/** + * Returns a function that runs at most `concurrency` tasks at once. Tasks + * started while every slot is busy wait their turn, in the order they were + * started. + */ +export function createLimit(concurrency: number) { + let active = 0 + const waiting: (() => void)[] = [] + + return async function limit(task: () => Promise): Promise { + if (active < concurrency) { + active++ + } else { + // The task that finishes hands its slot straight to us, see below. + await new Promise(resolve => waiting.push(resolve)) + } + + try { + return await task() + } finally { + /* + * Hand the slot over without freeing it first, so a task started in the + * meantime can't take it and push us over the limit. + */ + const next = waiting.shift() + if (next) { + next() + } else { + active-- + } + } + } +} diff --git a/src/lib/media/image/compress.ts b/src/lib/media/image/compress.ts index 1f8b3b51d..61947f350 100644 --- a/src/lib/media/image/compress.ts +++ b/src/lib/media/image/compress.ts @@ -1,12 +1,35 @@ import {SaveFormat} from 'expo-image-manipulator' +import {createLimit} from '#/lib/async/limit' import {moveIfNecessary} from '#/lib/media/image/cache' import {renderImage} from '#/lib/media/image-manipulator' import {type PickerImage} from '#/lib/media/picker.shared' -import {getDataUriSize} from '#/lib/media/util' +import {getUriSize} from '#/lib/media/uriSize' import {type ComposerImage} from '#/state/gallery' -export async function compressImage( +/* + * Each image being compressed holds full-size decoded bitmaps while it renders + * (about 48 MB each for a 12 MP photo on iOS), and iOS starts decoding as + * soon as a manipulation is created. Compressing a 10-image gallery all at + * once is enough to get the app killed for memory (APP-3190). iOS encodes on a + * single serial queue anyway, so running more than two at once buys little. + */ +const MAX_CONCURRENT_COMPRESSIONS = 2 +const limit = createLimit(MAX_CONCURRENT_COMPRESSIONS) + +/** + * Compresses an image to fit within `maxDimension` and `maxSize`. At most + * {@link MAX_CONCURRENT_COMPRESSIONS} images are compressed at once across the + * app, so callers can start them all in parallel. + */ +export function compressImage( + img: ComposerImage, + opts: {maxDimension: number; maxSize: number}, +): Promise { + return limit(() => compressImageNow(img, opts)) +} + +async function compressImageNow( img: ComposerImage, {maxDimension, maxSize}: {maxDimension: number; maxSize: number}, ): Promise { @@ -60,13 +83,16 @@ export async function compressImage( { compress: qualityPercentage / 100, format: SaveFormat.JPEG, - base64: true, }, ) - const base64 = res.base64 - const size = base64 ? getDataUriSize(base64) : 0 - if (base64 && size <= maxBytes) { + /* + * Measure the written file rather than requesting base64, which builds a + * base64 copy of every pass and sends it to JS (Android even encodes the + * JPEG a second time to do it). + */ + const size = await getUriSize(res.uri) + if (size <= maxBytes) { minQualityPercentage = qualityPercentage newDataUri = { path: await moveIfNecessary(res.uri), -- knotmirror 0.0.1