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

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..536e82f6a182252eedf497bac96f742b34197ff5 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,29 @@ 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()) {
+ _runCallbacks.add(callback);
+ if (_runCallbacks.length == 1) {
floitsch 2013/02/25 18:02:33 Add comment explaining that this is an optimizatio
Anders Johnsen 2013/06/11 12:14:31 Done.
+ 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));
floitsch 2013/02/25 18:02:33 runCallbacks
Anders Johnsen 2013/06/11 12:14:31 Done.
+ _runCallbacks.addAll(runCallbacks);
floitsch 2013/02/25 18:02:33 newCallbacks
Anders Johnsen 2013/06/11 12:14:31 Done.
+ 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