Skip to content

Commit 6580712

Browse files
authored
Fix many minor performance problems in tests and main code (#1828)
1 parent ed35615 commit 6580712

File tree

4 files changed

+70
-61
lines changed

4 files changed

+70
-61
lines changed

lib/src/dartdoc_options.dart

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -806,14 +806,21 @@ abstract class _DartdocFileOption<T> implements DartdocOption<T> {
806806
return _valueAtFromFiles(dir) ?? defaultsTo;
807807
}
808808

809+
Map<String, T> __valueAtFromFiles = new Map();
810+
// The value of this option from files will not change unless files are
811+
// modified during execution (not allowed in Dartdoc).
809812
T _valueAtFromFiles(Directory dir) {
810-
_OptionValueWithContext valueWithContext;
811-
if (parentDirOverridesChild) {
812-
valueWithContext = _valueAtFromFilesLastFound(dir);
813-
} else {
814-
valueWithContext = _valueAtFromFilesFirstFound(dir);
813+
String key = pathLib.canonicalize(dir.path);
814+
if (!__valueAtFromFiles.containsKey(key)) {
815+
_OptionValueWithContext valueWithContext;
816+
if (parentDirOverridesChild) {
817+
valueWithContext = _valueAtFromFilesLastFound(dir);
818+
} else {
819+
valueWithContext = _valueAtFromFilesFirstFound(dir);
820+
}
821+
__valueAtFromFiles[key] = _handlePathsInContext(valueWithContext);
815822
}
816-
return _handlePathsInContext(valueWithContext);
823+
return __valueAtFromFiles[key];
817824
}
818825

819826
/// Searches all dartdoc_options files through parent directories,

lib/src/tool_runner.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,20 @@ class ToolRunner {
2020
/// Takes a [toolConfiguration] that describes all of the available tools.
2121
/// An optional `errorCallback` will be called for each error message
2222
/// generated by the tool.
23-
ToolRunner(this.toolConfiguration, [this._errorCallback])
24-
: temporaryDirectory =
25-
Directory.systemTemp.createTempSync('dartdoc_tools_');
23+
ToolRunner(this.toolConfiguration, [this._errorCallback]);
2624

2725
final ToolConfiguration toolConfiguration;
28-
final Directory temporaryDirectory;
2926
final ToolErrorCallback _errorCallback;
3027
int _temporaryFileCount = 0;
3128

29+
Directory _temporaryDirectory;
30+
Directory get temporaryDirectory {
31+
if (_temporaryDirectory == null) {
32+
_temporaryDirectory = Directory.systemTemp.createTempSync('dartdoc_tools_');
33+
}
34+
return _temporaryDirectory;
35+
}
36+
3237
void _error(String message) {
3338
if (_errorCallback != null) {
3439
_errorCallback(message);
@@ -47,7 +52,7 @@ class ToolRunner {
4752
///
4853
/// This will remove any temporary files created by the tool runner.
4954
void dispose() {
50-
if (temporaryDirectory.existsSync())
55+
if (_temporaryDirectory != null && temporaryDirectory.existsSync())
5156
temporaryDirectory.deleteSync(recursive: true);
5257
}
5358

test/dartdoc_test.dart

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ void main() {
2020
group('dartdoc with generators', () {
2121
Directory tempDir;
2222
List<String> outputParam;
23-
setUp(() {
23+
setUpAll(() {
2424
tempDir = Directory.systemTemp.createTempSync('dartdoc.test.');
2525
outputParam = ['--output', tempDir.path];
2626
});
2727

28-
tearDown(() {
28+
tearDownAll(() {
2929
delete(tempDir);
3030
});
3131

@@ -40,7 +40,7 @@ void main() {
4040
DartdocResults results;
4141
PackageGraph p;
4242

43-
setUp(() async {
43+
setUpAll(() async {
4444
dartdoc = await buildDartdoc([], testPackageOptions);
4545
results = await dartdoc.generateDocsBase();
4646
p = results.packageGraph;
@@ -73,23 +73,23 @@ void main() {
7373
expect(Something.isPublic, isTrue);
7474
expect(Something.displayedCategories, isNotEmpty);
7575
});
76+
});
7677

77-
test('errors generate errors even when warnings are off', () async {
78-
Dartdoc dartdoc = await buildDartdoc([], testPackageToolError);
79-
DartdocResults results = await dartdoc.generateDocsBase();
80-
PackageGraph p = results.packageGraph;
81-
Iterable<String> unresolvedToolErrors = p
82-
.packageWarningCounter.countedWarnings.values
83-
.expand<String>((Set<Tuple2<PackageWarning, String>> s) => s
84-
.where((Tuple2<PackageWarning, String> t) =>
85-
t.item1 == PackageWarning.toolError)
86-
.map<String>((Tuple2<PackageWarning, String> t) => t.item2));
87-
88-
expect(p.packageWarningCounter.errorCount, equals(1));
89-
expect(unresolvedToolErrors.length, equals(1));
90-
expect(unresolvedToolErrors.first,
91-
contains('Tool "drill" returned non-zero exit code'));
92-
});
78+
test('errors generate errors even when warnings are off', () async {
79+
Dartdoc dartdoc = await buildDartdoc([], testPackageToolError);
80+
DartdocResults results = await dartdoc.generateDocsBase();
81+
PackageGraph p = results.packageGraph;
82+
Iterable<String> unresolvedToolErrors = p
83+
.packageWarningCounter.countedWarnings.values
84+
.expand<String>((Set<Tuple2<PackageWarning, String>> s) => s
85+
.where((Tuple2<PackageWarning, String> t) =>
86+
t.item1 == PackageWarning.toolError)
87+
.map<String>((Tuple2<PackageWarning, String> t) => t.item2));
88+
89+
expect(p.packageWarningCounter.errorCount, equals(1));
90+
expect(unresolvedToolErrors.length, equals(1));
91+
expect(unresolvedToolErrors.first,
92+
contains('Tool "drill" returned non-zero exit code'));
9393
});
9494

9595
group('Invoking command-line dartdoc', () {
@@ -118,7 +118,7 @@ void main() {
118118
DartdocResults results;
119119
Package testPackageOptions;
120120

121-
setUp(() async {
121+
setUpAll(() async {
122122
results = await (await buildDartdoc(
123123
['--link-to-remote'], testPackageOptionsImporter))
124124
.generateDocsBase();

test/model_test.dart

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,11 @@ void main() {
184184
Class htmlInjection;
185185
Method injectSimpleHtml;
186186

187-
setUp(() {
187+
setUpAll(() {
188188
htmlInjection =
189189
exLibrary.classes.firstWhere((c) => c.name == 'HtmlInjection');
190190
injectSimpleHtml = htmlInjection.allInstanceMethods
191191
.firstWhere((m) => m.name == 'injectSimpleHtml');
192-
packageGraph.allLocalModelElements.forEach((m) => m.documentation);
193192
});
194193
test("doesn't inject HTML if --inject-html option is not present", () {
195194
expect(
@@ -552,7 +551,7 @@ void main() {
552551
reexportTwoLib;
553552
Class SomeClass, SomeOtherClass, YetAnotherClass, AUnicornClass;
554553

555-
setUp(() {
554+
setUpAll(() {
556555
dartAsyncLib = utils.testPackageGraphSdk.libraries
557556
.firstWhere((l) => l.name == 'dart:async');
558557

@@ -699,7 +698,7 @@ void main() {
699698
Method withMacro, withMacro2, withPrivateMacro, withUndefinedMacro;
700699
EnumField macroReferencedHere;
701700

702-
setUp(() {
701+
setUpAll(() {
703702
dog = exLibrary.classes.firstWhere((c) => c.name == 'Dog');
704703
withMacro =
705704
dog.allInstanceMethods.firstWhere((m) => m.name == 'withMacro');
@@ -751,7 +750,7 @@ void main() {
751750
Method withAnimationBadHeight;
752751
Method withAnimationUnknownArg;
753752

754-
setUp(() {
753+
setUpAll(() {
755754
documentationErrors = errorLibrary.classes
756755
.firstWhere((c) => c.name == 'DocumentationErrors');
757756
withInvalidNamedAnimation = documentationErrors.allInstanceMethods
@@ -768,7 +767,6 @@ void main() {
768767
.firstWhere((m) => m.name == 'withAnimationBadHeight');
769768
withAnimationUnknownArg = documentationErrors.allInstanceMethods
770769
.firstWhere((m) => m.name == 'withAnimationUnknownArg');
771-
packageGraphErrors.allLocalModelElements.forEach((m) => m.documentation);
772770
});
773771

774772
test("warns with invalidly-named animation within the method documentation",
@@ -850,7 +848,7 @@ void main() {
850848
Method withAnimationInline;
851849
Method withAnimationOutOfOrder;
852850

853-
setUp(() {
851+
setUpAll(() {
854852
dog = exLibrary.classes.firstWhere((c) => c.name == 'Dog');
855853
withAnimation =
856854
dog.allInstanceMethods.firstWhere((m) => m.name == 'withAnimation');
@@ -866,7 +864,6 @@ void main() {
866864
.firstWhere((m) => m.name == 'withAnimationInline');
867865
withAnimationOutOfOrder = dog.allInstanceMethods
868866
.firstWhere((m) => m.name == 'withAnimationOutOfOrder');
869-
packageGraph.allLocalModelElements.forEach((m) => m.documentation);
870867
});
871868

872869
test("renders an unnamed animation within the method documentation", () {
@@ -920,7 +917,7 @@ void main() {
920917
Field aImplementingThingy;
921918
Accessor aImplementingThingyAccessor;
922919

923-
setUp(() {
920+
setUpAll(() {
924921
BaseThingy =
925922
fakeLibrary.classes.firstWhere((c) => c.name == 'BaseThingy');
926923
BaseThingy2 =
@@ -966,7 +963,7 @@ void main() {
966963

967964
ModelFunction short;
968965

969-
setUp(() {
966+
setUpAll(() {
970967
incorrectDocReferenceFromEx = exLibrary.constants
971968
.firstWhere((c) => c.name == 'incorrectDocReferenceFromEx');
972969
B = exLibrary.classes.firstWhere((c) => c.name == 'B');
@@ -1003,7 +1000,7 @@ void main() {
10031000
Class DocumentWithATable;
10041001
String docsAsHtml;
10051002

1006-
setUp(() {
1003+
setUpAll(() {
10071004
DocumentWithATable = fakeLibrary.classes
10081005
.firstWhere((cls) => cls.name == 'DocumentWithATable');
10091006
docsAsHtml = DocumentWithATable.documentationAsHtml;
@@ -1032,7 +1029,7 @@ void main() {
10321029
group('doc references', () {
10331030
String docsAsHtml;
10341031

1035-
setUp(() {
1032+
setUpAll(() {
10361033
docsAsHtml = doAwesomeStuff.documentationAsHtml;
10371034
});
10381035

@@ -1358,7 +1355,7 @@ void main() {
13581355
overrideByBoth,
13591356
overrideByModifierClass;
13601357

1361-
setUp(() {
1358+
setUpAll(() {
13621359
Iterable<Class> classes = fakeLibrary.publicClasses;
13631360
GenericClass = classes.firstWhere((c) => c.name == 'GenericClass');
13641361
ModifierClass = classes.firstWhere((c) => c.name == 'ModifierClass');
@@ -1487,7 +1484,7 @@ void main() {
14871484
Class Apple, B, Cat, Cool, Dog, F, Dep, SpecialList;
14881485
Class ExtendingClass, CatString;
14891486

1490-
setUp(() {
1487+
setUpAll(() {
14911488
classes = exLibrary.publicClasses.toList();
14921489
Apple = classes.firstWhere((c) => c.name == 'Apple');
14931490
B = classes.firstWhere((c) => c.name == 'B');
@@ -1741,7 +1738,7 @@ void main() {
17411738
group('Enum', () {
17421739
Enum animal;
17431740

1744-
setUp(() {
1741+
setUpAll(() {
17451742
animal = exLibrary.enums.firstWhere((e) => e.name == 'Animal');
17461743

17471744
/// Trigger code reference resolution
@@ -1809,7 +1806,7 @@ void main() {
18091806
ModelFunction topLevelFunction;
18101807
ModelFunction typeParamOfFutureOr;
18111808

1812-
setUp(() {
1809+
setUpAll(() {
18131810
f1 = exLibrary.functions.first;
18141811
genericFunction =
18151812
exLibrary.functions.firstWhere((f) => f.name == 'genericFunction');
@@ -1947,7 +1944,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
19471944
group('Type expansion', () {
19481945
Class TemplatedInterface, ClassWithUnusualProperties;
19491946

1950-
setUp(() {
1947+
setUpAll(() {
19511948
TemplatedInterface =
19521949
exLibrary.classes.singleWhere((c) => c.name == 'TemplatedInterface');
19531950
ClassWithUnusualProperties = fakeLibrary.classes
@@ -2116,7 +2113,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
21162113
Method inheritedClear, testGeneric, testGenericMethod;
21172114
Method getAFunctionReturningVoid, getAFunctionReturningBool;
21182115

2119-
setUp(() {
2116+
setUpAll(() {
21202117
klass = exLibrary.classes.singleWhere((c) => c.name == 'Klass');
21212118
classB = exLibrary.classes.singleWhere((c) => c.name == 'B');
21222119
HasGenerics =
@@ -2282,7 +2279,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
22822279
Class specializedDuration;
22832280
Operator plus, equalsOverride;
22842281

2285-
setUp(() {
2282+
setUpAll(() {
22862283
specializedDuration =
22872284
exLibrary.classes.firstWhere((c) => c.name == 'SpecializedDuration');
22882285
plus = specializedDuration.allOperators
@@ -2342,7 +2339,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
23422339
Field aProperty;
23432340
Field covariantField, covariantSetter;
23442341

2345-
setUp(() {
2342+
setUpAll(() {
23462343
c = exLibrary.classes.firstWhere((c) => c.name == 'Apple');
23472344
f1 = c.staticProperties[0]; // n
23482345
f2 = c.instanceProperties[0];
@@ -2655,7 +2652,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
26552652

26562653
Class classB;
26572654

2658-
setUp(() {
2655+
setUpAll(() {
26592656
TopLevelVariable justGetter =
26602657
fakeLibrary.properties.firstWhere((p) => p.name == 'justGetter');
26612658
onlyGetterGetter = justGetter.getter;
@@ -2704,7 +2701,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
27042701
TopLevelVariable complicatedReturn;
27052702
TopLevelVariable importantComputations;
27062703

2707-
setUp(() {
2704+
setUpAll(() {
27082705
v = exLibrary.properties.firstWhere((p) => p.name == 'number');
27092706
v3 = exLibrary.properties.firstWhere((p) => p.name == 'y');
27102707
importantComputations = fakeLibrary.properties
@@ -2839,7 +2836,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
28392836

28402837
Field aStaticConstField, aName;
28412838

2842-
setUp(() {
2839+
setUpAll(() {
28432840
greenConstant =
28442841
exLibrary.constants.firstWhere((c) => c.name == 'COLOR_GREEN');
28452842
orangeConstant =
@@ -2914,7 +2911,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
29142911
Constructor appleConstructorFromString;
29152912
Constructor constructorTesterDefault, constructorTesterFromSomething;
29162913
Class apple, constCat, constructorTester;
2917-
setUp(() {
2914+
setUpAll(() {
29182915
apple = exLibrary.classes.firstWhere((c) => c.name == 'Apple');
29192916
constCat = exLibrary.classes.firstWhere((c) => c.name == 'ConstantCat');
29202917
constructorTester =
@@ -2973,7 +2970,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
29732970
ModelFunction returningFutureVoid, aVoidParameter;
29742971
Class ExtendsFutureVoid, ImplementsFutureVoid, ATypeTakingClassMixedIn;
29752972

2976-
setUp(() {
2973+
setUpAll(() {
29772974
returningFutureVoid = fakeLibrary.functions
29782975
.firstWhere((f) => f.name == 'returningFutureVoid');
29792976
aVoidParameter =
@@ -3043,7 +3040,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
30433040
group('ModelType', () {
30443041
Field fList;
30453042

3046-
setUp(() {
3043+
setUpAll(() {
30473044
fList = exLibrary.classes
30483045
.firstWhere((c) => c.name == 'B')
30493046
.instanceProperties
@@ -3061,7 +3058,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
30613058
Typedef aComplexTypedef;
30623059
Class TypedefUsingClass;
30633060

3064-
setUp(() {
3061+
setUpAll(() {
30653062
t = exLibrary.typedefs.firstWhere((t) => t.name == 'processMessage');
30663063
generic =
30673064
fakeLibrary.typedefs.firstWhere((t) => t.name == 'NewGenericTypedef');
@@ -3155,7 +3152,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
31553152
applyCovariantParams;
31563153
Parameter intNumber, intCheckOptional;
31573154

3158-
setUp(() {
3155+
setUpAll(() {
31593156
c = exLibrary.classes.firstWhere((c) => c.name == 'Apple');
31603157
CovariantMemberParams = fakeLibrary.classes
31613158
.firstWhere((c) => c.name == 'CovariantMemberParams');
@@ -3248,7 +3245,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
32483245
Class b;
32493246
List<Class> implA, implC;
32503247

3251-
setUp(() {
3248+
setUpAll(() {
32523249
apple = exLibrary.classes.firstWhere((c) => c.name == 'Apple');
32533250
b = exLibrary.classes.firstWhere((c) => c.name == 'B');
32543251
implA = apple.publicImplementors.toList();
@@ -3300,7 +3297,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
33003297
group('Annotations', () {
33013298
Class forAnnotation, dog;
33023299
Method ctr;
3303-
setUp(() {
3300+
setUpAll(() {
33043301
forAnnotation =
33053302
exLibrary.classes.firstWhere((c) => c.name == 'HasAnnotation');
33063303
dog = exLibrary.classes.firstWhere((c) => c.name == 'Dog');

0 commit comments

Comments
 (0)