Skip to content

Lose generic information across isolate #40919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gaaclarke opened this issue Mar 7, 2020 · 5 comments
Closed

Lose generic information across isolate #40919

gaaclarke opened this issue Mar 7, 2020 · 5 comments
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-isolate type-enhancement A request for a change that isn't a bug

Comments

@gaaclarke
Copy link
Contributor

code:

import 'dart:isolate';

void _process<T, U>(SendPort sendPort) async {
  var receivePort = new ReceivePort();
  sendPort.send(receivePort.sendPort);
  U Function(T input) mapper;
  await for (dynamic input in receivePort) {
    if (mapper == null) {
      mapper = input;
    } else {
      sendPort.send(mapper(input as T));
    }
  }
}

Stream<U> pmap<T, U>(List<T> list, U Function(T input) mapper, {int parallel = 1}) async* {
  ReceivePort receivePort = new ReceivePort();
  Isolate isolate = await Isolate.spawn(_process, receivePort.sendPort);
  SendPort sendPort;
  await for (dynamic result in receivePort) {
    if (sendPort == null) {
      sendPort = result;
      sendPort.send(mapper);
      for (T item in list) {
        sendPort.send(item);
      }
    } else {
      yield result;
    }
  }
  isolate.kill();
}

int mapper(int x) => x * x;

void main() async {
  List<int> foo = [1, 2, 3, 4];
  Stream<int> results = pmap(foo, mapper);
  await for (int value in results) {
    print(value);
  }
}

Runtime exception:
type '(int) => int' is not a subtype of type '(dynamic) => dynamic' is generated when we execute mapper = input;

Expected:
Ideally I could say something like: Isolate.spawn(_process<T, U>, receivePort.sendPort); so I wouldn't lose that type information when the function is executed in the other isolate.

@lrhn lrhn added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-isolate type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) type-enhancement A request for a change that isn't a bug and removed type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Mar 9, 2020
@lrhn
Copy link
Member

lrhn commented Mar 9, 2020

There is currently no way to specify a non-constant function reference as the isolate entry point. That means that _process<T, U> cannot be sent, only the reference to the generic _process function.
So, when the new isolate needs to call that function, it defaults to instantiating it to _process<dynamic, dynamic>.

What you can do is to send the function as data instead of as entry point.

class _Generic2<T, U, P> {
  final P parameter;
  final void Function<T, U>(P) function;
  _Generic2(this.function, this.parameter);
  void call() {
    function<T, U>(parameter);  
  }
  static void entry(_Generic2 parameters) {
    parameters();
  }
}
...

  Isolate.spawn(_Generic2.entry, 
      _Generic2<T, U, SendPort>(_process, receivePort.sendPort));

@PlugFox
Copy link

PlugFox commented Mar 19, 2021

What you can do is to send the function as data instead of as entry point.

Works great and solves all problems! Thank you very much!

@mkustermann
Copy link
Member

We'll solve this issue as part of #36097.

@PlugFox
Copy link

PlugFox commented Mar 19, 2021

We'll solve this issue as part of #36097.

We are waiting for the addition of this feature (Lightweight Isolates and Isolate group), it will make possible to use isolates more often!
Thank you so much!

Maybe common interface with the web (web/service workers) and I am in paradise during my lifetime)

@mkustermann
Copy link
Member

Closing this issue, merge into the more general #46623.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-isolate type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants