Skip to content

Commit 6c95c02

Browse files
authored
Eliminate dartdoc-global cache of ModelElements/Analyzer bits (#1800)
* Move global markdown object cache into PackageGraph * dartfmt
1 parent 2b8e295 commit 6c95c02

File tree

5 files changed

+32
-35
lines changed

5 files changed

+32
-35
lines changed

lib/src/markdown_processor.dart

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import 'package:analyzer/dart/ast/ast.dart' hide TypeParameter;
1212
import 'package:analyzer/dart/element/element.dart';
1313
import 'package:dartdoc/src/element_type.dart';
1414
import 'package:dartdoc/src/model.dart';
15-
import 'package:dartdoc/src/model_utils.dart';
1615
import 'package:dartdoc/src/tuple.dart';
1716
import 'package:dartdoc/src/warnings.dart';
1817
import 'package:html/parser.dart' show parse;
@@ -311,9 +310,6 @@ bool _ConsiderIfConstructor(String codeRef, ModelElement modelElement) {
311310

312311
// Basic map of reference to ModelElement, for cases where we're searching
313312
// outside of scope.
314-
// TODO(jcollins-g): function caches with maps are very common in dartdoc.
315-
// Extract into library.
316-
Map<String, Set<ModelElement>> _findRefElementCache;
317313
// TODO(jcollins-g): Rewrite this to handle constructors in a less hacky way
318314
// TODO(jcollins-g): This function breaks down naturally into many helpers, extract them
319315
// TODO(jcollins-g): Subcomponents of this function shouldn't be adding nulls to results, strip the
@@ -410,32 +406,13 @@ ModelElement _findRefElementInLibrary(String codeRef, Warnable element,
410406
results.remove(null);
411407

412408
// We now need the ref element cache to keep from repeatedly searching [Package.allModelElements].
413-
// TODO(jcollins-g): Find somewhere to cache elements outside package.libraries
414-
// so we can give the right warning (no canonical found)
415-
// when referring to objects in libraries outside the
416-
// documented set.
417-
if (results.isEmpty && _findRefElementCache == null) {
418-
assert(packageGraph.allLibrariesAdded);
419-
_findRefElementCache = new Map();
420-
for (final modelElement
421-
in filterNonDocumented(packageGraph.allLocalModelElements)) {
422-
_findRefElementCache.putIfAbsent(
423-
modelElement.fullyQualifiedNameWithoutLibrary, () => new Set());
424-
_findRefElementCache.putIfAbsent(
425-
modelElement.fullyQualifiedName, () => new Set());
426-
_findRefElementCache[modelElement.fullyQualifiedName].add(modelElement);
427-
_findRefElementCache[modelElement.fullyQualifiedNameWithoutLibrary]
428-
.add(modelElement);
429-
}
430-
}
431-
432409
// But if not, look for a fully qualified match. (That only makes sense
433410
// if the codeRef might be qualified, and contains periods.)
434411
if (results.isEmpty &&
435412
codeRefChomped.contains('.') &&
436-
_findRefElementCache.containsKey(codeRefChomped)) {
413+
packageGraph.findRefElementCache.containsKey(codeRefChomped)) {
437414
for (final ModelElement modelElement
438-
in _findRefElementCache[codeRefChomped]) {
415+
in packageGraph.findRefElementCache[codeRefChomped]) {
439416
if (!_ConsiderIfConstructor(codeRef, modelElement)) continue;
440417
// For fully qualified matches, the original preferredClass passed
441418
// might make no sense. Instead, use the enclosing class from the
@@ -464,8 +441,10 @@ ModelElement _findRefElementInLibrary(String codeRef, Warnable element,
464441
results.remove(null);
465442

466443
// And if we still haven't found anything, just search the whole ball-of-wax.
467-
if (results.isEmpty && _findRefElementCache.containsKey(codeRefChomped)) {
468-
for (final modelElement in _findRefElementCache[codeRefChomped]) {
444+
if (results.isEmpty &&
445+
packageGraph.findRefElementCache.containsKey(codeRefChomped)) {
446+
for (final modelElement
447+
in packageGraph.findRefElementCache[codeRefChomped]) {
469448
if (codeRefChomped == modelElement.fullyQualifiedNameWithoutLibrary ||
470449
(modelElement is Library &&
471450
codeRefChomped == modelElement.fullyQualifiedName)) {
@@ -486,8 +465,9 @@ ModelElement _findRefElementInLibrary(String codeRef, Warnable element,
486465
.sublist(0, codeRefChompedParts.length - 1)
487466
.join('.');
488467
String maybeEnumMember = codeRefChompedParts.last;
489-
if (_findRefElementCache.containsKey(maybeEnumName)) {
490-
for (final modelElement in _findRefElementCache[maybeEnumName]) {
468+
if (packageGraph.findRefElementCache.containsKey(maybeEnumName)) {
469+
for (final modelElement
470+
in packageGraph.findRefElementCache[maybeEnumName]) {
491471
if (modelElement is Enum) {
492472
if (modelElement.constants.any((e) => e.name == maybeEnumMember)) {
493473
results.add(modelElement);

lib/src/model.dart

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,8 +1377,7 @@ abstract class Categorization implements ModelElement {
13771377
}
13781378

13791379
/// Classes extending this class have canonicalization support in Dartdoc.
1380-
abstract class Canonicalization
1381-
implements Locatable, Documentable {
1380+
abstract class Canonicalization implements Locatable, Documentable {
13821381
bool get isCanonical;
13831382
Library get canonicalLibrary;
13841383

@@ -4541,6 +4540,25 @@ class PackageGraph {
45414540
return _implementors;
45424541
}
45434542

4543+
Map<String, Set<ModelElement>> _findRefElementCache;
4544+
Map<String, Set<ModelElement>> get findRefElementCache {
4545+
if (_findRefElementCache == null) {
4546+
assert(packageGraph.allLibrariesAdded);
4547+
_findRefElementCache = new Map();
4548+
for (final modelElement
4549+
in filterNonDocumented(packageGraph.allLocalModelElements)) {
4550+
_findRefElementCache.putIfAbsent(
4551+
modelElement.fullyQualifiedNameWithoutLibrary, () => new Set());
4552+
_findRefElementCache.putIfAbsent(
4553+
modelElement.fullyQualifiedName, () => new Set());
4554+
_findRefElementCache[modelElement.fullyQualifiedName].add(modelElement);
4555+
_findRefElementCache[modelElement.fullyQualifiedNameWithoutLibrary]
4556+
.add(modelElement);
4557+
}
4558+
}
4559+
return _findRefElementCache;
4560+
}
4561+
45444562
// All library objects related to this package; a superset of _libraries.
45454563
final Map<LibraryElement, Library> allLibraries = new Map();
45464564

test/model_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import 'package:test/test.dart';
1616
import 'src/utils.dart' as utils;
1717

1818
/// For testing sort behavior.
19-
class TestLibraryContainer extends LibraryContainer with Nameable{
19+
class TestLibraryContainer extends LibraryContainer with Nameable {
2020
@override
2121
final List<String> containerOrder;
2222
@override

test/tool_runner_test.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ void main() {
7474
'TEST INPUT',
7575
);
7676
expect(errors, isNotEmpty);
77-
expect(
78-
errors[0], contains('Tool "drill" returned non-zero exit code'));
77+
expect(errors[0], contains('Tool "drill" returned non-zero exit code'));
7978
expect(result, isEmpty);
8079
});
8180
test("fails if tool in tool map doesn't exist", () {

testing/test_package/lib/example.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,4 +605,4 @@ abstract class HtmlInjection {
605605
/// <div style="opacity: 0.5;">[HtmlInjection]</div>
606606
/// {@end-inject-html}
607607
void injectSimpleHtml();
608-
}
608+
}

0 commit comments

Comments
 (0)