Skip to content
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

Have better typed code in dart:async's zone implementation #41253

Closed
mkustermann opened this issue Mar 30, 2020 · 3 comments
Closed

Have better typed code in dart:async's zone implementation #41253

mkustermann opened this issue Mar 30, 2020 · 3 comments
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-async type-enhancement A request for a change that isn't a bug

Comments

@mkustermann
Copy link
Member

Right now completing a completer calls _Future.handleValue

  FutureOr<T> handleValue(S sourceResult) {
    return _zone.runUnary<FutureOr<T>, S>(_onValue, sourceResult);
  }

Which calls _CustomZone.runUnary:

  R runUnary<R, T>(R f(T arg), T arg) {
    var implementation = this._runUnary;
    assert(implementation != null);
    ZoneDelegate parentDelegate = _parentDelegate(implementation.zone);
    RunUnaryHandler handler = implementation.function;
    return handler(implementation.zone, parentDelegate, this, f, arg);
  }

Which accesses the _CustomZone._runUnary callback:

class _CustomZone {
  _ZoneFunction<Function> _runUnary;
  ...;
}

class _ZoneFunction<T extends Function> {
  final _Zone zone;
  final T function;
  const _ZoneFunction(this.zone, this.function);
}

Notice above that RunUnaryHandler handler = implementation.function will cause us to perform a runtime type cast against RunUnaryHandler:

typedef RunUnaryHandler = R Function<R, T>(
    Zone self, ZoneDelegate parent, Zone zone, R Function(T arg) f, T arg);

@lrhn Can we avoid these hot runtime type checks in the async/zone implementation?

@mkustermann mkustermann added the area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. label Mar 30, 2020
@mkustermann
Copy link
Member Author

/cc @mraleph

@lrhn
Copy link
Member

lrhn commented Mar 30, 2020

Maybe!

The problem is that the correct type is ZoneFunction<R Function<R, T>(T)>, but you cannot have generic function types as type arguments (dart-lang/language#496).
So, we cannot just correct the type.

Maybe we can make a specific class, UnaryZoneFunction, with the exact same signatures as ZoneFunction<R Function<R, T>(T)> and use that instead of ZoneFunction.
We'll need to do the same for run, runBinary, bind, bindUnary, bindBinary, etc. (maybe some can be reused, I'm guessing runUnary and bindUnary will have the same signatures).

@lrhn lrhn added library-async type-enhancement A request for a change that isn't a bug labels Mar 30, 2020
@lrhn
Copy link
Member

lrhn commented Mar 31, 2020

Proof-of-concept code: https://dart-review.googlesource.com/c/sdk/+/141884 (completely untested yet).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-async type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

2 participants