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

Unified Diff: sdk/lib/async/timer.dart

Issue 12316103: Introduce optimized Timer.run queue for immediate runs. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/isolate/timer_cancel_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/async/timer.dart
diff --git a/sdk/lib/async/timer.dart b/sdk/lib/async/timer.dart
index 39fe4c2df6202e1500940372e47d2583085f16b8..bc1b9b6df889b03bca4a36d5e42cf9a0c7708b7d 100644
--- a/sdk/lib/async/timer.dart
+++ b/sdk/lib/async/timer.dart
@@ -5,6 +5,9 @@
part of dart.async;
abstract class Timer {
+ // Internal list used to group Timer.run callbacks.
+ static List _runCallbacks = [];
+
/**
* Creates a new timer.
*
@@ -47,12 +50,31 @@ abstract class Timer {
/**
* Runs the given [callback] asynchronously as soon as possible.
- *
- * Returns a [Timer] that can be cancelled if the callback is not necessary
- * anymore.
*/
- static Timer run(void callback()) {
- return new Timer(const Duration(), callback);
+ static void run(void callback()) {
+ // Optimizing a group of Timer.run callbacks to be executed in the
+ // same Timer callback.
+ _runCallbacks.add(callback);
+ if (_runCallbacks.length == 1) {
+ new Timer(const Duration(milliseconds: 0), () {
+ List runCallbacks = _runCallbacks;
+ // Create new list to make sure we don't call newly added callbacks in
+ // this event.
+ _runCallbacks = [];
+ for (int i = 0; i < runCallbacks.length; i++) {
+ Function callback = runCallbacks[i];
+ try {
+ callback();
+ } catch (e) {
+ List newCallbacks = _runCallbacks;
+ _runCallbacks = [];
+ _runCallbacks.addAll(runCallbacks.skip(i + 1));
+ _runCallbacks.addAll(newCallbacks);
+ throw;
+ }
+ }
+ });
+ }
}
/**
« no previous file with comments | « no previous file | tests/isolate/timer_cancel_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698