Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(551)

Side by Side Diff: sdk/lib/async/future_impl.dart

Issue 136113014: Introduce and use Zone:_enter and Zone:_leave, in Future. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Make futures faster and add a simple test. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | sdk/lib/async/zone.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of dart.async; 5 part of dart.async;
6 6
7 /** The onValue and onError handlers return either a value or a future */ 7 /** The onValue and onError handlers return either a value or a future */
8 typedef dynamic _FutureOnValue<T>(T value); 8 typedef dynamic _FutureOnValue<T>(T value);
9 /** Test used by [Future.catchError] to handle skip some errors. */ 9 /** Test used by [Future.catchError] to handle skip some errors. */
10 typedef bool _FutureErrorTest(var error); 10 typedef bool _FutureErrorTest(var error);
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 return; 404 return;
405 } 405 }
406 if (listeners == null) return; 406 if (listeners == null) return;
407 _Future listener = listeners; 407 _Future listener = listeners;
408 if (listener._nextListener != null) { 408 if (listener._nextListener != null) {
409 // Usually futures only have one listener. If they have several, we 409 // Usually futures only have one listener. If they have several, we
410 // handle them specially. 410 // handle them specially.
411 _propagateMultipleListeners(source, listeners); 411 _propagateMultipleListeners(source, listeners);
412 return; 412 return;
413 } 413 }
414 if (hasError && !source._zone.inSameErrorZone(listener._zone)) { 414 Zone zone = listener._zone;
415 if (hasError && !source._zone.inSameErrorZone(zone)) {
415 // Don't cross zone boundaries with errors. 416 // Don't cross zone boundaries with errors.
416 _AsyncError asyncError = source._error; 417 _AsyncError asyncError = source._error;
417 source._zone.handleUncaughtError( 418 source._zone.handleUncaughtError(
418 asyncError.error, asyncError.stackTrace); 419 asyncError.error, asyncError.stackTrace);
419 return; 420 return;
420 } 421 }
421 if (!identical(Zone.current, listener._zone)) { 422 Zone old;
422 // Run the propagation in the listener's zone to avoid 423 if (!identical(Zone.current, zone)) {
423 // zone transitions. The idea is that many chained futures will 424 // Change zone if it's not current.
424 // be in the same zone. 425 old = Zone._enter(zone);
425 listener._zone.run(() {
426 _propagateToListeners(source, listener);
427 });
428 return;
429 } 426 }
430
431 // Do the actual propagation. 427 // Do the actual propagation.
432 // TODO(floitsch): Do we need to go through the zone even if we
433 // don't have a callback to execute?
434 bool listenerHasValue; 428 bool listenerHasValue;
435 var listenerValueOrError; 429 var listenerValueOrError;
436 // Set to true if a whenComplete needs to wait for a future. 430 // Set to true if a whenComplete needs to wait for a future.
437 // The whenComplete action will resume the propagation by itself. 431 // The whenComplete action will resume the propagation by itself.
438 bool isPropagationAborted = false; 432 bool isPropagationAborted = false;
439 // Even though we are already in the right zone (due to the optimization 433 // At this point we are in the right zone. Each callback is invoked
440 // above), we still need to go through the zone. The overhead of 434 // through Zone.run* to be sure to invoke potential callbacks.
441 // executeCallback is however smaller when it is already in the correct 435 // TODO(floitsch): mark the listener as pending completion. Currently
442 // zone. 436 // we can't do this, since the markPendingCompletion verifies that
443 // TODO(floitsch): only run callbacks in the zone, not the whole 437 // the future is not already marked (or chained).
444 // handling code. 438 bool handleValue() {
floitsch 2014/01/28 14:22:45 add new line before "handleValue".
Anders Johnsen 2014/01/29 08:00:28 Done.
445 listener._zone.run(() { 439 var value = source._value;
446 // TODO(floitsch): mark the listener as pending completion. Currently 440 listenerHasValue = true;
447 // we can't do this, since the markPendingCompletion verifies that 441 if (listener._onValue != null) {
448 // the future is not already marked (or chained). 442 try {
449 try { 443 listenerValueOrError = zone.runUnary(listener._onValue, value);
450 if (!hasError) { 444 } catch (e, s) {
451 var value = source._value; 445 listenerValueOrError = new _AsyncError(e, s);
452 if (listener._onValue != null) { 446 listenerHasValue = false;
453 listenerValueOrError = listener._onValue(value); 447 return false;
454 listenerHasValue = true;
455 } else {
456 // Copy over the value from the source.
457 listenerValueOrError = value;
458 listenerHasValue = true;
459 }
460 } else {
461 _AsyncError asyncError = source._error;
462 _FutureErrorTest test = listener._errorTest;
463 bool matchesTest = true;
464 if (test != null) {
465 matchesTest = test(asyncError.error);
466 }
467 if (matchesTest && listener._onError != null) {
468 Function errorCallback = listener._onError;
469 listenerValueOrError = _invokeErrorHandler(errorCallback,
470 asyncError.error,
471 asyncError.stackTrace);
472 listenerHasValue = true;
473 } else {
474 // Copy over the error from the source.
475 listenerValueOrError = asyncError;
476 listenerHasValue = false;
477 }
478 } 448 }
479 449 } else {
480 if (listener._whenCompleteAction != null) { 450 // Copy over the value from the source.
481 var completeResult = listener._whenCompleteAction(); 451 listenerValueOrError = value;
452 }
453 return true;
454 }
455 bool handleError() {
floitsch 2014/01/28 14:22:45 new line before nested function.
Anders Johnsen 2014/01/29 08:00:28 Done.
456 _AsyncError asyncError = source._error;
457 _FutureErrorTest test = listener._errorTest;
458 bool matchesTest = true;
459 if (test != null) {
460 try {
461 matchesTest = zone.runUnary(test, asyncError.error);
462 } catch (e, s) {
463 listenerValueOrError = identical(asyncError.error, e) ?
464 asyncError : new _AsyncError(e, s);
465 listenerHasValue = false;
466 return false;
467 }
468 }
469 if (matchesTest && listener._onError != null) {
470 Function errorCallback = listener._onError;
471 // TODO(ajohnsen): Use either runUnary or runBinary.
floitsch 2014/01/28 14:22:45 if (errorCallback is ZoneBinaryCallback) { liste
Anders Johnsen 2014/01/29 08:00:28 Done.
472 try {
473 listenerValueOrError = zone.run(
474 () => _invokeErrorHandler(errorCallback,
475 asyncError.error,
476 asyncError.stackTrace));
477 } catch (e, s) {
478 listenerValueOrError = identical(asyncError.error, e) ?
479 asyncError : new _AsyncError(e, s);
480 listenerHasValue = false;
481 return false;
482 }
483 listenerHasValue = true;
484 } else {
485 // Copy over the error from the source.
486 listenerValueOrError = asyncError;
487 listenerHasValue = false;
488 }
489 return true;
490 }
491 void handleWhenComplete() {
floitsch 2014/01/28 14:22:45 New line before nested function.
Anders Johnsen 2014/01/29 08:00:28 Done.
492 if (listener._whenCompleteAction != null) {
493 try {
494 var completeResult = zone.run(listener._whenCompleteAction);
482 if (completeResult is Future) { 495 if (completeResult is Future) {
483 listener._isChained = true; 496 listener._isChained = true;
484 completeResult.then((ignored) { 497 completeResult.then((ignored) {
485 // Try again, but this time don't run the whenComplete callback. 498 // Try again, but this time don't run the whenComplete callback.
486 _propagateToListeners(source, listener); 499 _propagateToListeners(source, listener);
487 }, onError: (error, [stackTrace]) { 500 }, onError: (error, [stackTrace]) {
488 // When there is an error, we have to make the error the new 501 // When there is an error, we have to make the error the new
489 // result of the current listener. 502 // result of the current listener.
490 if (completeResult is! _Future) { 503 if (completeResult is! _Future) {
491 // This should be a rare case. 504 // This should be a rare case.
492 completeResult = new _Future(); 505 completeResult = new _Future();
493 completeResult._setError(error, stackTrace); 506 completeResult._setError(error, stackTrace);
494 } 507 }
495 _propagateToListeners(completeResult, listener); 508 _propagateToListeners(completeResult, listener);
496 }); 509 });
497 isPropagationAborted = true; 510 isPropagationAborted = true;
498 } 511 }
512 } catch (e, s) {
floitsch 2014/01/28 14:22:45 The try/catch can just surround the whenCompleteAc
Anders Johnsen 2014/01/29 08:00:28 Done.
513 if (hasError && identical(source._error.error, e)) {
514 listenerValueOrError = source._error;
515 } else {
516 listenerValueOrError = new _AsyncError(e, s);
517 }
518 listenerHasValue = false;
499 } 519 }
500 } catch (e, s) {
501 // Set the exception as error unless the error is the same as the
502 // original one.
503 if (hasError && identical(source._error.error, e)) {
504 listenerValueOrError = source._error;
505 } else {
506 listenerValueOrError = new _AsyncError(e, s);
507 }
508 listenerHasValue = false;
509 } 520 }
510 }); 521 }
522 if (!hasError) {
floitsch 2014/01/28 14:22:45 I think there is no case where there is a value/er
Anders Johnsen 2014/01/29 08:00:28 Sadly, depending on if any error was cough, handle
523 if (handleValue()) {
524 handleWhenComplete();
525 }
526 } else {
527 if (handleError()) {
528 handleWhenComplete();
529 }
530 }
531 // If we changed zone, old will not be null.
532 if (old != null) Zone._leave(old);
511 if (isPropagationAborted) return; 533 if (isPropagationAborted) return;
512 // If the listener's value is a future we need to chain it. 534 // If the listener's value is a future we need to chain it.
513 if (listenerHasValue && listenerValueOrError is Future) { 535 if (listenerHasValue && listenerValueOrError is Future) {
514 Future chainSource = listenerValueOrError; 536 Future chainSource = listenerValueOrError;
515 // Shortcut if the chain-source is already completed. Just continue the 537 // Shortcut if the chain-source is already completed. Just continue the
516 // loop. 538 // loop.
517 if (chainSource is _Future && chainSource._isComplete) { 539 if (chainSource is _Future && chainSource._isComplete) {
518 // propagate the value (simulating a tail call). 540 // propagate the value (simulating a tail call).
519 listener._isChained = true; 541 listener._isChained = true;
520 source = chainSource; 542 source = chainSource;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 } 587 }
566 }, onError: (e, s) { 588 }, onError: (e, s) {
567 if (timer.isActive) { 589 if (timer.isActive) {
568 timer.cancel(); 590 timer.cancel();
569 result._completeError(e, s); 591 result._completeError(e, s);
570 } 592 }
571 }); 593 });
572 return result; 594 return result;
573 } 595 }
574 } 596 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/async/zone.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698