Dart isolate is very slow when running a function that returns a very long list
Asked Answered
H

0

6

In my Flutter project for Windows, I use the compute function to run some code in an Isolate, in order to avoid freezing the user interface.

But I'm facing a problem: the code I run in the Isolate returns a very long list of double (a few millions of items), and because of that, the overall execution takes a ridiculously big amount of time, compare to if I don't use Isolate at all.

Here is the function that makes many calculations and returns a big list:

static Future<List<double>> extractWavSamples(File fileWav) async
{
    // read the audio file:
    Uint8List bytes = await fileWAV.readAsBytes();
    bytes = bytes.sublist(44);

    // convert to a list of doubles:
    List<double> samples = [];
    Uint8List samplesBytes = Uint8List(2);
    int sample = 0;
    for (int a = 0 ; a < bytes.length - 2 ; a += 2)
    {
      samplesBytes[0] = bytes.sublist(a + 1, a + 2)[0];
      samplesBytes[1] = bytes.sublist(a, a + 1)[0];
      sample = ByteData.view(samplesBytes.buffer).getInt16(0);
      samples.add(sample / 32768.0);
    }
    return samples;
}

Here is how I run that function without compute (it takes 5-6 seconds to run, but it freezes the user interface):

List<double> samples = await extractWavSamples(myFileWav);

Here is how I run that function with compute (it takes about 20 seconds to run):

List<double> samples = await compute(extractWavSamples, myFileWav);

And I found out that, because the extractWavSamples() function returns a big list, that's why the code takes a way too long time to run. If I make it return an empty list, it's much faster.

Tha thing is: I need that list in the main thread, so I can display it (it allows me to display the waveform of the audio file).

So what should I fix to make the code work in a reasonable amount of time?

Thanks.

Hip answered 27/9, 2021 at 10:1 Comment(6)
Did you find a solution?Byrann
Did you try this again with the new “lightweight isolates”?Byrann
@TobiasMarschall Not yet, worth a try though, thanks!Hip
What is a "lightweight isolate"? Can you please elaborate if this is an actual new feature, library or what? I ask because I'm having trouble with slow isolates too. They are far too slow starting up.Aney
No, I haven't work on that yet.Hip
I know that's too old , but i want to hint a suggestion for every one has the same issue ... you can send a data in a small chunks (example: every 10 iterations as a chunk, and keep joining all that lists in one big list in the main isolate), sending a one big list at once is not a good way to do it and it well lead to performance issues.Bracey

© 2022 - 2024 — McMap. All rights reserved.