Improve application performance with isolate

Issue

I have an application that generate hashed password and to generate it takes time. I think to improve performance, I would let hashed password generator work in a seperate core. My computer support 3 core processors and I think it is a good idea to use dart:isolate for calculate hashed password in other processor core.

I have tried following:

import 'dart:isolate';
import 'package:dbcrypt/dbcrypt.dart';

main() {

  ReceivePort receivePort = new ReceivePort();
  var receivePortPw = new ReceivePort();

  receivePortPw.listen((msg) {
     print(msg); 
  });

  Isolate.spawn(ReturnHashedPassword, receivePortPw.sendPort);
  print('Print1 -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));  
  print('Print2 -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));  

}

void ReturnHashedPassword(SendPort sendPort)
{
    ReceivePort receivePort = new ReceivePort();
    sendPort.send('Isolate -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));    
}

as output i have got

Print1 -> $2a$10$XggGPuIyLP2GLon2eKtW2.kG5QwK4fkiIDFa8hkgDPdy1h1AAC6LO
Print2 -> $2a$10$zK..L6Hi0NkeRbkm2/v6H.5s25QQSjwRszI83.i3CzFZlb7pFCW6G
Isolate -> $2a$10$DM/.25em/3amvGNu2G6Wl.SQQ2ECGSE6DUwPc56tvdoMGw9ZBja36

It seems be, it is not working concurrency. I expected, that isolate will be the first place or second place not the last. What do i wrong here?

Solution

Your code is working concurrently. When you add a print() to your Isolate’s function like this:

void ReturnHashedPassword(SendPort sendPort)
{
    print('ok');
    ReceivePort receivePort = new ReceivePort();
    sendPort.send('Isolate -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));
    print('done');
}

The output will be:

ok
done
Print1 -> $2a$10$p..GSYxybtjmbrhKe.xu1.IfEUihBxXPL9DYCLHqx72yKHsZB0e1e
Print2 -> $2a$10$eA.2bNvakH6uBFjiWNwua.jDUBhgYPsMP2PyOpsGd84GCx.spaAS.
Isolate -> $2a$10$.sBmleeuV5U.NaSGOE6ON.kxQ7Cnq6yj8IXRBgCZgx8TGmcBZT7Ny

I guess dart has some inbuilt resource allocation algorithm that gives stdout to processes to avoid weird prints.

When you change your code like this:

import 'dart:isolate';
import 'package:dbcrypt/dbcrypt.dart';
import 'dart:async';

main() {

  //ReceivePort receivePort = new ReceivePort();
  var receivePortPw = new ReceivePort();
  receivePortPw.listen((msg) {
     print(msg); 
  });


  Future<Isolate> f = Isolate.spawn(ReturnHashedPassword, receivePortPw.sendPort);
  f.then((Isolate i) {
    print('Print1 -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));
    print('Print2 -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));
  });
}

void ReturnHashedPassword(SendPort sendPort)
{
    print('ok');
    ReceivePort receivePort = new ReceivePort();
    sendPort.send('Isolate -> ' + new DBCrypt().hashpw('Password', new DBCrypt().gensalt()));
    print('done');
}

The output is:

ok
Print1 -> $2a$10$zABOnUhKUn.GqERW2Euu7.HpzNizwTDyDbSSLe0b1XL6o9jEo/4dm
done
Print2 -> $2a$10$SE.eczx2i1o2dfey.NpSI.gZXhJU9KDWPAp1UtOFBjUI/ltjppwy2
Isolate -> $2a$10$s.B.0dnGQ0KO1za..I5uL.U1ARKLUK/Jtv/.O8BjP7gQroidvesEC

You can see that it is working concurrent.

Regards, Robert

Answered By – Robert

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *