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

Unified 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, 4 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 | runtime/lib/integers.dart » ('j') | runtime/lib/integers.dart » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.
« 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