flutter_avif icon indicating copy to clipboard operation
flutter_avif copied to clipboard

Dart copy performance is about as long as avif decoding. Ouch.

Open markg85 opened this issue 9 months ago • 0 comments

Hi,

Image

I was just curious about some performance aspect of flutter avif and did some profiling. It's hard in flutter to get an accurate measurement.. What you see here is just where my app loads like 40 images of about 500x400.

The slow part here is protobuf with it's _copyInto function, it does this:

  /// Copy bytes from the given typed data array into the output buffer.
  ///
  /// Has a specialization for Uint8List for performance.
  int _copyInto(Uint8List buffer, int pos, TypedData value) {
    if (value is Uint8List) {
      final len = value.length;
      for (var j = 0; j < len; j++) {
        buffer[pos++] = value[j];
      }
      return pos;
    } else {
      final len = value.lengthInBytes;
      final u8 = Uint8List.view(
          value.buffer, value.offsetInBytes, value.lengthInBytes);
      for (var j = 0; j < len; j++) {
        buffer[pos++] = u8[j];
      }
      return pos;
    }
  }

A byte by byte copy, yeah that's the slow naive memcopy implementation in any language. They do know about this.

This issue is just to let you know. Perhaps there's something you can do on the library side to make things more efficient? You might be interested in this recent talk about FFI in Dart where it's compared to a couple different languages. There too dart was slow - and remained the loser compared to the others - but there a massive optimization was to use Float64List instead of List<double> to make dart approach other languages.

Just a FYI :)

markg85 avatar Feb 26 '25 17:02 markg85