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

Side by Side Diff: runtime/lib/integers.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
« runtime/lib/double.dart ('K') | « runtime/lib/double.dart ('k') | 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 extends _Num { 7 class _IntegerImplementation extends _Num {
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 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 0x36, 0x38, 0x36, 0x39, 0x37, 0x30, 0x37, 0x31, 286 0x36, 0x38, 0x36, 0x39, 0x37, 0x30, 0x37, 0x31,
287 0x37, 0x32, 0x37, 0x33, 0x37, 0x34, 0x37, 0x35, 287 0x37, 0x32, 0x37, 0x33, 0x37, 0x34, 0x37, 0x35,
288 0x37, 0x36, 0x37, 0x37, 0x37, 0x38, 0x37, 0x39, 288 0x37, 0x36, 0x37, 0x37, 0x37, 0x38, 0x37, 0x39,
289 0x38, 0x30, 0x38, 0x31, 0x38, 0x32, 0x38, 0x33, 289 0x38, 0x30, 0x38, 0x31, 0x38, 0x32, 0x38, 0x33,
290 0x38, 0x34, 0x38, 0x35, 0x38, 0x36, 0x38, 0x37, 290 0x38, 0x34, 0x38, 0x35, 0x38, 0x36, 0x38, 0x37,
291 0x38, 0x38, 0x38, 0x39, 0x39, 0x30, 0x39, 0x31, 291 0x38, 0x38, 0x38, 0x39, 0x39, 0x30, 0x39, 0x31,
292 0x39, 0x32, 0x39, 0x33, 0x39, 0x34, 0x39, 0x35, 292 0x39, 0x32, 0x39, 0x33, 0x39, 0x34, 0x39, 0x35,
293 0x39, 0x36, 0x39, 0x37, 0x39, 0x38, 0x39, 0x39 293 0x39, 0x36, 0x39, 0x37, 0x39, 0x38, 0x39, 0x39
294 ]; 294 ];
295 295
296 /**
297 * Result of int.toString for -99, -98, ..., 98, 99.
298 */
299 static const _smallLookupTable = const [
300 "-99", "-98", "-97", "-96", "-95", "-94", "-93", "-92", "-91", "-90",
301 "-89", "-88", "-87", "-86", "-85", "-84", "-83", "-82", "-81", "-80",
302 "-79", "-78", "-77", "-76", "-75", "-74", "-73", "-72", "-71", "-70",
303 "-69", "-68", "-67", "-66", "-65", "-64", "-63", "-62", "-61", "-60",
304 "-59", "-58", "-57", "-56", "-55", "-54", "-53", "-52", "-51", "-50",
305 "-49", "-48", "-47", "-46", "-45", "-44", "-43", "-42", "-41", "-40",
306 "-39", "-38", "-37", "-36", "-35", "-34", "-33", "-32", "-31", "-30",
307 "-29", "-28", "-27", "-26", "-25", "-24", "-23", "-22", "-21", "-20",
308 "-19", "-18", "-17", "-16", "-15", "-14", "-13", "-12", "-11", "-10",
309 "-9", "-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1", "0",
310 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
311 "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
312 "21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
313 "31", "32", "33", "34", "35", "36", "37", "38", "39", "40",
314 "41", "42", "43", "44", "45", "46", "47", "48", "49", "50",
315 "51", "52", "53", "54", "55", "56", "57", "58", "59", "60",
316 "61", "62", "63", "64", "65", "66", "67", "68", "69", "70",
317 "71", "72", "73", "74", "75", "76", "77", "78", "79", "80",
318 "81", "82", "83", "84", "85", "86", "87", "88", "89", "90",
319 "91", "92", "93", "94", "95", "96", "97", "98", "99",
Ivan Posva 2014/08/29 20:58:47 Trailing comma.
koda 2014/08/29 21:02:33 Done.
Lasse Reichstein Nielsen 2014/09/01 07:14:21 There isn't anything inherently wrong with a trail
320 ];
321
296 // Powers of 10 above 1000000 are indistinguishable by eye. 322 // Powers of 10 above 1000000 are indistinguishable by eye.
297 static const int _POW_10_7 = 10000000; 323 static const int _POW_10_7 = 10000000;
298 static const int _POW_10_8 = 100000000; 324 static const int _POW_10_8 = 100000000;
299 static const int _POW_10_9 = 1000000000; 325 static const int _POW_10_9 = 1000000000;
300 326
301 // Find the number of decimal digits in a positive smi. 327 // Find the number of decimal digits in a positive smi.
302 // Never called with numbers < 100. These are handled before calling. 328 // Never called with numbers < 100. These are handled before calling.
303 static int _positiveBase10Length(var smi) { 329 static int _positiveBase10Length(var smi) {
304 // A positive smi has length <= 19 if 63-bit, <=10 if 31-bit. 330 // A positive smi has length <= 19 if 63-bit, <=10 if 31-bit.
305 // Avoid comparing a 31-bit smi to a non-smi. 331 // Avoid comparing a 31-bit smi to a non-smi.
306 if (smi < 1000) return 3; 332 if (smi < 1000) return 3;
307 if (smi < 10000) return 4; 333 if (smi < 10000) return 4;
308 if (smi < _POW_10_7) { 334 if (smi < _POW_10_7) {
309 if (smi < 100000) return 5; 335 if (smi < 100000) return 5;
310 if (smi < 1000000) return 6; 336 if (smi < 1000000) return 6;
311 return 7; 337 return 7;
312 } 338 }
313 if (smi < _POW_10_8) return 8; 339 if (smi < _POW_10_8) return 8;
314 if (smi < _POW_10_9) return 9; 340 if (smi < _POW_10_9) return 9;
315 smi = smi ~/ _POW_10_9; 341 smi = smi ~/ _POW_10_9;
316 // Handle numbers < 100 before calling recursively. 342 // Handle numbers < 100 before calling recursively.
317 if (smi < 10) return 10; 343 if (smi < 10) return 10;
318 if (smi < 100) return 11; 344 if (smi < 100) return 11;
319 return 9 + _positiveBase10Length(smi); 345 return 9 + _positiveBase10Length(smi);
320 } 346 }
321 347
322 String toString() { 348 String toString() {
349 if (this < 100 && this > -100) return _smallLookupTable[this + 99];
Lasse Reichstein Nielsen 2014/09/01 07:14:21 Would it make sense to put this after the sign che
323 if (this < 0) return _negativeToString(this); 350 if (this < 0) return _negativeToString(this);
324 // Inspired by Andrei Alexandrescu: "Three Optimization Tips for C++" 351 // Inspired by Andrei Alexandrescu: "Three Optimization Tips for C++"
325 // Avoid expensive remainder operation by doing it on more than 352 // Avoid expensive remainder operation by doing it on more than
326 // one digit at a time. 353 // one digit at a time.
327 const int DIGIT_ZERO = 0x30; 354 const int DIGIT_ZERO = 0x30;
328 if (this < 10) {
329 return _OneByteString._allocate(1).._setAt(0, DIGIT_ZERO + this);
330 }
331 if (this < 100) {
332 int digitIndex = 2 * this;
333 return _OneByteString._allocate(2)
334 .._setAt(0, _digitTable[digitIndex])
335 .._setAt(1, _digitTable[digitIndex + 1]);
336 }
337 int length = _positiveBase10Length(this); 355 int length = _positiveBase10Length(this);
338 _OneByteString result = _OneByteString._allocate(length); 356 _OneByteString result = _OneByteString._allocate(length);
339 int index = length - 1; 357 int index = length - 1;
340 var smi = this; 358 var smi = this;
341 do { 359 do {
342 // Two digits at a time. 360 // Two digits at a time.
343 var twoDigits = smi.remainder(100); 361 var twoDigits = smi.remainder(100);
344 smi = smi ~/ 100; 362 smi = smi ~/ 100;
345 int digitIndex = twoDigits * 2; 363 int digitIndex = twoDigits * 2;
346 result._setAt(index, _digitTable[digitIndex + 1]); 364 result._setAt(index, _digitTable[digitIndex + 1]);
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 } else { 485 } else {
468 return 0; 486 return 0;
469 } 487 }
470 } 488 }
471 int _shlFromInt(int other) native "Bigint_shlFromInt"; 489 int _shlFromInt(int other) native "Bigint_shlFromInt";
472 490
473 int pow(int exponent) { 491 int pow(int exponent) {
474 throw "Bigint.pow not implemented"; 492 throw "Bigint.pow not implemented";
475 } 493 }
476 } 494 }
OLDNEW
« runtime/lib/double.dart ('K') | « runtime/lib/double.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698