From cd9bce1a96292994ac32ddeec8e48b4a86165ef7 Mon Sep 17 00:00:00 2001 From: Tom Brow Date: Fri, 13 Nov 2026 10:57:38 -0800 Subject: [PATCH] refactor(mobile): drop the picker's decoded image cache Flutter's memory and disk caches already serve the bytes without a download; decoding an 84 px emoji again is cheap. Co-authored-by: studio Signed-off-by: Tom Brow --- mobile/ios/Runner/NativeEmojiPicker.swift | 1 - mobile/ios/Runner/NativeEmojiPickerView.swift | 39 ++----------------- 2 files changed, 4 insertions(+), 36 deletions(-) diff --git a/mobile/ios/Runner/NativeEmojiPicker.swift b/mobile/ios/Runner/NativeEmojiPicker.swift index d8d7342652..2c2a1bef5f 100644 --- a/mobile/ios/Runner/NativeEmojiPicker.swift +++ b/mobile/ios/Runner/NativeEmojiPicker.swift @@ -91,7 +91,6 @@ final class NativeEmojiPickerCoordinator: NSObject, // A sheet is already owned by an earlier caller; report busy rather than a // successful presentation so the caller does not treat this as its own. guard presentedController == nil else { return false } - NativeEmojiRemoteImageCache.reset() guard let data = NativeEmojiPickerDataLoader.load(arguments: arguments), let presenter = topViewController( diff --git a/mobile/ios/Runner/NativeEmojiPickerView.swift b/mobile/ios/Runner/NativeEmojiPickerView.swift index d0dc75eda4..778eb76bb1 100644 --- a/mobile/ios/Runner/NativeEmojiPickerView.swift +++ b/mobile/ios/Runner/NativeEmojiPickerView.swift @@ -432,7 +432,7 @@ struct NativeEmojiRemoteImage: View { } .task(id: url) { do { - phase = .success(try await NativeEmojiRemoteImageCache.image(for: url)) + phase = .success(try await NativeEmojiRemoteImageLoader.image(for: url)) } catch { if !Task.isCancelled { phase = .failure } } @@ -461,52 +461,21 @@ private struct NativeEmojiAnimatedImage: UIViewRepresentable { } } -/// Decoded custom emoji, keyed by URL, so scrolling back within one picker -/// presentation doesn't decode again. Flutter owns the bytes and their memory -/// and disk caches, which it scopes to the signed-in identity, so this cache is -/// emptied each time the picker opens rather than outliving an account switch. -enum NativeEmojiRemoteImageCache { - private static let cache: NSCache = { - let cache = NSCache() - cache.totalCostLimit = 32 * 1024 * 1024 - return cache - }() - +/// Custom emoji, decoded at the picker's glyph size. Flutter serves the bytes +/// from its memory and disk caches, so a reopen never downloads again. +enum NativeEmojiRemoteImageLoader { /// The picker's 28-point custom emoji glyph at 3x. private static let maximumPixelSize = 84 - /// Bumped by `reset()` so a decode from an earlier presentation is not cached. - @MainActor private static var generation = 0 - - @MainActor - static func reset() { - generation += 1 - cache.removeAllObjects() - } - @MainActor static func image(for url: URL) async throws -> UIImage { - let key = url.absoluteString as NSString - if let cached = cache.object(forKey: key) { return cached } - let started = generation let bytes = try await NativeEmojiPickerCoordinator.mediaBytes(for: url) let decoded = await Task.detached(priority: .userInitiated) { NativeMessageImageLoader.decode(bytes, maximumPixelSize: maximumPixelSize) }.value guard let decoded else { throw NativeEmojiRemoteImageError.invalidImage } - if generation == started { - cache.setObject(decoded, forKey: key, cost: cost(of: decoded)) - } return decoded } - - private static func cost(of image: UIImage) -> Int { - let frames = image.images ?? [image] - return frames.reduce(0) { total, frame in - guard let cgImage = frame.cgImage else { return total } - return total + cgImage.bytesPerRow * cgImage.height - } - } } enum NativeEmojiRemoteImageError: Error {