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

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

Issue 12250002: dart2js: In minified mode shorter getter and setter names. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 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 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 /** 7 /**
8 * Assigns JavaScript identifiers to Dart variables, class-names and members. 8 * Assigns JavaScript identifiers to Dart variables, class-names and members.
9 */ 9 */
10 class MinifyNamer extends Namer { 10 class MinifyNamer extends Namer {
11 MinifyNamer(Compiler compiler) : super(compiler) { 11 MinifyNamer(Compiler compiler) : super(compiler) {
12 reserveBackendNames(); 12 reserveBackendNames();
13 } 13 }
14 14
15 String get isolateName => 'I'; 15 String get isolateName => 'I';
16 String get isolatePropertiesName => 'p'; 16 String get isolatePropertiesName => 'p';
17 bool get shouldMinify => true; 17 bool get shouldMinify => true;
18 18
19 final String getterPrefix = 'g';
20 final String setterPrefix = 's';
21
19 const ALPHABET_CHARACTERS = 52; // a-zA-Z. 22 const ALPHABET_CHARACTERS = 52; // a-zA-Z.
20 const ALPHANUMERIC_CHARACTERS = 62; // a-zA-Z0-9. 23 const ALPHANUMERIC_CHARACTERS = 62; // a-zA-Z0-9.
21 24
22 // You can pass an invalid identifier to this and unlike its non-minifying 25 // You can pass an invalid identifier to this and unlike its non-minifying
23 // counterpart it will never return the proposedName as the new fresh name. 26 // counterpart it will never return the proposedName as the new fresh name.
24 String getFreshName(String proposedName, 27 String getFreshName(String proposedName,
25 Set<String> usedNames, 28 Set<String> usedNames,
26 Map<String, String> suggestedNames, 29 Map<String, String> suggestedNames,
27 {bool ensureSafe: true}) { 30 {bool ensureSafe: true}) {
28 var freshName; 31 var freshName;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 'Blob', 'blue', 'blur', 'BLUR', 'body', 'BOOL', 'BOTH', 'btoa', 'BYTE', 65 'Blob', 'blue', 'blur', 'BLUR', 'body', 'BOOL', 'BOTH', 'btoa', 'BYTE',
63 'cite', 'clip', 'code', 'cols', 'cues', 'data', 'DECR', 'DONE', 'face', 66 'cite', 'clip', 'code', 'cols', 'cues', 'data', 'DECR', 'DONE', 'face',
64 'file', 'File', 'fill', 'find', 'font', 'form', 'gain', 'hash', 'head', 67 'file', 'File', 'fill', 'find', 'font', 'form', 'gain', 'hash', 'head',
65 'high', 'hint', 'host', 'href', 'HRTF', 'IDLE', 'INCR', 'info', 'INIT', 68 'high', 'hint', 'host', 'href', 'HRTF', 'IDLE', 'INCR', 'info', 'INIT',
66 'isId', 'item', 'KEEP', 'kind', 'knee', 'lang', 'left', 'LESS', 'line', 69 'isId', 'item', 'KEEP', 'kind', 'knee', 'lang', 'left', 'LESS', 'line',
67 'link', 'list', 'load', 'loop', 'mode', 'name', 'Node', 'None', 'NONE', 70 'link', 'list', 'load', 'loop', 'mode', 'name', 'Node', 'None', 'NONE',
68 'only', 'open', 'OPEN', 'ping', 'play', 'port', 'rect', 'Rect', 'refX', 71 'only', 'open', 'OPEN', 'ping', 'play', 'port', 'rect', 'Rect', 'refX',
69 'refY', 'RGBA', 'root', 'rows', 'save', 'seed', 'seek', 'self', 'send', 72 'refY', 'RGBA', 'root', 'rows', 'save', 'seed', 'seek', 'self', 'send',
70 'show', 'SINE', 'size', 'span', 'stat', 'step', 'stop', 'tags', 'text', 73 'show', 'SINE', 'size', 'span', 'stat', 'step', 'stop', 'tags', 'text',
71 'Text', 'time', 'type', 'view', 'warn', 'wrap', 'ZERO']; 74 'Text', 'time', 'type', 'view', 'warn', 'wrap', 'ZERO'];
75
72 for (var name in reservedNativeProperties) { 76 for (var name in reservedNativeProperties) {
73 if (name.length < 2) { 77 if (name.length < 2) {
74 instanceNameMap[name] = name; 78 instanceNameMap[name] = name;
75 } 79 }
76 usedInstanceNames.add(name); 80 usedInstanceNames.add(name);
81 // Getter and setter names are autogenerated by prepending 'g' and 's' to
82 // field names. Therefore there are some field names we don't want to
83 // use.
84 if (name.startsWith('g')) usedInstanceNames.add(name.substring(1));
karlklose 2013/02/12 14:41:19 Can you use _hasBannedPrefix here?
erikcorry 2013/02/12 14:50:40 Yes, but the substring(1) still retains implicit k
85 if (name.startsWith('s')) usedInstanceNames.add(name.substring(1));
77 } 86 }
78 87
79 // This list of popular instance variable names generated with: 88 // This list of popular instance variable names generated with:
80 // cat out.js | 89 // cat out.js |
81 // perl -ne '$_=~s/(?<![^a-z0-9_\$]\$)\.([a-z0-9_\$]+)/print("$1\n")/gei' | 90 // perl -ne '$_=~s/(?<![^a-z0-9_\$]\$)\.([a-z0-9_\$]+)/print("$1\n")/gei' |
82 // sort | uniq -c | sort -nr | head -40 91 // sort | uniq -c | sort -nr | head -40
83 // Removed: html, call*, hasOwnProperty. 92 // Removed: html, call*, hasOwnProperty.
84 _populateSuggestedNames( 93 _populateSuggestedNames(
85 suggestedInstanceNames, 94 suggestedInstanceNames,
86 usedInstanceNames, 95 usedInstanceNames,
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 for (var n = 2; n <= 3; n++) { 154 for (var n = 2; n <= 3; n++) {
146 int h = hash; 155 int h = hash;
147 while (h > 10) { 156 while (h > 10) {
148 var codes = <int>[_letterNumber(h)]; 157 var codes = <int>[_letterNumber(h)];
149 int h2 = h ~/ ALPHABET_CHARACTERS; 158 int h2 = h ~/ ALPHABET_CHARACTERS;
150 for (var i = 1; i < n; i++) { 159 for (var i = 1; i < n; i++) {
151 codes.add(_alphaNumericNumber(h2)); 160 codes.add(_alphaNumericNumber(h2));
152 h2 ~/= ALPHANUMERIC_CHARACTERS; 161 h2 ~/= ALPHANUMERIC_CHARACTERS;
153 } 162 }
154 final candidate = new String.fromCharCodes(codes); 163 final candidate = new String.fromCharCodes(codes);
155 if (!usedNames.contains(candidate) && !jsReserved.contains(candidate)) { 164 if (!usedNames.contains(candidate) &&
165 !jsReserved.contains(candidate) &&
166 !_hasBannedPrefix(candidate)) {
156 return candidate; 167 return candidate;
157 } 168 }
158 // Try again with a slightly different hash. After around 10 turns 169 // Try again with a slightly different hash. After around 10 turns
159 // around this loop h is zero and we try a longer name. 170 // around this loop h is zero and we try a longer name.
160 h ~/= 7; 171 h ~/= 7;
161 } 172 }
162 } 173 }
163 174
164 // If we can't find a hash based name in the three-letter space, then base 175 // If we can't find a hash based name in the three-letter space, then base
165 // the name on a letter and a counter. 176 // the name on a letter and a counter.
166 var startLetter = new String.fromCharCodes([_letterNumber(hash)]); 177 var startLetter = new String.fromCharCodes([_letterNumber(hash)]);
167 var i = 0; 178 var i = 0;
168 while (usedNames.contains("$startLetter$i")) { 179 while (usedNames.contains("$startLetter$i")) {
169 i++; 180 i++;
170 } 181 }
182 // We don't need to check for banned prefix because the name is in the form
183 // xnnn, where nnn is a number. There can be no getter or setter called
184 // gnnn since that would imply a numeric field name.
171 return "$startLetter$i"; 185 return "$startLetter$i";
172 } 186 }
173 187
188 // Instance members starting with g and s are reserved for getters and
karlklose 2013/02/12 14:41:19 Use /// to turn comment into dart doc.
erikcorry 2013/02/12 14:50:40 Done.
189 // setters.
190 bool _hasBannedPrefix(String name) {
191 int code = name.codeUnitAt(0);
192 if (code == $g || code == $s) return true;
karlklose 2013/02/12 14:41:19 'return (code == $g || code == $s)'?
erikcorry 2013/02/12 14:50:40 Done.
193 return false;
194 }
195
174 int _calculateHash(String name) { 196 int _calculateHash(String name) {
175 int h = 0; 197 int h = 0;
176 for (int i = 0; i < name.length; i++) { 198 for (int i = 0; i < name.length; i++) {
177 h += name.charCodeAt(i); 199 h += name.charCodeAt(i);
178 h &= 0xffffffff; 200 h &= 0xffffffff;
179 h += h << 10; 201 h += h << 10;
180 h &= 0xffffffff; 202 h &= 0xffffffff;
181 h ^= h >> 6; 203 h ^= h >> 6;
182 h &= 0xffffffff; 204 h &= 0xffffffff;
183 } 205 }
184 return h; 206 return h;
185 } 207 }
186 208
187 int _letterNumber(int x) { 209 int _letterNumber(int x) {
188 if (x >= ALPHABET_CHARACTERS) x %= ALPHABET_CHARACTERS; 210 if (x >= ALPHABET_CHARACTERS) x %= ALPHABET_CHARACTERS;
189 if (x < 26) return $a + x; 211 if (x < 26) return $a + x;
190 return $A + x - 26; 212 return $A + x - 26;
191 } 213 }
192 214
193 int _alphaNumericNumber(int x) { 215 int _alphaNumericNumber(int x) {
194 if (x >= ALPHANUMERIC_CHARACTERS) x %= ALPHANUMERIC_CHARACTERS; 216 if (x >= ALPHANUMERIC_CHARACTERS) x %= ALPHANUMERIC_CHARACTERS;
195 if (x < 26) return $a + x; 217 if (x < 26) return $a + x;
196 if (x < 52) return $A + x - 26; 218 if (x < 52) return $A + x - 26;
197 return $0 + x - 52; 219 return $0 + x - 52;
198 } 220 }
199 221
200 } 222 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698