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

Side by Side Diff: pkg/compiler/lib/src/js_backend/constant_emitter.dart

Issue 938323003: dart2js: Better big-number shortening. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 10 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 | tests/compiler/dart2js/number_output_test.dart » ('j') | 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 part of js_backend; 5 part of js_backend;
6 6
7 class ConstantEmitter { 7 class ConstantEmitter {
8 ConstantReferenceEmitter _referenceEmitter; 8 ConstantReferenceEmitter _referenceEmitter;
9 ConstantLiteralEmitter _literalEmitter; 9 ConstantLiteralEmitter _literalEmitter;
10 10
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 compiler.internalError(NO_LOCATION_SPANNABLE, 193 compiler.internalError(NO_LOCATION_SPANNABLE,
194 "The function constant does not need specific JS code."); 194 "The function constant does not need specific JS code.");
195 return null; 195 return null;
196 } 196 }
197 197
198 @override 198 @override
199 jsAst.Expression visitNull(NullConstantValue constant, [_]) { 199 jsAst.Expression visitNull(NullConstantValue constant, [_]) {
200 return new jsAst.LiteralNull(); 200 return new jsAst.LiteralNull();
201 } 201 }
202 202
203 /// Reduces the size of exponential representations when minification is
204 /// enabled.
205 ///
206 /// Removes the "+" after the exponential sign, and removes the "." before the
207 /// "e". For example `1.23e+5` is changed to `123e3`.
208 String _shortenExponentialRepresentation(String numberString) {
209 if (!compiler.enableMinification) return numberString;
sra1 2015/02/20 22:08:25 I would still do this, the cutoff avoids most weir
floitsch 2015/02/23 18:36:23 It was to avoid running it for visitDouble but don
210 if (numberString.length < 4) return numberString;
211 if (numberString[1] == "e" && numberString[2] == "+") {
212 // For example: "1e+5". Remove the "+".
213 return "${numberString[0]}e${numberString.substring(2)}";
214 }
215 if (numberString[1] != ".") return numberString;
216 int digitsAfterDotCount = 0;
217 int pos = 2;
sra1 2015/02/20 22:08:25 int pos = numberString.indexOf('e', 2); Or you co
floitsch 2015/02/23 18:36:23 Exponential representations always start with a si
218 while (pos < numberString.length && numberString[pos] != "e") {
219 pos++;
220 digitsAfterDotCount++;
221 }
222 if (pos >= numberString.length) return numberString;
223 int exponent = int.parse(numberString.substring(pos + 1));
224 if (exponent <= digitsAfterDotCount) return numberString;
225 String digitsAfterDot = numberString.substring(2, pos);
226 int shiftedExponent = exponent - digitsAfterDotCount;
227 String result = "${numberString[0]}${digitsAfterDot}e$shiftedExponent";
228 assert(double.parse(result) == double.parse(numberString));
229 return result;
230 }
231
203 @override 232 @override
204 jsAst.Expression visitInt(IntConstantValue constant, [_]) { 233 jsAst.Expression visitInt(IntConstantValue constant, [_]) {
205 int primitiveValue = constant.primitiveValue; 234 int primitiveValue = constant.primitiveValue;
206 // Since we are in JavaScript we can shorten long integers to their 235 // Since we are in JavaScript we can shorten long integers to their
207 // shorter exponential representation. 236 // shorter exponential representation.
208 // For example: "1e+4" is shorter than "10000". 237 // For example: "1e+4" is shorter than "10000".
209 // 238 //
210 // Note that this shortening apparently loses precision for big numbers 239 // Note that this shortening apparently loses precision for big numbers
211 // (like 1234567890123456789012345 which becomes 1.2345678901234568e+24). 240 // (like 1234567890123456789012345 which becomes 1.2345678901234568e+24).
sra1 2015/02/20 22:08:25 This comment might not be accurate - no +.
floitsch 2015/02/23 18:36:23 Done.
212 // However, since JavaScript engines implicitly convert to double, these 241 // However, since JavaScript engines represent all numbers as doubles,
213 // digits are lost anyway. 242 // these digits are lost anyway.
214 if (primitiveValue.abs() >= 10000) { 243 int cutOffValue = compiler.enableMinification ? 10000 : 1e20.toInt();
215 String exponential = primitiveValue.toStringAsExponential(); 244 if (primitiveValue.abs() >= cutOffValue) {
245 String exponential = _shortenExponentialRepresentation(
246 primitiveValue.toStringAsExponential());
216 String decimal = primitiveValue.toString(); 247 String decimal = primitiveValue.toString();
217 return new jsAst.LiteralNumber( 248 return new jsAst.LiteralNumber(
218 (exponential.length < decimal.length) ? exponential : decimal); 249 (exponential.length < decimal.length) ? exponential : decimal);
219 } 250 }
220 return new jsAst.LiteralNumber('$primitiveValue'); 251 return new jsAst.LiteralNumber('$primitiveValue');
221 } 252 }
222 253
223 @override 254 @override
224 jsAst.Expression visitDouble(DoubleConstantValue constant, [_]) { 255 jsAst.Expression visitDouble(DoubleConstantValue constant, [_]) {
225 double value = constant.primitiveValue; 256 double value = constant.primitiveValue;
226 if (value.isNaN) { 257 if (value.isNaN) {
227 return js("0/0"); 258 return js("0/0");
228 } else if (value == double.INFINITY) { 259 } else if (value == double.INFINITY) {
229 return js("1/0"); 260 return js("1/0");
230 } else if (value == -double.INFINITY) { 261 } else if (value == -double.INFINITY) {
231 return js("-1/0"); 262 return js("-1/0");
232 } else { 263 } else {
233 return new jsAst.LiteralNumber("$value"); 264 String shortened = _shortenExponentialRepresentation("$value");
265 return new jsAst.LiteralNumber(shortened);
234 } 266 }
235 } 267 }
236 268
237 @override 269 @override
238 jsAst.Expression visitBool(BoolConstantValue constant, [_]) { 270 jsAst.Expression visitBool(BoolConstantValue constant, [_]) {
239 if (compiler.enableMinification) { 271 if (compiler.enableMinification) {
240 if (constant.isTrue) { 272 if (constant.isTrue) {
241 // Use !0 for true. 273 // Use !0 for true.
242 return js("!0"); 274 return js("!0");
243 } else { 275 } else {
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 [value, argumentList]); 445 [value, argumentList]);
414 } 446 }
415 return value; 447 return value;
416 } 448 }
417 449
418 @override 450 @override
419 jsAst.Expression visitDeferred(DeferredConstantValue constant, [_]) { 451 jsAst.Expression visitDeferred(DeferredConstantValue constant, [_]) {
420 return constantEmitter.reference(constant.referenced); 452 return constantEmitter.reference(constant.referenced);
421 } 453 }
422 } 454 }
OLDNEW
« no previous file with comments | « no previous file | tests/compiler/dart2js/number_output_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698