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

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

Issue 24359002: Make smi.toString() do its work faster using native string helpers. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Did optimization entirely in dart, using native string building helpers. Created 7 years, 2 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 | no next file » | no next file with comments »
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 // TODO(srdjan): fix limitations. 5 // TODO(srdjan): fix limitations.
6 // - shift amount must be a Smi. 6 // - shift amount must be a Smi.
7 class _IntegerImplementation { 7 class _IntegerImplementation {
8 factory _IntegerImplementation._uninstantiable() { 8 factory _IntegerImplementation._uninstantiable() {
9 throw new UnsupportedError( 9 throw new UnsupportedError(
10 "_IntegerImplementation can only be allocated by the VM"); 10 "_IntegerImplementation can only be allocated by the VM");
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 return this; 226 return this;
227 } 227 }
228 int operator ~() native "Smi_bitNegate"; 228 int operator ~() native "Smi_bitNegate";
229 int get bitLength native "Smi_bitLength"; 229 int get bitLength native "Smi_bitLength";
230 230
231 int _shrFromInt(int other) native "Smi_shrFromInt"; 231 int _shrFromInt(int other) native "Smi_shrFromInt";
232 int _shlFromInt(int other) native "Smi_shlFromInt"; 232 int _shlFromInt(int other) native "Smi_shlFromInt";
233 233
234 String toString() { 234 String toString() {
235 if (this == 0) return "0"; 235 if (this == 0) return "0";
236 var reversed = new List(); 236 var reversed = _toStringBuffer;
237 var val = this < 0 ? -this : this; 237 var negative = false;
238 var val = this;
239 int index = 0;
240
241 if (this < 0) {
kasperl 2013/09/24 11:22:09 Use val < 0?
Lasse Reichstein Nielsen 2013/09/24 19:48:49 good point.
242 negative = true;
243 // Handle the first digit as negative to avoid negating the minimum
244 // smi, for which the negation is not a smi.
245 int digit = -(val.remainder(10));
246 reversed[index++] = digit + 0x30;
247 val = -(val ~/ 10);
248 }
249
238 while (val > 0) { 250 while (val > 0) {
239 reversed.add((val % 10) + 0x30); 251 int digit = val % 10;
252 reversed[index++] = (digit + 0x30);
240 val = val ~/ 10; 253 val = val ~/ 10;
241 } 254 }
242 final int numDigits = reversed.length; 255 if (negative) reversed[index++] = 0x2D; // '-'.
243 List digits; 256
244 int i; 257 _OneByteString string = _OneByteString._allocate(index);
245 if (this < 0) { 258 for (int i = 0, j = index; i < index; i++) {
246 digits = new List(numDigits + 1); 259 string._setAt(i, reversed[--j]);
247 digits[0] = 0x2D; // '-'.
248 i = 1;
249 } else {
250 digits = new List(numDigits);
251 i = 0;
252 } 260 }
253 int ri = reversed.length - 1; 261 return string;
254 for (; i < digits.length; i++, ri--) {
255 digits[i] = reversed[ri];
256 }
257 return _StringBase.createFromCharCodes(digits);
258 } 262 }
259 } 263 }
260 264
265 // Reusable buffer used by smi.toString.
266 List _toStringBuffer = new Uint8List(20);
267
261 // Represents integers that cannot be represented by Smi but fit into 64bits. 268 // Represents integers that cannot be represented by Smi but fit into 64bits.
262 class _Mint extends _IntegerImplementation implements int { 269 class _Mint extends _IntegerImplementation implements int {
263 factory _Mint._uninstantiable() { 270 factory _Mint._uninstantiable() {
264 throw new UnsupportedError( 271 throw new UnsupportedError(
265 "_Mint can only be allocated by the VM"); 272 "_Mint can only be allocated by the VM");
266 } 273 }
267 int get hashCode { 274 int get hashCode {
268 return this; 275 return this;
269 } 276 }
270 int operator ~() native "Mint_bitNegate"; 277 int operator ~() native "Mint_bitNegate";
(...skipping 30 matching lines...) Expand all
301 } else { 308 } else {
302 return 0; 309 return 0;
303 } 310 }
304 } 311 }
305 int _shlFromInt(int other) native "Bigint_shlFromInt"; 312 int _shlFromInt(int other) native "Bigint_shlFromInt";
306 313
307 int pow(int exponent) { 314 int pow(int exponent) {
308 throw "Bigint.pow not implemented"; 315 throw "Bigint.pow not implemented";
309 } 316 }
310 } 317 }
OLDNEW
« 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