Skip to content

Commit 18b4ac0

Browse files
authored
Improve performance by 22% via memoization. (#2255)
* Improve performance via memoization. I tested with the camera flutter plugin. Before this change it took 67 seconds. Multiple fixes brought down the duration: * memoize Package.fileType => 53 seconds (79% duration) * memoize ModelElement.name => 52 seconds (78% duration)
1 parent b0e2786 commit 18b4ac0

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

lib/src/model/model_element.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -712,12 +712,11 @@ abstract class ModelElement extends Canonicalization
712712
var confidence = highestScore - secondHighestScore;
713713
var message =
714714
'${candidateLibraries.map((l) => l.name)} -> ${candidateLibraries.last.name} (confidence ${confidence.toStringAsPrecision(4)})';
715-
var debugLines = <String>[];
716-
debugLines.addAll(scoredCandidates.map((s) => '${s.toString()}'));
717715

718716
if (confidence < config.ambiguousReexportScorerMinConfidence) {
719717
warnable.warn(PackageWarning.ambiguousReexport,
720-
message: message, extendedDebug: debugLines);
718+
message: message,
719+
extendedDebug: scoredCandidates.map((s) => '$s'));
721720
}
722721
}
723722
if (candidateLibraries.isNotEmpty) {
@@ -977,8 +976,10 @@ abstract class ModelElement extends Canonicalization
977976
_modelType = type;
978977
}
979978

979+
String _name;
980+
980981
@override
981-
String get name => element.name;
982+
String get name => _name ??= element.name;
982983

983984
@override
984985
String get oneLineDoc => _documentation.asOneLiner;

lib/src/model/package.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,17 @@ class Package extends LibraryContainer
194194

195195
String get filePath => 'index.$fileType';
196196

197+
String _fileType;
198+
197199
String get fileType {
198200
// TODO(jdkoren): Provide a way to determine file type of a remote package's
199201
// docs. Perhaps make this configurable through dartdoc options.
200202
// In theory, a remote package could be documented in any supported format.
201203
// In practice, devs depend on Dart, Flutter, and/or packages fetched
202204
// from pub.dev, and we know that all of those use html docs.
203-
if (package.documentedWhere == DocumentLocation.remote) {
204-
return 'html';
205-
}
206-
return config.format;
205+
return _fileType ??= (package.documentedWhere == DocumentLocation.remote)
206+
? 'html'
207+
: config.format;
207208
}
208209

209210
@override

0 commit comments

Comments
 (0)