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

Side by Side Diff: runtime/lib/double.dart

Issue 518093002: Optimize number-to-string conversions. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 3 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | runtime/lib/integers.dart » ('j') | runtime/lib/integers.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 class _Double extends _Num implements double { 5 class _Double extends _Num implements double {
6 factory _Double.fromInteger(int value) 6 factory _Double.fromInteger(int value)
7 native "Double_doubleFromInteger"; 7 native "Double_doubleFromInteger";
8 8
9 Type get runtimeType => double; 9 Type get runtimeType => double;
10 10
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 } 129 }
130 if (lowerLimit.isNaN) return lowerLimit; 130 if (lowerLimit.isNaN) return lowerLimit;
131 if (this.compareTo(lowerLimit) < 0) return lowerLimit; 131 if (this.compareTo(lowerLimit) < 0) return lowerLimit;
132 if (this.compareTo(upperLimit) > 0) return upperLimit; 132 if (this.compareTo(upperLimit) > 0) return upperLimit;
133 return this; 133 return this;
134 } 134 }
135 135
136 int toInt() native "Double_toInt"; 136 int toInt() native "Double_toInt";
137 double toDouble() { return this; } 137 double toDouble() { return this; }
138 138
139 // Number of keys + values. Should be a power of 2.
140 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.
141 static const int CACHE_MASK = CACHE_LENGTH - 1;
142 // Each key (double) followed by its toString result.
143 static final List _cache = new List(CACHE_LENGTH);
144 static int _cacheEvictIndex = 0;
Lasse Reichstein Nielsen 2014/09/01 07:14:21 This "optimization" seems a little too speculative
145
146 String toString() {
147 // TODO(koda): Consider optimizing all small integral values.
148 if (identical(0.0, this)) {
149 return "0.0";
150 }
151 // TODO(koda): Consider starting at most recently inserted.
152 for (int i = 0; i < CACHE_LENGTH; i += 2) {
153 // Need 'identical' to handle negative zero, etc.
154 if (identical(_cache[i], this)) {
155 return _cache[i + 1];
156 }
157 }
158 String result = super.toString();
159 // Replace the least recently inserted entry.
160 _cache[_cacheEvictIndex] = this;
161 _cache[_cacheEvictIndex + 1] = result;
162 _cacheEvictIndex = (_cacheEvictIndex + 2) & CACHE_MASK;
163 return result;
164 }
165
139 String toStringAsFixed(int fractionDigits) { 166 String toStringAsFixed(int fractionDigits) {
140 // See ECMAScript-262, 15.7.4.5 for details. 167 // See ECMAScript-262, 15.7.4.5 for details.
141 168
142 if (fractionDigits is! int) { 169 if (fractionDigits is! int) {
143 throw new ArgumentError(fractionDigits); 170 throw new ArgumentError(fractionDigits);
144 } 171 }
145 // Step 2. 172 // Step 2.
146 if (fractionDigits < 0 || fractionDigits > 20) { 173 if (fractionDigits < 0 || fractionDigits > 20) {
147 throw new RangeError(fractionDigits); 174 throw new RangeError(fractionDigits);
148 } 175 }
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 return EQUAL; 263 return EQUAL;
237 } 264 }
238 } else if (isNaN) { 265 } else if (isNaN) {
239 return other.isNaN ? EQUAL : GREATER; 266 return other.isNaN ? EQUAL : GREATER;
240 } else { 267 } else {
241 // Other is NaN. 268 // Other is NaN.
242 return LESS; 269 return LESS;
243 } 270 }
244 } 271 }
245 } 272 }
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/integers.dart » ('j') | runtime/lib/integers.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698