From eed03994e46b8732feaa2ec7a65b8e643a336adb Mon Sep 17 00:00:00 2001 From: "sra@google.com" Date: Wed, 12 Nov 2014 21:42:14 +0000 Subject: [PATCH] Redo "Fast path constructor for Duration" TBR=lrn@google.com BUG= Review URL: https://codereview.chromium.org//715233006 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@41699 260f80e4-7a28-3924-810f-c04153c831b5 --- sdk/lib/core/duration.dart | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/sdk/lib/core/duration.dart b/sdk/lib/core/duration.dart index c081422ba85..402d289e12d 100644 --- a/sdk/lib/core/duration.dart +++ b/sdk/lib/core/duration.dart @@ -92,19 +92,24 @@ class Duration implements Comparable { 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 { * 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 { * 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 { // 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 { } 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 { * 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 { * 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); }