From 50122c0f3a28d619f12a7bbc20f268fc45363404 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Fri, 8 Mar 2024 14:12:35 -0800 Subject: [PATCH] Convert many fields on Container and TopLevelContainer to getters These were late, final, and public, my least favorite thing. The idea, historically, behind late final fields in dartdoc is caching. If a value is expensive to calculate, like a big list of things, and we have to access that value a few times, best to calculate it once. But some late final fields are only accessed while rendering HTML, accessed from the rendered templates. And typically only accessed once. The '*Sorted' fields definitely fall into this bucket. So I made them all getters, and then benchmarked with the googleapis package, and found no significant change in time-to-document, or max RSS. --- .../templates.runtime_renderers.dart | 2 +- lib/src/model/container.dart | 14 ++++++------- lib/src/model/top_level_container.dart | 20 +++++++++---------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/src/generator/templates.runtime_renderers.dart b/lib/src/generator/templates.runtime_renderers.dart index f2762f0eb2..2adcdfd6d1 100644 --- a/lib/src/generator/templates.runtime_renderers.dart +++ b/lib/src/generator/templates.runtime_renderers.dart @@ -14390,7 +14390,7 @@ class _Renderer_TopLevelContainer extends RendererBase { renderVariable: (CT_ c, Property self, List remainingNames) => self.renderSimpleVariable( - c, remainingNames, 'Iterable'), + c, remainingNames, 'List'), renderIterable: (CT_ c, RendererBase r, List ast, StringSink sink) { return c.publicConstantsSorted.map((e) => diff --git a/lib/src/model/container.dart b/lib/src/model/container.dart index 448cf4a734..f6bea689b1 100644 --- a/lib/src/model/container.dart +++ b/lib/src/model/container.dart @@ -97,7 +97,7 @@ abstract class Container extends ModelElement @nonVirtual bool get hasPublicInstanceMethods => instanceMethods.any((e) => e.isPublic); - late final List publicInstanceMethodsSorted = + List get publicInstanceMethodsSorted => instanceMethods.wherePublic.toList(growable: false)..sort(); @nonVirtual @@ -113,7 +113,7 @@ abstract class Container extends ModelElement bool get hasPublicInstanceOperators => instanceOperators.any((e) => e.isPublic); - late final List publicInstanceOperatorsSorted = + List get publicInstanceOperatorsSorted => instanceOperators.wherePublic.toList(growable: false)..sort(); /// Fields fully declared in this [Container]. @@ -128,14 +128,14 @@ abstract class Container extends ModelElement @nonVirtual bool get hasPublicInstanceFields => instanceFields.any((e) => e.isPublic); - late final List publicInstanceFieldsSorted = + List get publicInstanceFieldsSorted => instanceFields.wherePublic.toList(growable: false)..sort(byName); Iterable get constantFields => declaredFields.where((f) => f.isConst); bool get hasPublicConstantFields => constantFields.any((e) => e.isPublic); - late final List publicConstantFieldsSorted = + List get publicConstantFieldsSorted => constantFields.wherePublic.toList(growable: false)..sort(byName); /// The total list of public enum values. @@ -194,7 +194,7 @@ abstract class Container extends ModelElement bool get hasPublicStaticFields => staticFields.any((e) => e.isPublic); - late final List publicStaticFieldsSorted = + List get publicStaticFieldsSorted => staticFields.wherePublic.toList(growable: false)..sort(); Iterable get staticFields => declaredFields.where((f) => f.isStatic); @@ -205,7 +205,7 @@ abstract class Container extends ModelElement bool get hasPublicVariableStaticFields => variableStaticFields.any((e) => e.isPublic); - late final List publicVariableStaticFieldsSorted = + List get publicVariableStaticFieldsSorted => variableStaticFields.wherePublic.toList(growable: false)..sort(); Iterable get staticMethods => @@ -213,7 +213,7 @@ abstract class Container extends ModelElement bool get hasPublicStaticMethods => staticMethods.any((e) => e.isPublic); - late final List publicStaticMethodsSorted = + List get publicStaticMethodsSorted => staticMethods.wherePublic.toList(growable: false)..sort(); /// For subclasses to add items after the main pass but before the diff --git a/lib/src/model/top_level_container.dart b/lib/src/model/top_level_container.dart index c8f817d377..7eff3ad86a 100644 --- a/lib/src/model/top_level_container.dart +++ b/lib/src/model/top_level_container.dart @@ -55,33 +55,33 @@ mixin TopLevelContainer implements Nameable { // TODO(jcollins-g): Setting this type parameter to `Container` magically // fixes a number of type problems in the AOT compiler, but I am mystified as // to why that should be the case. - late final List publicClassesSorted = + List get publicClassesSorted => classes.wherePublic.toList(growable: false)..sort(); - late final List publicExtensionsSorted = + List get publicExtensionsSorted => extensions.wherePublic.toList(growable: false)..sort(); - late final List publicExtensionTypesSorted = + List get publicExtensionTypesSorted => extensionTypes.wherePublic.toList(growable: false)..sort(); - Iterable get publicConstantsSorted => + List get publicConstantsSorted => constants.wherePublic.toList(growable: false)..sort(); - late final List publicEnumsSorted = + List get publicEnumsSorted => enums.wherePublic.toList(growable: false)..sort(); - late final List publicExceptionsSorted = + List get publicExceptionsSorted => exceptions.wherePublic.toList(growable: false)..sort(); - late final List publicFunctionsSorted = + List get publicFunctionsSorted => functions.wherePublic.toList(growable: false)..sort(); - late final List publicMixinsSorted = + List get publicMixinsSorted => mixins.wherePublic.toList(growable: false)..sort(); - late final List publicPropertiesSorted = + List get publicPropertiesSorted => properties.wherePublic.toList(growable: false)..sort(); - late final List publicTypedefsSorted = + List get publicTypedefsSorted => typedefs.wherePublic.toList(growable: false)..sort(); }