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

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

Issue 11265020: 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, 2 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
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 minify => true;
floitsch 2012/10/25 12:58:38 I think I prefer "shouldMinify" or "isMinifying",
erikcorry 2012/12/06 09:38:07 Done.
14
15 String getFreshName(String proposedName, Set<String> usedNames) {
16 var freshName = _getUnusedName(proposedName, usedNames);
17 usedNames.add(freshName);
18 return freshName;
19 }
20
21 // This gets a minified name based on a hash of the proposed name. This
22 // is slightly less efficient than just getting the next name in a series,
23 // but it means that small changes in the input program will give smallish
24 // changes in the output, which can be useful for diffing etc.
25 String _getUnusedName(String proposedName, Set<String> usedNames) {
26 // Try single-character names with characters that occur in the
27 // input.
28 for (int i = 0; i < proposedName.length; i++) {
29 String candidate = proposedName[i];
30 int code = candidate.charCodeAt(0);
31 if (code < $A) continue;
32 if (code > $z) continue;
33 if (code > $Z && code < $a) continue;
34 if (!usedNames.contains(candidate)) return candidate;
35 }
36
37 int hash = _calculateHash(proposedName);
38 // Avoid very small hashes that won't try many names.
39 hash = hash < 1000 ? hash * 314159 : hash;
40
41 // Try other n-character names based on the hash.
floitsch 2012/10/25 12:58:38 Give a small description maybe mentioning that: -
erikcorry 2012/12/06 09:38:07 Done.
42 for (var n = 1; n <= 3; n++) {
43 int h = hash;
44 while (h > 10) {
45 var codes = <int>[_letterNumber(h)];
floitsch 2012/10/25 12:58:38 It probably doesn't matter, but you could allocate
erikcorry 2012/12/06 09:38:07 I tried it and it got uglier, so I'll leave it as
46 var divisor = 26;
floitsch 2012/10/25 12:58:38 62.
erikcorry 2012/12/06 09:38:07 Done.
47 for (var i = 1; i < n; i++) {
48 codes.add(_alphaNumericNumber(h ~/ divisor));
49 divisor *= 26;
50 }
51 final candidate = new String.fromCharCodes(codes);
52 if (!usedNames.contains(candidate) && !jsReserved.contains(candidate)) {
53 return candidate;
54 }
55 h ~/= 7;
56 }
57 }
58
59 // If we can't find a hash based name in the three-letter space, then base
60 // the name on a letter and a counter.
61 proposedName = new String.fromCharCodes([_letterNumber(hash)]);
62 var i = 0;
63 while (usedNames.contains("$proposedName$i")) {
64 i++;
65 }
66 return "$proposedName$i";
67 }
68
69 int _calculateHash(String name) {
70 int h = 0;
71 for (int i = 0; i < name.length; i++) {
72 h += name.charCodeAt(i);
73 h &= 0xffffffff;
74 h += h << 10;
75 h &= 0xffffffff;
76 h ^= h >> 6;
77 h &= 0xffffffff;
78 }
79 return h;
80 }
81
82 int _letterNumber(int x) {
83 if (x >= 52) x %= 52;
84 if (x < 26) return $a + x;
85 return $A + x - 26;
86 }
87
88 int _alphaNumericNumber(int x) {
89 if (x >= 62) x %= 62;
90 if (x < 26) return $a + x;
91 if (x < 52) return $A + x - 26;
92 return $0 + x - 52;
93 }
94
95 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698