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

Unified Diff: pkg/analyzer/lib/src/generated/resolver.dart

Issue 1904433003: Remove MemberMap in favor of HashMap (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: pkg/analyzer/lib/src/generated/resolver.dart
diff --git a/pkg/analyzer/lib/src/generated/resolver.dart b/pkg/analyzer/lib/src/generated/resolver.dart
index 6c022fd3328ea53aea80b11c606a9803d7ab7c74..72201914e4b9250d4931de474e02c51943143e2b 100644
--- a/pkg/analyzer/lib/src/generated/resolver.dart
+++ b/pkg/analyzer/lib/src/generated/resolver.dart
@@ -4843,20 +4843,20 @@ class InheritanceManager {
* This is a mapping between each [ClassElement] and a map between the [String] member
* names and the associated [ExecutableElement] in the mixin and superclass chain.
*/
- HashMap<ClassElement, MemberMap> _classLookup;
+ Map<ClassElement, Map<String, ExecutableElement>> _classLookup;
/**
* This is a mapping between each [ClassElement] and a map between the [String] member
* names and the associated [ExecutableElement] in the interface set.
*/
- HashMap<ClassElement, MemberMap> _interfaceLookup;
+ Map<ClassElement, Map<String, ExecutableElement>> _interfaceLookup;
/**
* A map between each visited [ClassElement] and the set of [AnalysisError]s found on
* the class element.
*/
- HashMap<ClassElement, HashSet<AnalysisError>> _errorsInClassElement =
- new HashMap<ClassElement, HashSet<AnalysisError>>();
+ Map<ClassElement, Set<AnalysisError>> _errorsInClassElement =
+ new HashMap<ClassElement, Set<AnalysisError>>();
/**
* Initialize a newly created inheritance manager.
@@ -4865,8 +4865,9 @@ class InheritanceManager {
*/
InheritanceManager(LibraryElement library) {
this._library = library;
- _classLookup = new HashMap<ClassElement, MemberMap>();
- _interfaceLookup = new HashMap<ClassElement, MemberMap>();
+ _classLookup = new HashMap<ClassElement, Map<String, ExecutableElement>>();
+ _interfaceLookup =
+ new HashMap<ClassElement, Map<String, ExecutableElement>>();
}
/**
@@ -4897,7 +4898,8 @@ class InheritanceManager {
* @return a mapping between the set of all members inherited from the passed [ClassElement]
* superclass hierarchy, and the associated [ExecutableElement]
*/
- MemberMap getMapOfMembersInheritedFromClasses(ClassElement classElt) =>
+ Map<String, ExecutableElement> getMapOfMembersInheritedFromClasses(
+ ClassElement classElt) =>
_computeClassChainLookupMap(classElt, new HashSet<ClassElement>());
/**
@@ -4908,7 +4910,8 @@ class InheritanceManager {
* @return a mapping between the set of all string names of the members inherited from the passed
* [ClassElement] interface hierarchy, and the associated [ExecutableElement].
*/
- MemberMap getMapOfMembersInheritedFromInterfaces(ClassElement classElt) =>
+ Map<String, ExecutableElement> getMapOfMembersInheritedFromInterfaces(
+ ClassElement classElt) =>
_computeInterfaceLookupMap(classElt, new HashSet<ClassElement>());
/**
@@ -4927,12 +4930,11 @@ class InheritanceManager {
if (memberName == null || memberName.isEmpty) {
return null;
}
- ExecutableElement executable =
- _computeClassChainLookupMap(classElt, new HashSet<ClassElement>())
- .get(memberName);
+ ExecutableElement executable = _computeClassChainLookupMap(
+ classElt, new HashSet<ClassElement>())[memberName];
if (executable == null) {
- return _computeInterfaceLookupMap(classElt, new HashSet<ClassElement>())
- .get(memberName);
+ return _computeInterfaceLookupMap(classElt, new HashSet<ClassElement>())[
+ memberName];
}
return executable;
}
@@ -4971,11 +4973,11 @@ class InheritanceManager {
if (memberName == null || memberName.isEmpty) {
return result;
}
- List<MemberMap> interfaceMaps =
+ List<Map<String, ExecutableElement>> interfaceMaps =
_gatherInterfaceLookupMaps(classElt, new HashSet<ClassElement>());
if (interfaceMaps != null) {
- for (MemberMap interfaceMap in interfaceMaps) {
- ExecutableElement overriddenElement = interfaceMap.get(memberName);
+ for (Map<String, ExecutableElement> interfaceMap in interfaceMaps) {
+ ExecutableElement overriddenElement = interfaceMap[memberName];
if (overriddenElement != null) {
if (overriddenElement is MultiplyInheritedExecutableElement) {
MultiplyInheritedExecutableElement multiplyInheritedElement =
@@ -5046,13 +5048,13 @@ class InheritanceManager {
* @return a mapping between the set of all string names of the members inherited from the passed
* [ClassElement] superclass hierarchy, and the associated [ExecutableElement]
*/
- MemberMap _computeClassChainLookupMap(
+ Map<String, ExecutableElement> _computeClassChainLookupMap(
ClassElement classElt, HashSet<ClassElement> visitedClasses) {
- MemberMap resultMap = _classLookup[classElt];
+ Map<String, ExecutableElement> resultMap = _classLookup[classElt];
if (resultMap != null) {
return resultMap;
} else {
- resultMap = new MemberMap();
+ resultMap = new HashMap<String, ExecutableElement>();
}
ClassElement superclassElt = null;
InterfaceType supertype = classElt.supertype;
@@ -5067,7 +5069,7 @@ class InheritanceManager {
if (!visitedClasses.contains(superclassElt)) {
visitedClasses.add(superclassElt);
try {
- resultMap = new MemberMap.from(
+ resultMap = new HashMap<String, ExecutableElement>.from(
_computeClassChainLookupMap(superclassElt, visitedClasses));
//
// Substitute the super types down the hierarchy.
@@ -5100,8 +5102,9 @@ class InheritanceManager {
if (!visitedClasses.contains(mixinElement)) {
visitedClasses.add(mixinElement);
try {
- MemberMap map = new MemberMap.from(
- _computeClassChainLookupMap(mixinElement, visitedClasses));
+ Map<String, ExecutableElement> map =
+ new HashMap<String, ExecutableElement>.from(
+ _computeClassChainLookupMap(mixinElement, visitedClasses));
//
// Substitute the super types down the hierarchy.
//
@@ -5113,18 +5116,15 @@ class InheritanceManager {
//
// Add the members from map into result map.
//
- for (int j = 0; j < map.size; j++) {
- String key = map.getKey(j);
- ExecutableElement value = map.getValue(j);
- if (key != null) {
- ClassElement definingClass = value
- .getAncestor((Element element) => element is ClassElement);
- if (!definingClass.type.isObject) {
- ExecutableElement existingValue = resultMap.get(key);
- if (existingValue == null ||
- (existingValue != null && !_isAbstract(value))) {
- resultMap.put(key, value);
- }
+ for (String memberName in map.keys) {
+ ExecutableElement value = map[memberName];
+ ClassElement definingClass = value
+ .getAncestor((Element element) => element is ClassElement);
+ if (!definingClass.type.isObject) {
+ ExecutableElement existingValue = resultMap[memberName];
+ if (existingValue == null ||
+ (existingValue != null && !_isAbstract(value))) {
+ resultMap[memberName] = value;
}
}
}
@@ -5219,18 +5219,18 @@ class InheritanceManager {
* @return a mapping between the set of all string names of the members inherited from the passed
* [ClassElement] interface hierarchy, and the associated [ExecutableElement]
*/
- MemberMap _computeInterfaceLookupMap(
+ Map<String, ExecutableElement> _computeInterfaceLookupMap(
ClassElement classElt, HashSet<ClassElement> visitedInterfaces) {
- MemberMap resultMap = _interfaceLookup[classElt];
+ Map<String, ExecutableElement> resultMap = _interfaceLookup[classElt];
if (resultMap != null) {
return resultMap;
}
- List<MemberMap> lookupMaps =
+ List<Map<String, ExecutableElement>> lookupMaps =
_gatherInterfaceLookupMaps(classElt, visitedInterfaces);
if (lookupMaps == null) {
- resultMap = new MemberMap();
+ resultMap = new HashMap<String, ExecutableElement>();
} else {
- HashMap<String, List<ExecutableElement>> unionMap =
+ Map<String, List<ExecutableElement>> unionMap =
_unionInterfaceLookupMaps(lookupMaps);
resultMap = _resolveInheritanceLookup(classElt, unionMap);
}
@@ -5252,7 +5252,7 @@ class InheritanceManager {
* are no classes above this one in the class hierarchy. Otherwise, a list of interface
* lookup maps.
*/
- List<MemberMap> _gatherInterfaceLookupMaps(
+ List<Map<String, ExecutableElement>> _gatherInterfaceLookupMaps(
ClassElement classElt, HashSet<ClassElement> visitedInterfaces) {
InterfaceType supertype = classElt.supertype;
ClassElement superclassElement =
@@ -5260,7 +5260,8 @@ class InheritanceManager {
List<InterfaceType> mixins = classElt.mixins;
List<InterfaceType> interfaces = classElt.interfaces;
// Recursively collect the list of mappings from all of the interface types
- List<MemberMap> lookupMaps = new List<MemberMap>();
+ List<Map<String, ExecutableElement>> lookupMaps =
+ new List<Map<String, ExecutableElement>>();
//
// Superclass element
//
@@ -5271,9 +5272,9 @@ class InheritanceManager {
//
// Recursively compute the map for the super type.
//
- MemberMap map =
+ Map<String, ExecutableElement> map =
_computeInterfaceLookupMap(superclassElement, visitedInterfaces);
- map = new MemberMap.from(map);
+ map = new HashMap<String, ExecutableElement>.from(map);
//
// Substitute the super type down the hierarchy.
//
@@ -5303,9 +5304,9 @@ class InheritanceManager {
//
// Recursively compute the map for the mixin.
//
- MemberMap map =
+ Map<String, ExecutableElement> map =
_computeInterfaceLookupMap(mixinElement, visitedInterfaces);
- map = new MemberMap.from(map);
+ map = new HashMap<String, ExecutableElement>.from(map);
//
// Substitute the mixin type down the hierarchy.
//
@@ -5335,9 +5336,9 @@ class InheritanceManager {
//
// Recursively compute the map for the interfaces.
//
- MemberMap map =
+ Map<String, ExecutableElement> map =
_computeInterfaceLookupMap(interfaceElement, visitedInterfaces);
- map = new MemberMap.from(map);
+ map = new HashMap<String, ExecutableElement>.from(map);
//
// Substitute the supertypes down the hierarchy
//
@@ -5400,14 +5401,14 @@ class InheritanceManager {
* @param type the type that will be recorded into the passed map
* @param doIncludeAbstract `true` if abstract members will be put into the map
*/
- void _recordMapWithClassMembers(
- MemberMap map, InterfaceType type, bool doIncludeAbstract) {
+ void _recordMapWithClassMembers(Map<String, ExecutableElement> map,
+ InterfaceType type, bool doIncludeAbstract) {
List<MethodElement> methods = type.methods;
for (MethodElement method in methods) {
if (method.isAccessibleIn(_library) &&
!method.isStatic &&
(doIncludeAbstract || !method.isAbstract)) {
- map.put(method.name, method);
+ map[method.name] = method;
}
}
List<PropertyAccessorElement> accessors = type.accessors;
@@ -5415,7 +5416,7 @@ class InheritanceManager {
if (accessor.isAccessibleIn(_library) &&
!accessor.isStatic &&
(doIncludeAbstract || !accessor.isAbstract)) {
- map.put(accessor.name, accessor);
+ map[accessor.name] = accessor;
}
}
}
@@ -5454,9 +5455,10 @@ class InheritanceManager {
* defined in superclasses of [classElt].
* @return the inheritance lookup map for [classElt].
*/
- MemberMap _resolveInheritanceLookup(ClassElement classElt,
- HashMap<String, List<ExecutableElement>> unionMap) {
- MemberMap resultMap = new MemberMap();
+ Map<String, ExecutableElement> _resolveInheritanceLookup(
+ ClassElement classElt, Map<String, List<ExecutableElement>> unionMap) {
+ Map<String, ExecutableElement> resultMap =
+ new HashMap<String, ExecutableElement>();
unionMap.forEach((String key, List<ExecutableElement> list) {
int numOfEltsWithMatchingNames = list.length;
if (numOfEltsWithMatchingNames == 1) {
@@ -5467,7 +5469,7 @@ class InheritanceManager {
// different interfaces, but they both have the same signature, so it is
// the method inherited.
//
- resultMap.put(key, list[0]);
+ resultMap[key] = list[0];
} else {
//
// Then numOfEltsWithMatchingNames > 1, check for the warning cases.
@@ -5539,7 +5541,7 @@ class InheritanceManager {
// Tests: InheritanceManagerTest.
// test_getMapOfMembersInheritedFromInterfaces_union_oneSubtype_*
//
- resultMap.put(key, elements[subtypesOfAllOtherTypesIndexes[0]]);
+ resultMap[key] = elements[subtypesOfAllOtherTypesIndexes[0]];
} else {
if (subtypesOfAllOtherTypesIndexes.isEmpty) {
//
@@ -5593,7 +5595,7 @@ class InheritanceManager {
}
ExecutableElement mergedExecutableElement =
_computeMergedExecutableElement(elementArrayToMerge);
- resultMap.put(key, mergedExecutableElement);
+ resultMap[key] = mergedExecutableElement;
}
}
} else {
@@ -5611,24 +5613,21 @@ class InheritanceManager {
}
/**
- * Loop through all of the members in some [MemberMap], performing type parameter
- * substitutions using a passed supertype.
- *
- * @param superType the supertype to substitute into the members of the [MemberMap]
- * @param map the MemberMap to perform the substitutions on
+ * Loop through all of the members in the given [map], and perform type
+ * parameter substitutions using a passed [supertype].
*/
void _substituteTypeParametersDownHierarchy(
- InterfaceType superType, MemberMap map) {
- for (int i = 0; i < map.size; i++) {
- ExecutableElement executableElement = map.getValue(i);
+ InterfaceType superType, Map<String, ExecutableElement> map) {
+ for (String memberName in map.keys) {
+ ExecutableElement executableElement = map[memberName];
if (executableElement is MethodMember) {
executableElement =
MethodMember.from(executableElement as MethodMember, superType);
- map.setValue(i, executableElement);
+ map[memberName] = executableElement;
} else if (executableElement is PropertyAccessorMember) {
executableElement = PropertyAccessorMember.from(
executableElement as PropertyAccessorMember, superType);
- map.setValue(i, executableElement);
+ map[memberName] = executableElement;
}
}
}
@@ -5642,28 +5641,22 @@ class InheritanceManager {
* @param lookupMaps the maps to be unioned together.
* @return the resulting union map.
*/
- HashMap<String, List<ExecutableElement>> _unionInterfaceLookupMaps(
- List<MemberMap> lookupMaps) {
- HashMap<String, List<ExecutableElement>> unionMap =
+ Map<String, List<ExecutableElement>> _unionInterfaceLookupMaps(
+ List<Map<String, ExecutableElement>> lookupMaps) {
+ Map<String, List<ExecutableElement>> unionMap =
new HashMap<String, List<ExecutableElement>>();
- for (MemberMap lookupMap in lookupMaps) {
- int lookupMapSize = lookupMap.size;
- for (int i = 0; i < lookupMapSize; i++) {
- // Get the string key, if null, break.
- String key = lookupMap.getKey(i);
- if (key == null) {
- break;
- }
+ for (Map<String, ExecutableElement> lookupMap in lookupMaps) {
+ for (String memberName in lookupMap.keys) {
// Get the list value out of the unionMap
- List<ExecutableElement> list = unionMap[key];
+ List<ExecutableElement> list = unionMap[memberName];
// If we haven't created such a map for this key yet, do create it and
// put the list entry into the unionMap.
if (list == null) {
list = new List<ExecutableElement>();
- unionMap[key] = list;
+ unionMap[memberName] = list;
}
// Fetch the entry out of this lookupMap
- ExecutableElement newExecutableElementEntry = lookupMap.getValue(i);
+ ExecutableElement newExecutableElementEntry = lookupMap[memberName];
if (list.isEmpty) {
// If the list is empty, just the new value
list.add(newExecutableElementEntry);
@@ -5892,166 +5885,6 @@ class INIT_STATE extends Enum<INIT_STATE> {
}
/**
- * This class is used to replace uses of `HashMap<String, ExecutableElement>`
- * which are not as performant as this class.
- */
-class MemberMap {
- /**
- * The current size of this map.
- */
- int _size = 0;
-
- /**
- * The array of keys.
- */
- List<String> _keys;
-
- /**
- * The array of ExecutableElement values.
- */
- List<ExecutableElement> _values;
-
- /**
- * Initialize a newly created member map to have the given [initialCapacity].
- * The map will grow if needed.
- */
- MemberMap([int initialCapacity = 10]) {
- _initArrays(initialCapacity);
- }
-
- /**
- * Initialize a newly created member map to contain the same members as the
- * given [memberMap].
- */
- MemberMap.from(MemberMap memberMap) {
- _initArrays(memberMap._size + 5);
- for (int i = 0; i < memberMap._size; i++) {
- _keys[i] = memberMap._keys[i];
- _values[i] = memberMap._values[i];
- }
- _size = memberMap._size;
- }
-
- /**
- * The size of the map.
- *
- * @return the size of the map.
- */
- int get size => _size;
-
- /**
- * Given some key, return the ExecutableElement value from the map, if the key does not exist in
- * the map, `null` is returned.
- *
- * @param key some key to look up in the map
- * @return the associated ExecutableElement value from the map, if the key does not exist in the
- * map, `null` is returned
- */
- ExecutableElement get(String key) {
- for (int i = 0; i < _size; i++) {
- if (_keys[i] != null && _keys[i] == key) {
- return _values[i];
- }
- }
- return null;
- }
-
- /**
- * Get and return the key at the specified location. If the key/value pair has been removed from
- * the set, then `null` is returned.
- *
- * @param i some non-zero value less than size
- * @return the key at the passed index
- * @throw ArrayIndexOutOfBoundsException this exception is thrown if the passed index is less than
- * zero or greater than or equal to the capacity of the arrays
- */
- String getKey(int i) => _keys[i];
-
- /**
- * Get and return the ExecutableElement at the specified location. If the key/value pair has been
- * removed from the set, then then `null` is returned.
- *
- * @param i some non-zero value less than size
- * @return the key at the passed index
- * @throw ArrayIndexOutOfBoundsException this exception is thrown if the passed index is less than
- * zero or greater than or equal to the capacity of the arrays
- */
- ExecutableElement getValue(int i) => _values[i];
-
- /**
- * Given some key/value pair, store the pair in the map. If the key exists already, then the new
- * value overrides the old value.
- *
- * @param key the key to store in the map
- * @param value the ExecutableElement value to store in the map
- */
- void put(String key, ExecutableElement value) {
- // If we already have a value with this key, override the value
- for (int i = 0; i < _size; i++) {
- if (_keys[i] != null && _keys[i] == key) {
- _values[i] = value;
- return;
- }
- }
- // If needed, double the size of our arrays and copy values over in both
- // arrays
- if (_size == _keys.length) {
- int newArrayLength = _size * 2;
- List<String> keys_new_array = new List<String>(newArrayLength);
- List<ExecutableElement> values_new_array =
- new List<ExecutableElement>(newArrayLength);
- for (int i = 0; i < _size; i++) {
- keys_new_array[i] = _keys[i];
- }
- for (int i = 0; i < _size; i++) {
- values_new_array[i] = _values[i];
- }
- _keys = keys_new_array;
- _values = values_new_array;
- }
- // Put new value at end of array
- _keys[_size] = key;
- _values[_size] = value;
- _size++;
- }
-
- /**
- * Given some [String] key, this method replaces the associated key and value pair with
- * `null`. The size is not decremented with this call, instead it is expected that the users
- * check for `null`.
- *
- * @param key the key of the key/value pair to remove from the map
- */
- void remove(String key) {
- for (int i = 0; i < _size; i++) {
- if (_keys[i] == key) {
- _keys[i] = null;
- _values[i] = null;
- return;
- }
- }
- }
-
- /**
- * Sets the ExecutableElement at the specified location.
- *
- * @param i some non-zero value less than size
- * @param value the ExecutableElement value to store in the map
- */
- void setValue(int i, ExecutableElement value) {
- _values[i] = value;
- }
-
- /**
- * Initializes [keys] and [values].
- */
- void _initArrays(int initialCapacity) {
- _keys = new List<String>(initialCapacity);
- _values = new List<ExecutableElement>(initialCapacity);
- }
-}
-
-/**
* Instances of the class `OverrideVerifier` visit all of the declarations in a compilation
* unit to verify that if they have an override annotation it is being used correctly.
*/
« no previous file with comments | « pkg/analyzer/lib/src/generated/error_verifier.dart ('k') | pkg/analyzer/test/generated/inheritance_manager_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698