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

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: Address comments. 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 (numberString.length < 4) return numberString;
210 if (numberString[1] == "e" && numberString[2] == "+") {
211 // For example: "1e+5". Remove the "+".
212 return "${numberString[0]}e${numberString.substring(2)}";
213 }
214 if (numberString[1] != ".") return numberString;
215 int digitsAfterDotCount = 0;
216 int pos = 2;
217 while (pos < numberString.length && numberString[pos] != "e") {
218 pos++;
219 digitsAfterDotCount++;
220 }
221 if (pos >= numberString.length) return numberString;
222 int exponent = int.parse(numberString.substring(pos + 1));
223 if (exponent <= digitsAfterDotCount) return numberString;
224 String digitsAfterDot = numberString.substring(2, pos);
225 int shiftedExponent = exponent - digitsAfterDotCount;
226 String result = "${numberString[0]}${digitsAfterDot}e$shiftedExponent";
227 assert(double.parse(result) == double.parse(numberString));
228 return result;
229 }
230
203 @override 231 @override
204 jsAst.Expression visitInt(IntConstantValue constant, [_]) { 232 jsAst.Expression visitInt(IntConstantValue constant, [_]) {
205 int primitiveValue = constant.primitiveValue; 233 int primitiveValue = constant.primitiveValue;
206 // Since we are in JavaScript we can shorten long integers to their 234 // Since we are in JavaScript we can shorten long integers to their
207 // shorter exponential representation. 235 // shorter exponential representation.
208 // For example: "1e+4" is shorter than "10000". 236 // For example: "1e+4" is shorter than "10000".
209 // 237 //
210 // Note that this shortening apparently loses precision for big numbers 238 // Note that this shortening apparently loses precision for big numbers
211 // (like 1234567890123456789012345 which becomes 1.2345678901234568e+24). 239 // (like 1234567890123456789012345 which becomes 12345678901234568e8).
212 // However, since JavaScript engines implicitly convert to double, these 240 // However, since JavaScript engines represent all numbers as doubles,
213 // digits are lost anyway. 241 // these digits are lost anyway.
214 if (primitiveValue.abs() >= 10000) { 242 int cutOffValue = compiler.enableMinification ? 10000 : 1e20.toInt();
215 String exponential = primitiveValue.toStringAsExponential(); 243 if (primitiveValue.abs() >= cutOffValue) {
244 String exponential = _shortenExponentialRepresentation(
245 primitiveValue.toStringAsExponential());
216 String decimal = primitiveValue.toString(); 246 String decimal = primitiveValue.toString();
217 return new jsAst.LiteralNumber( 247 return new jsAst.LiteralNumber(
218 (exponential.length < decimal.length) ? exponential : decimal); 248 (exponential.length < decimal.length) ? exponential : decimal);
219 } 249 }
220 return new jsAst.LiteralNumber('$primitiveValue'); 250 return new jsAst.LiteralNumber('$primitiveValue');
221 } 251 }
222 252
223 @override 253 @override
224 jsAst.Expression visitDouble(DoubleConstantValue constant, [_]) { 254 jsAst.Expression visitDouble(DoubleConstantValue constant, [_]) {
225 double value = constant.primitiveValue; 255 double value = constant.primitiveValue;
226 if (value.isNaN) { 256 if (value.isNaN) {
227 return js("0/0"); 257 return js("0/0");
228 } else if (value == double.INFINITY) { 258 } else if (value == double.INFINITY) {
229 return js("1/0"); 259 return js("1/0");
230 } else if (value == -double.INFINITY) { 260 } else if (value == -double.INFINITY) {
231 return js("-1/0"); 261 return js("-1/0");
232 } else { 262 } else {
233 return new jsAst.LiteralNumber("$value"); 263 String shortened = _shortenExponentialRepresentation("$value");
264 return new jsAst.LiteralNumber(shortened);
234 } 265 }
235 } 266 }
236 267
237 @override 268 @override
238 jsAst.Expression visitBool(BoolConstantValue constant, [_]) { 269 jsAst.Expression visitBool(BoolConstantValue constant, [_]) {
239 if (compiler.enableMinification) { 270 if (compiler.enableMinification) {
240 if (constant.isTrue) { 271 if (constant.isTrue) {
241 // Use !0 for true. 272 // Use !0 for true.
242 return js("!0"); 273 return js("!0");
243 } else { 274 } else {
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 [value, argumentList]); 444 [value, argumentList]);
414 } 445 }
415 return value; 446 return value;
416 } 447 }
417 448
418 @override 449 @override
419 jsAst.Expression visitDeferred(DeferredConstantValue constant, [_]) { 450 jsAst.Expression visitDeferred(DeferredConstantValue constant, [_]) {
420 return constantEmitter.reference(constant.referenced); 451 return constantEmitter.reference(constant.referenced);
421 } 452 }
422 } 453 }
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