Chromium Code Reviews| Index: runtime/lib/double.dart |
| =================================================================== |
| --- runtime/lib/double.dart (revision 39698) |
| +++ runtime/lib/double.dart (working copy) |
| @@ -136,6 +136,33 @@ |
| int toInt() native "Double_toInt"; |
| double toDouble() { return this; } |
| + // Number of keys + values. Should be a power of 2. |
| + static const int CACHE_LENGTH = 16; |
|
Ivan Posva
2014/08/29 20:58:47
How about enforcing the comment by:
static const i
koda
2014/08/29 21:02:33
Done.
|
| + static const int CACHE_MASK = CACHE_LENGTH - 1; |
| + // Each key (double) followed by its toString result. |
| + static final List _cache = new List(CACHE_LENGTH); |
| + static int _cacheEvictIndex = 0; |
|
Lasse Reichstein Nielsen
2014/09/01 07:14:21
This "optimization" seems a little too speculative
|
| + |
| + String toString() { |
| + // TODO(koda): Consider optimizing all small integral values. |
| + if (identical(0.0, this)) { |
| + return "0.0"; |
| + } |
| + // TODO(koda): Consider starting at most recently inserted. |
| + for (int i = 0; i < CACHE_LENGTH; i += 2) { |
| + // Need 'identical' to handle negative zero, etc. |
| + if (identical(_cache[i], this)) { |
| + return _cache[i + 1]; |
| + } |
| + } |
| + String result = super.toString(); |
| + // Replace the least recently inserted entry. |
| + _cache[_cacheEvictIndex] = this; |
| + _cache[_cacheEvictIndex + 1] = result; |
| + _cacheEvictIndex = (_cacheEvictIndex + 2) & CACHE_MASK; |
| + return result; |
| + } |
| + |
| String toStringAsFixed(int fractionDigits) { |
| // See ECMAScript-262, 15.7.4.5 for details. |