Skip to content

Commit 7e171fc

Browse files
authored
Make a few optimizations (#3660)
* Make 'allFields' a late field instead * Cache _inheritedElements in allModelElements call * Make documentedWhere a late field
1 parent 5b1a9dd commit 7e171fc

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

lib/src/model/inheriting_container.dart

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,15 @@ abstract class InheritingContainer extends Container
104104
packageGraph.rendererFactory.languageFeatureRenderer)
105105
.toList();
106106

107-
late final List<ModelElement> _allModelElements = [
108-
...super.allModelElements,
109-
...typeParameters,
110-
];
107+
late final List<ModelElement> _allModelElements = () {
108+
_inheritedElementsCache = _inheritedElements;
109+
var result = [
110+
...super.allModelElements,
111+
...typeParameters,
112+
];
113+
_inheritedElementsCache = null;
114+
return result;
115+
}();
111116

112117
Iterable<Method> get inheritedMethods {
113118
var methodNames = declaredMethods.map((m) => m.element.name).toSet();
@@ -144,7 +149,9 @@ abstract class InheritingContainer extends Container
144149
late final List<DefinedElementType> publicSuperChain =
145150
model_utils.filterNonPublic(superChain).toList(growable: false);
146151

152+
List<ExecutableElement>? _inheritedElementsCache;
147153
List<ExecutableElement> get _inheritedElements {
154+
if (_inheritedElementsCache != null) return _inheritedElementsCache!;
148155
if (element is ClassElement && (element as ClassElement).isDartCoreObject) {
149156
return const <ExecutableElement>[];
150157
}
@@ -193,7 +200,7 @@ abstract class InheritingContainer extends Container
193200
}
194201

195202
/// All fields defined on this container, _including inherited fields_.
196-
List<Field> get allFields {
203+
late List<Field> allFields = () {
197204
var inheritedAccessorElements = {
198205
..._inheritedElements.whereType<PropertyAccessorElement>()
199206
};
@@ -243,7 +250,7 @@ abstract class InheritingContainer extends Container
243250
});
244251

245252
return fields;
246-
}
253+
}();
247254

248255
@override
249256
late final List<Method> declaredMethods = element.methods

lib/src/model/package.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class Package extends LibraryContainer
175175

176176
/// Returns the location of documentation for this package, for linkToRemote
177177
/// and canonicalization decision making.
178-
DocumentLocation get documentedWhere {
178+
late DocumentLocation documentedWhere = () {
179179
if (isLocal && isPublic) {
180180
return DocumentLocation.local;
181181
}
@@ -186,7 +186,7 @@ class Package extends LibraryContainer
186186
return DocumentLocation.remote;
187187
}
188188
return DocumentLocation.missing;
189-
}
189+
}();
190190

191191
@override
192192
String get enclosingName => packageGraph.defaultPackageName;

0 commit comments

Comments
 (0)