1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Flutter compute function takes time to start execute

Discussão em 'Mobile' iniciado por Stack, Outubro 16, 2024 às 09:02.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página