mirror of
https://github.com/dart-lang/sdk
synced 2024-09-15 23:09:48 +00:00
Fix Stopwatch timers to not overflow on intermediate computations.
With a nanosecond tick counter, the elapsedMicroseconds getter could overflow if a stopwatch runs for only ~2.5 hours. Change-Id: I10cb54bba928713a3127a6bfbd19346b01d775b0 Reviewed-on: https://dart-review.googlesource.com/c/94381 Commit-Queue: Lasse R.H. Nielsen <lrn@google.com> Reviewed-by: Peter von der Ahé <ahe@google.com>
This commit is contained in:
parent
edcaa05375
commit
40bab34fbd
|
@ -374,6 +374,22 @@ class Stopwatch {
|
|||
|
||||
@patch
|
||||
static int _now() => Primitives.timerTicks();
|
||||
|
||||
@patch
|
||||
int get elapsedMicroseconds {
|
||||
int ticks = elapsedTicks;
|
||||
if (_frequency == 1000000) return ticks;
|
||||
assert(_frequency == 1000);
|
||||
return ticks * 1000;
|
||||
}
|
||||
|
||||
@patch
|
||||
int get elapsedMilliseconds {
|
||||
int ticks = elapsedTicks;
|
||||
if (_frequency == 1000) return ticks;
|
||||
assert(_frequency == 1000000);
|
||||
return ticks ~/ 1000;
|
||||
}
|
||||
}
|
||||
|
||||
// Patch for List implementation.
|
||||
|
|
|
@ -197,7 +197,7 @@ class Primitives {
|
|||
}
|
||||
|
||||
static int timerFrequency;
|
||||
static Function timerTicks;
|
||||
static int Function() timerTicks;
|
||||
|
||||
static bool get isD8 {
|
||||
return JS(
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
|
||||
@patch
|
||||
class Stopwatch {
|
||||
static const _maxInt = 0x7FFFFFFFFFFFFFFF;
|
||||
|
||||
@patch
|
||||
static void _initTicker() {
|
||||
if (_frequency == null) {
|
||||
|
@ -19,4 +21,36 @@ class Stopwatch {
|
|||
|
||||
// Returns the frequency of clock ticks in Hz.
|
||||
static int _computeFrequency() native "Stopwatch_frequency";
|
||||
|
||||
@patch
|
||||
int get elapsedMicroseconds {
|
||||
int ticks = elapsedTicks;
|
||||
// Special case the more likely frequencies to avoid division,
|
||||
// or divide by a known value.
|
||||
if (_frequency == 1000000000) return ticks ~/ 1000;
|
||||
if (_frequency == 1000000) return ticks;
|
||||
if (_frequency == 1000) return ticks * 1000;
|
||||
if (ticks <= (_maxInt ~/ 1000000)) {
|
||||
return (ticks * 1000000) ~/ _frequency;
|
||||
}
|
||||
// Multiplication would have overflowed.
|
||||
int ticksPerSecond = ticks ~/ _frequency;
|
||||
int remainingTicks = ticks.remainder(_frequency);
|
||||
return ticksPerSecond * 1000000 + (remainingTicks * 1000000) ~/ _frequency;
|
||||
}
|
||||
|
||||
@patch
|
||||
int get elapsedMilliseconds {
|
||||
int ticks = elapsedTicks;
|
||||
if (_frequency == 1000000000) return ticks ~/ 1000000;
|
||||
if (_frequency == 1000000) return ticks ~/ 1000;
|
||||
if (_frequency == 1000) return ticks;
|
||||
if (ticks <= (_maxInt ~/ 1000)) {
|
||||
return (ticks * 1000) ~/ _frequency;
|
||||
}
|
||||
// Multiplication would have overflowed.
|
||||
int ticksPerSecond = ticks ~/ _frequency;
|
||||
int remainingTicks = ticks.remainder(_frequency);
|
||||
return ticksPerSecond * 1000 + (remainingTicks * 1000) ~/ _frequency;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -391,6 +391,22 @@ class Stopwatch {
|
|||
|
||||
@patch
|
||||
static int _now() => Primitives.timerTicks();
|
||||
|
||||
@patch
|
||||
int get elapsedMicroseconds {
|
||||
int ticks = elapsedTicks;
|
||||
if (_frequency == 1000000) return ticks;
|
||||
assert(_frequency == 1000);
|
||||
return ticks * 1000;
|
||||
}
|
||||
|
||||
@patch
|
||||
int get elapsedMilliseconds {
|
||||
int ticks = elapsedTicks;
|
||||
if (_frequency == 1000) return ticks;
|
||||
assert(_frequency == 1000000);
|
||||
return ticks ~/ 1000;
|
||||
}
|
||||
}
|
||||
|
||||
// Patch for List implementation.
|
||||
|
|
|
@ -582,7 +582,7 @@ class Primitives {
|
|||
return "Instance of '$name'";
|
||||
}
|
||||
|
||||
static num dateNow() => JS('int', r'Date.now()');
|
||||
static int dateNow() => JS('int', r'Date.now()');
|
||||
|
||||
static void initTicker() {
|
||||
if (timerFrequency != null) return;
|
||||
|
@ -600,7 +600,7 @@ class Primitives {
|
|||
}
|
||||
|
||||
static int timerFrequency;
|
||||
static Function timerTicks;
|
||||
static int Function() timerTicks;
|
||||
|
||||
static String currentUri() {
|
||||
requiresPreamble();
|
||||
|
|
|
@ -93,8 +93,8 @@ abstract class Iterable<E> {
|
|||
*
|
||||
* If [generator] is omitted, it defaults to an identity function
|
||||
* on integers `(int x) => x`, so it may only be omitted if the type
|
||||
* parameter allows integer values. That is, if [E] is one of
|
||||
* `int`, `num`, `Object` or `dynamic`.
|
||||
* parameter allows integer values. That is, if [E] is a super-type
|
||||
* of [int].
|
||||
*
|
||||
* As an `Iterable`, `new Iterable.generate(n, generator))` is equivalent to
|
||||
* `const [0, ..., n - 1].map(generator)`.
|
||||
|
|
|
@ -9,7 +9,9 @@ part of dart.core;
|
|||
*/
|
||||
class Stopwatch {
|
||||
/**
|
||||
* Cached frequency of the system. Must be initialized in [_initTicker];
|
||||
* Cached frequency of the system in Hz (ticks per second).
|
||||
*
|
||||
* Must be initialized in [_initTicker];
|
||||
*/
|
||||
static int _frequency;
|
||||
|
||||
|
@ -100,16 +102,12 @@ class Stopwatch {
|
|||
/**
|
||||
* The [elapsedTicks] counter converted to microseconds.
|
||||
*/
|
||||
int get elapsedMicroseconds {
|
||||
return (elapsedTicks * 1000000) ~/ frequency;
|
||||
}
|
||||
external int get elapsedMicroseconds;
|
||||
|
||||
/**
|
||||
* The [elapsedTicks] counter converted to milliseconds.
|
||||
*/
|
||||
int get elapsedMilliseconds {
|
||||
return (elapsedTicks * 1000) ~/ frequency;
|
||||
}
|
||||
external int get elapsedMilliseconds;
|
||||
|
||||
/**
|
||||
* Whether the [Stopwatch] is currently running.
|
||||
|
|
Loading…
Reference in a new issue