2

I am trying to use Flutters compute function to do some real time heavy image processing using a C++ code and dart ffi.
I tried wrapping the call to the heavy function in a compute to avoid messing with the ui thread and I took some time measurements to see what takes the most time to execute.

the code looks like this:

double _work(CheckPhotoData p) {
  DateTime s = DateTime.now();
  Pointer<Double> rPointer = Pointer.fromAddress(p.rPointerAddress);
  Pointer<Double> gPointer = Pointer.fromAddress(p.gPointerAddress);
  Pointer<Double> bPointer = Pointer.fromAddress(p.bPointerAddress);
  final a =  NativeCCode.checkPhoto(rPointer, gPointer, bPointer, p.w, 1);
  print("ACTUAL NativeCCode.checkPhoto took: " + DateTime.now().difference(s).inMilliseconds.toString());
  return a;
}

class CheckPhotoWrapper {
  static Future<double> checkPhotoWrapper(Uint8List photo) async {
    final CheckPhotoData deconstructData = _deconstructData(photo);
    DateTime s = DateTime.now();
    double res = await compute(_work, deconstructData);
    print("compute took: " + DateTime.now().difference(s).inMilliseconds.toString());
    return res;
  }
  ...
}

After running the code I got this output:

ACTUAL NativeCCode.checkPhoto took: 106
compute took: 514

(this means that compute took 408ms more than the code it runs)

From what I understand from these results, the actual compute method from dart:async is taking much more time then the actual code its executing and causes a big overhead impacting the performance.
Even worse, my app UI is stuck when the processing starts.

Is there a way to reduce the overhead that compute introduces or a different approach this issue that I couldn't figure out?

Thanks for any idea or a solution to my problem.

Note:

  • I ran the test on debug mode on a physical device.
  • CheckPhotoData is a simple class containing the parameters to my _work function.
  • I am using flutter version 2.2.3, Channel stable

1 Answers1

3

The overhead seems to be caused by debug mode. I saw a similar compute delay of several hundred milliseconds in my app (using Flutter 2.10.2), but when running in release mode it's less than 10 milliseconds.

bnenning
  • 106
  • 2
  • I also have immense UI frame drops when using compute() somewhere in an un-awaited future. But in release mode everything is fine. Does anybody know where that behaviour comes from? – JohnnyRainbow Jul 03 '23 at 09:31