Skip to content

Fix image upload bottleneck - #1039

Open
K-Zawis wants to merge 4 commits into
commetchat:mainfrom
K-Zawis:fix-image-upload-bottleneck
Open

Fix image upload bottleneck#1039
K-Zawis wants to merge 4 commits into
commetchat:mainfrom
K-Zawis:fix-image-upload-bottleneck

Conversation

@K-Zawis

@K-Zawis K-Zawis commented Aug 31, 2026

Copy link
Copy Markdown

Description

This PR addresses a major performance bottleneck during file uploads where pure-Dart decoding caused multi-second UI freezes and high memory allocation that has been plaguing me during my use of Commet (as I mainly use it on mobile). It also patches several fatal crash vectors (NullCheckError, LateInitializationError, and platform exceptions) in both the image and video processing pipelines.

Key Changes

  • Native Image Processing: Integrated flutter_image_compress to utilize hardware-accelerated media pipelines on Android, iOS, and macOS.
  • Memory-Safe Desktop Fallbacks: Implemented a pure-Dart isolate fallback (_fallbackProcessImage) for Linux and Windows that streams image bytes directly from disk (img.decodeImageFile) instead of allocating full payloads in RAM, avoiding OOM spikes.
  • Crash Prevention (Images & Video): Guarded processVideo() against uninitialized controllers/corrupted media, and replaced force-unwrap operators (!) in the image pipeline with safe null checks to prevent unhandled NullCheckErrors on unsupported file types.
  • Dynamic MIME Resolution: Added _resolveMimeType to read header magic numbers via partial file streams, safely resolving missing MIME types upfront and preventing LateInitializationErrors.
  • Future Extensibility: Integrating this native pipeline makes it trivial to surface user-facing quality controls and format options directly in the attachment picker in future updates for supported devices.
  • Build Config: Updated Android ndkVersion to 28.2.13676358 to resolve native C++ plugin toolchain mismatches.

Performance Benchmarks

Tested on a physical Samsung Galaxy S22 Ultra (SM-S908B) processing a standard 12 MP photo (5.41 MB, 3000×4000 resolution):

  • Pure-Dart Software Fallback (package:image): 4,754 ms
  • Native Acceleration (flutter_image_compress): 289 ms
  • Result: 16.4x faster execution (~93.9% time reduction)

(Note: Native C-codec execution via flutter_image_compress_linux was also verified on Linux x86_64, providing a noticeably snappier frame response compared to pure-Dart isolate decoding.)

Initialize `MimeTypeResolver` statically once instead of re-instantiating it on every `lookupType` call. Simplify `lookupType` to directly pass `headerBytes` to `_resolver.lookup`, ensuring magic numbers are evaluated first regardless of whether the file path has an extension. Expose `magicNumbersMaxLength` getter to allow memory-safe, partial file stream reads when determining MIME types.
… and memory-safe fallbacks

Integrate flutter_image_compress for hardware-accelerated image processing on Android, iOS, and macOS, drastically reducing execution time.

Refactor image processing and MIME resolution pipeline:
- Add `_resolveMimeType` to read header magic numbers via partial file streams without allocating full payloads in RAM.
- Use `img.decodeImageFile` in pure-Dart `_fallbackProcessImage` isolate to stream directly from disk for Windows/Linux.
- Guard `processFile()` to resolve missing MIME types upfront and prevent `LateInitializationError` on unsupported attachments.
- Update Android `ndkVersion` in build config to resolve native C++ compilation warnings and plugin toolchain mismatches.
Guard against uninitialized video controllers, add try/catch for platform exceptions on corrupt files, and provide a fallback MIME type for video thumbnails.
@K-Zawis
K-Zawis marked this pull request as draft August 31, 2026 17:48
@K-Zawis
K-Zawis marked this pull request as ready for review August 31, 2026 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant