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

Unified Diff: sdk/lib/core/duration.dart

Issue 715233006: Redo "Fast path constructor for Duration" (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 1 month 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 | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/core/duration.dart
diff --git a/sdk/lib/core/duration.dart b/sdk/lib/core/duration.dart
index c081422ba850ca27423e0c5582c6bf96c2c9a7af..402d289e12d0c7414b434d6b970e2a32b863c824 100644
--- a/sdk/lib/core/duration.dart
+++ b/sdk/lib/core/duration.dart
@@ -92,19 +92,24 @@ class Duration implements Comparable<Duration> {
int seconds: 0,
int milliseconds: 0,
int microseconds: 0})
- : _duration = days * MICROSECONDS_PER_DAY +
- hours * MICROSECONDS_PER_HOUR +
- minutes * MICROSECONDS_PER_MINUTE +
- seconds * MICROSECONDS_PER_SECOND +
- milliseconds * MICROSECONDS_PER_MILLISECOND +
- microseconds;
+ : this._microseconds(
+ days * MICROSECONDS_PER_DAY +
+ hours * MICROSECONDS_PER_HOUR +
+ minutes * MICROSECONDS_PER_MINUTE +
+ seconds * MICROSECONDS_PER_SECOND +
+ milliseconds * MICROSECONDS_PER_MILLISECOND +
+ microseconds);
+
+ // Fast path internal direct constructor to avoids the optional arguments and
+ // [_microseconds] recomputation.
+ const Duration._microseconds(this._duration);
/**
* Adds this Duration and [other] and
* returns the sum as a new Duration object.
*/
Duration operator +(Duration other) {
- return new Duration(microseconds: _duration + other._duration);
+ return new Duration._microseconds(_duration + other._duration);
}
/**
@@ -112,7 +117,7 @@ class Duration implements Comparable<Duration> {
* returns the difference as a new Duration object.
*/
Duration operator -(Duration other) {
- return new Duration(microseconds: _duration - other._duration);
+ return new Duration._microseconds(_duration - other._duration);
}
/**
@@ -123,7 +128,7 @@ class Duration implements Comparable<Duration> {
* 53 bits, precision is lost because of double-precision arithmetic.
*/
Duration operator *(num factor) {
- return new Duration(microseconds: (_duration * factor).round());
+ return new Duration._microseconds((_duration * factor).round());
}
/**
@@ -136,7 +141,7 @@ class Duration implements Comparable<Duration> {
// By doing the check here instead of relying on "~/" below we get the
// exception even with dart2js.
if (quotient == 0) throw new IntegerDivisionByZeroException();
- return new Duration(microseconds: _duration ~/ quotient);
+ return new Duration._microseconds(_duration ~/ quotient);
}
/**
@@ -248,9 +253,7 @@ class Duration implements Comparable<Duration> {
}
if (inMicroseconds < 0) {
- Duration duration =
- new Duration(microseconds: -inMicroseconds);
- return "-$duration";
+ return "-${-this}";
}
String twoDigitMinutes = twoDigits(inMinutes.remainder(MINUTES_PER_HOUR));
String twoDigitSeconds = twoDigits(inSeconds.remainder(SECONDS_PER_MINUTE));
@@ -274,7 +277,7 @@ class Duration implements Comparable<Duration> {
* The returned `Duration` has the same length as this one, but is always
* positive.
*/
- Duration abs() => new Duration(microseconds: _duration.abs());
+ Duration abs() => new Duration._microseconds(_duration.abs());
/**
* Returns a new `Duration` representing this `Duration` negated.
@@ -282,5 +285,5 @@ class Duration implements Comparable<Duration> {
* The returned `Duration` has the same length as this one, but will have the
* opposite sign of this one.
*/
- Duration operator -() => new Duration(microseconds: -_duration);
+ Duration operator -() => new Duration._microseconds(-_duration);
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698