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

Side by Side Diff: lib/compiler/implementation/js_backend/minify_namer.dart

Issue 11307009: Revert "Minifying renamer for classes, methods and instance variables." (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 1 month 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
OLDNEW
(Empty)
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
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.
4
5 /**
6 * Assigns JavaScript identifiers to Dart variables, class-names and members.
7 */
8 class MinifyNamer extends Namer {
9 MinifyNamer(Compiler compiler) : super(compiler);
10
11 String get ISOLATE => 'I';
12 String get ISOLATE_PROPERTIES => 'p';
13 bool get shouldMinify => true;
14
15 const ALPHABET_CHARACTERS = 52; // a-zA-Z.
16 const ALPHANUMERIC_CHARACTERS = 62; // a-zA-Z0-9.
17
18 String getFreshName(String proposedName, Set<String> usedNames) {
19 var freshName = _getUnusedName(proposedName, usedNames);
20 usedNames.add(freshName);
21 return freshName;
22 }
23
24 // This gets a minified name based on a hash of the proposed name. This
25 // is slightly less efficient than just getting the next name in a series,
26 // but it means that small changes in the input program will give smallish
27 // changes in the output, which can be useful for diffing etc.
28 String _getUnusedName(String proposedName, Set<String> usedNames) {
29 // Try single-character names with characters that occur in the
30 // input.
31 for (int i = 0; i < proposedName.length; i++) {
32 String candidate = proposedName[i];
33 int code = candidate.charCodeAt(0);
34 if (code < $A) continue;
35 if (code > $z) continue;
36 if (code > $Z && code < $a) continue;
37 if (!usedNames.contains(candidate)) return candidate;
38 }
39
40 int hash = _calculateHash(proposedName);
41 // Avoid very small hashes that won't try many names.
42 hash = hash < 1000 ? hash * 314159 : hash; // Yes, it's prime.
43
44 // Try other n-character names based on the hash. We try one to three
45 // character identifiers. For each length we try around 10 different names
46 // in a predictable order determined by the proposed name. This is in order
47 // to make the renamer stable: small changes in the input should nornally
48 // result in relatively small changes in the output.
49 for (var n = 1; n <= 3; n++) {
50 int h = hash;
51 while (h > 10) {
52 var codes = <int>[_letterNumber(h)];
53 int h2 = h ~/ ALPHABET_CHARACTERS;
54 for (var i = 1; i < n; i++) {
55 codes.add(_alphaNumericNumber(h2));
56 h2 ~/= ALPHANUMERIC_CHARACTERS;
57 }
58 final candidate = new String.fromCharCodes(codes);
59 if (!usedNames.contains(candidate) && !jsReserved.contains(candidate)) {
60 return candidate;
61 }
62 // Try again with a slightly different hash. After around 10 turns
63 // around this loop h is zero and we try a longer name.
64 h ~/= 7;
65 }
66 }
67
68 // If we can't find a hash based name in the three-letter space, then base
69 // the name on a letter and a counter.
70 proposedName = new String.fromCharCodes([_letterNumber(hash)]);
71 var i = 0;
72 while (usedNames.contains("$proposedName$i")) {
73 i++;
74 }
75 return "$proposedName$i";
76 }
77
78 int _calculateHash(String name) {
79 int h = 0;
80 for (int i = 0; i < name.length; i++) {
81 h += name.charCodeAt(i);
82 h &= 0xffffffff;
83 h += h << 10;
84 h &= 0xffffffff;
85 h ^= h >> 6;
86 h &= 0xffffffff;
87 }
88 return h;
89 }
90
91 int _letterNumber(int x) {
92 if (x >= ALPHABET_CHARACTERS) x %= ALPHABET_CHARACTERS;
93 if (x < 26) return $a + x;
94 return $A + x - 26;
95 }
96
97 int _alphaNumericNumber(int x) {
98 if (x >= ALPHANUMERIC_CHARACTERS) x %= ALPHANUMERIC_CHARACTERS;
99 if (x < 26) return $a + x;
100 if (x < 52) return $A + x - 26;
101 return $0 + x - 52;
102 }
103
104 }
OLDNEW
« no previous file with comments | « lib/compiler/implementation/js_backend/js_backend.dart ('k') | lib/compiler/implementation/js_backend/namer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698