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

Unified Diff: pkg/compiler/lib/src/js_backend/minify_namer.dart

Issue 1428853004: Use better names for captured variables in closures. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Address comments Created 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/compiler/lib/src/js_backend/frequency_namer.dart ('k') | pkg/compiler/lib/src/js_backend/namer.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/compiler/lib/src/js_backend/minify_namer.dart
diff --git a/pkg/compiler/lib/src/js_backend/minify_namer.dart b/pkg/compiler/lib/src/js_backend/minify_namer.dart
index b781bc0ad0eb9a8155c638cb272e0a126a786f64..0ba2e4e453e684116b0ae8d0b5e1b9482c70fee0 100644
--- a/pkg/compiler/lib/src/js_backend/minify_namer.dart
+++ b/pkg/compiler/lib/src/js_backend/minify_namer.dart
@@ -34,19 +34,17 @@ class MinifyNamer extends Namer with _MinifiedFieldNamer,
/// minified names will always avoid clashing with annotated names or natives.
@override
String _generateFreshStringForName(String proposedName,
- Set<String> usedNames,
- Map<String, String> suggestedNames,
+ NamingScope scope,
{bool sanitizeForNatives: false,
bool sanitizeForAnnotations: false}) {
String freshName;
- String suggestion = suggestedNames[proposedName];
- if (suggestion != null && !usedNames.contains(suggestion)) {
+ String suggestion = scope.suggestName(proposedName);
+ if (suggestion != null && scope.isUnused(suggestion)) {
freshName = suggestion;
} else {
- freshName = _getUnusedName(proposedName, usedNames,
- suggestedNames.values);
+ freshName = _getUnusedName(proposedName, scope);
}
- usedNames.add(freshName);
+ scope.registerUse(freshName);
return freshName;
}
@@ -85,12 +83,14 @@ class MinifyNamer extends Namer with _MinifiedFieldNamer,
String disambiguatedName = name;
reservePublicMemberName(name, disambiguatedName);
}
- usedInstanceNames.add(name);
+ instanceScope.registerUse(name);
// Getter and setter names are autogenerated by prepending 'g' and 's' to
// field names. Therefore there are some field names we don't want to
// use. It is implicit in the next line that the banned prefix is
// only one character.
- if (_hasBannedPrefix(name)) usedInstanceNames.add(name.substring(1));
+ if (_hasBannedPrefix(name)) {
+ instanceScope.registerUse(name.substring(1));
+ }
}
// These popular names are present in most programs and deserve
@@ -99,8 +99,7 @@ class MinifyNamer extends Namer with _MinifiedFieldNamer,
// minifier was less stable from version to version of the program being
// minified.
_populateSuggestedNames(
- suggestedInstanceNames,
- usedInstanceNames,
+ instanceScope,
const <String>[
r'$add', r'add$1', r'$and',
r'$or',
@@ -115,8 +114,7 @@ class MinifyNamer extends Namer with _MinifiedFieldNamer,
r'$tdiv', r'toString$0']);
_populateSuggestedNames(
- suggestedGlobalNames,
- usedGlobalNames,
+ globalScope,
const <String>[
r'Object', 'wrapException', r'$eq', r'S', r'ioore',
r'UnsupportedError$', r'length', r'$sub',
@@ -135,8 +133,7 @@ class MinifyNamer extends Namer with _MinifiedFieldNamer,
]);
}
- void _populateSuggestedNames(Map<String, String> suggestionMap,
- Set<String> used,
+ void _populateSuggestedNames(NamingScope scope,
List<String> suggestions) {
int c = $a - 1;
String letter;
@@ -145,9 +142,9 @@ class MinifyNamer extends Namer with _MinifiedFieldNamer,
assert(c != $Z);
c = (c == $z) ? $A : c + 1;
letter = new String.fromCharCodes([c]);
- } while (_hasBannedPrefix(letter) || used.contains(letter));
- assert(suggestionMap[name] == null);
- suggestionMap[name] = letter;
+ } while (_hasBannedPrefix(letter) || scope.isUsed(letter));
+ assert(!scope.hasSuggestion(name));
+ scope.addSuggestion(name, letter);
}
}
@@ -156,8 +153,7 @@ class MinifyNamer extends Namer with _MinifiedFieldNamer,
// is slightly less efficient than just getting the next name in a series,
// but it means that small changes in the input program will give smallish
// changes in the output, which can be useful for diffing etc.
- String _getUnusedName(String proposedName, Set<String> usedNames,
- Iterable<String> suggestions) {
+ String _getUnusedName(String proposedName, NamingScope scope) {
int hash = _calculateHash(proposedName);
// Avoid very small hashes that won't try many names.
hash = hash < 1000 ? hash * 314159 : hash; // Yes, it's prime.
@@ -177,10 +173,10 @@ class MinifyNamer extends Namer with _MinifiedFieldNamer,
h2 ~/= ALPHANUMERIC_CHARACTERS;
}
final candidate = new String.fromCharCodes(codes);
- if (!usedNames.contains(candidate) &&
+ if (scope.isUnused(candidate) &&
!jsReserved.contains(candidate) &&
!_hasBannedPrefix(candidate) &&
- (n != 1 || !suggestions.contains(candidate))) {
+ (n != 1 || scope.isSuggestion(candidate))) {
return candidate;
}
// Try again with a slightly different hash. After around 10 turns
@@ -188,7 +184,7 @@ class MinifyNamer extends Namer with _MinifiedFieldNamer,
h ~/= 7;
}
}
- return _badName(hash, usedNames);
+ return _badName(hash, scope);
}
/// Instance members starting with g and s are reserved for getters and
@@ -217,7 +213,7 @@ class MinifyNamer extends Namer with _MinifiedFieldNamer,
/// If we can't find a hash based name in the three-letter space, then base
/// the name on a letter and a counter.
- String _badName(int hash, Set<String> usedNames) {
+ String _badName(int hash, NamingScope scope) {
int count = _badNames.putIfAbsent(hash, () => 0);
String startLetter =
new String.fromCharCodes([_letterNumber(hash + count)]);
@@ -226,7 +222,7 @@ class MinifyNamer extends Namer with _MinifiedFieldNamer,
int i = 0;
do {
name = "$startLetter${i++}";
- } while (usedNames.contains(name));
+ } while (scope.isUsed(name));
// We don't need to check for banned prefix because the name is in the form
// xnnn, where nnn is a number. There can be no getter or setter called
// gnnn since that would imply a numeric field name.
« no previous file with comments | « pkg/compiler/lib/src/js_backend/frequency_namer.dart ('k') | pkg/compiler/lib/src/js_backend/namer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698