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

Unified 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, 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/integers.dart
diff --git a/runtime/lib/integers.dart b/runtime/lib/integers.dart
index 7c242ef95659a9872e9810492060f0d695f8dc13..aa2267efc0a9be8ad733fc28ff7e569242e77d53 100644
--- a/runtime/lib/integers.dart
+++ b/runtime/lib/integers.dart
@@ -233,31 +233,38 @@ class _Smi extends _IntegerImplementation implements int {
String toString() {
if (this == 0) return "0";
- var reversed = new List();
- var val = this < 0 ? -this : this;
+ var reversed = _toStringBuffer;
+ var negative = false;
+ var val = this;
+ int index = 0;
+
+ if (this < 0) {
kasperl 2013/09/24 11:22:09 Use val < 0?
Lasse Reichstein Nielsen 2013/09/24 19:48:49 good point.
+ negative = true;
+ // Handle the first digit as negative to avoid negating the minimum
+ // smi, for which the negation is not a smi.
+ int digit = -(val.remainder(10));
+ reversed[index++] = digit + 0x30;
+ val = -(val ~/ 10);
+ }
+
while (val > 0) {
- reversed.add((val % 10) + 0x30);
+ int digit = val % 10;
+ reversed[index++] = (digit + 0x30);
val = val ~/ 10;
}
- final int numDigits = reversed.length;
- List digits;
- int i;
- if (this < 0) {
- digits = new List(numDigits + 1);
- digits[0] = 0x2D; // '-'.
- i = 1;
- } else {
- digits = new List(numDigits);
- i = 0;
- }
- int ri = reversed.length - 1;
- for (; i < digits.length; i++, ri--) {
- digits[i] = reversed[ri];
+ if (negative) reversed[index++] = 0x2D; // '-'.
+
+ _OneByteString string = _OneByteString._allocate(index);
+ for (int i = 0, j = index; i < index; i++) {
+ string._setAt(i, reversed[--j]);
}
- return _StringBase.createFromCharCodes(digits);
+ return string;
}
}
+// Reusable buffer used by smi.toString.
+List _toStringBuffer = new Uint8List(20);
+
// Represents integers that cannot be represented by Smi but fit into 64bits.
class _Mint extends _IntegerImplementation implements int {
factory _Mint._uninstantiable() {
« 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