Skip to content

Commit 394c812

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Optimize addXyz()
Instead of defensive copies, check if the current value is an empty sentinel, and turn it into an empty list. Change-Id: I798bb5bdac2c4f584f3e00eee3b2fdc922796a8e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/442164 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 44a1ebf commit 394c812

File tree

1 file changed

+108
-62
lines changed

1 file changed

+108
-62
lines changed

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 108 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4299,23 +4299,19 @@ abstract class InstanceElementImpl extends ElementImpl
42994299
List<TypeParameterElementImpl> get typeParameters2 => typeParameters;
43004300

43014301
void addField(FieldElementImpl element) {
4302-
// TODO(scheglov): optimize
4303-
_fields = [..._fields, element];
4302+
_fields.add(element);
43044303
}
43054304

43064305
void addGetter(GetterElementImpl element) {
4307-
// TODO(scheglov): optimize
4308-
_getters = [..._getters, element];
4306+
_getters.add(element);
43094307
}
43104308

43114309
void addMethod(MethodElementImpl element) {
4312-
// TODO(scheglov): optimize
4313-
_methods = [..._methods, element];
4310+
_methods.add(element);
43144311
}
43154312

43164313
void addSetter(SetterElementImpl element) {
4317-
// TODO(scheglov): optimize
4318-
_setters = [..._setters, element];
4314+
_setters.add(element);
43194315
}
43204316

43214317
@Deprecated('Use displayString instead')
@@ -4621,10 +4617,10 @@ abstract class InstanceFragmentImpl extends FragmentImpl
46214617
@override
46224618
InstanceFragmentImpl? nextFragment;
46234619

4624-
List<FieldFragmentImpl> _fields = _Sentinel.fieldElement;
4625-
List<GetterFragmentImpl> _getters = _Sentinel.getterElement;
4626-
List<SetterFragmentImpl> _setters = _Sentinel.setterElement;
4627-
List<MethodFragmentImpl> _methods = _Sentinel.methodElement;
4620+
List<FieldFragmentImpl> _fields = _Sentinel.fieldFragment;
4621+
List<GetterFragmentImpl> _getters = _Sentinel.getterFragment;
4622+
List<SetterFragmentImpl> _setters = _Sentinel.setterFragment;
4623+
List<MethodFragmentImpl> _methods = _Sentinel.methodFragment;
46284624

46294625
InstanceFragmentImpl({required this.name, required super.firstTokenOffset});
46304626

@@ -4641,7 +4637,7 @@ abstract class InstanceFragmentImpl extends FragmentImpl
46414637

46424638
@override
46434639
List<FieldFragmentImpl> get fields {
4644-
if (!identical(_fields, _Sentinel.fieldElement)) {
4640+
if (!identical(_fields, _Sentinel.fieldFragment)) {
46454641
return _fields;
46464642
}
46474643

@@ -4663,7 +4659,7 @@ abstract class InstanceFragmentImpl extends FragmentImpl
46634659

46644660
@override
46654661
List<GetterFragmentImpl> get getters {
4666-
if (!identical(_getters, _Sentinel.getterElement)) {
4662+
if (!identical(_getters, _Sentinel.getterFragment)) {
46674663
return _getters;
46684664
}
46694665

@@ -4687,7 +4683,7 @@ abstract class InstanceFragmentImpl extends FragmentImpl
46874683

46884684
@override
46894685
List<MethodFragmentImpl> get methods {
4690-
if (!identical(_methods, _Sentinel.methodElement)) {
4686+
if (!identical(_methods, _Sentinel.methodFragment)) {
46914687
return _methods;
46924688
}
46934689

@@ -4712,7 +4708,7 @@ abstract class InstanceFragmentImpl extends FragmentImpl
47124708

47134709
@override
47144710
List<SetterFragmentImpl> get setters {
4715-
if (!identical(_setters, _Sentinel.setterElement)) {
4711+
if (!identical(_setters, _Sentinel.setterFragment)) {
47164712
return _setters;
47174713
}
47184714

@@ -4729,26 +4725,34 @@ abstract class InstanceFragmentImpl extends FragmentImpl
47294725
}
47304726

47314727
void addField(FieldFragmentImpl fragment) {
4732-
// TODO(scheglov): optimize
4733-
_fields = [..._fields, fragment];
4728+
if (identical(_fields, _Sentinel.fieldFragment)) {
4729+
_fields = [];
4730+
}
4731+
_fields.add(fragment);
47344732
fragment.enclosingFragment = this;
47354733
}
47364734

47374735
void addGetter(GetterFragmentImpl fragment) {
4738-
// TODO(scheglov): optimize
4739-
_getters = [..._getters, fragment];
4736+
if (identical(_getters, _Sentinel.getterFragment)) {
4737+
_getters = [];
4738+
}
4739+
_getters.add(fragment);
47404740
fragment.enclosingFragment = this;
47414741
}
47424742

47434743
void addMethod(MethodFragmentImpl fragment) {
4744-
// TODO(scheglov): optimize
4745-
_methods = [..._methods, fragment];
4744+
if (identical(_methods, _Sentinel.methodFragment)) {
4745+
_methods = [];
4746+
}
4747+
_methods.add(fragment);
47464748
fragment.enclosingFragment = this;
47474749
}
47484750

47494751
void addSetter(SetterFragmentImpl fragment) {
4750-
// TODO(scheglov): optimize
4751-
_setters = [..._setters, fragment];
4752+
if (identical(_setters, _Sentinel.setterFragment)) {
4753+
_setters = [];
4754+
}
4755+
_setters.add(fragment);
47524756
fragment.enclosingFragment = this;
47534757
}
47544758
}
@@ -4904,8 +4908,10 @@ abstract class InterfaceElementImpl extends InstanceElementImpl
49044908
}
49054909

49064910
void addConstructor(ConstructorElementImpl element) {
4907-
// TODO(scheglov): optimize
4908-
_constructors = [..._constructors, element];
4911+
if (identical(_constructors, _Sentinel.constructorElement)) {
4912+
_constructors = [];
4913+
}
4914+
_constructors.add(element);
49094915
}
49104916

49114917
@override
@@ -5251,8 +5257,10 @@ abstract class InterfaceFragmentImpl extends InstanceFragmentImpl
52515257
}
52525258

52535259
void addConstructor(ConstructorFragmentImpl fragment) {
5254-
// TODO(scheglov): optimize
5255-
_constructors = [..._constructors, fragment];
5260+
if (identical(_constructors, _Sentinel.constructorFragment)) {
5261+
_constructors = [];
5262+
}
5263+
_constructors.add(fragment);
52565264
fragment.enclosingFragment = this;
52575265
}
52585266
}
@@ -6163,34 +6171,37 @@ class LibraryFragmentImpl extends FragmentImpl
61636171
List<PartIncludeImpl> _parts = const <PartIncludeImpl>[];
61646172

61656173
/// All top-level getters in this compilation unit.
6166-
List<GetterFragmentImpl> _getters = _Sentinel.getterElement;
6174+
List<GetterFragmentImpl> _getters = _Sentinel.getterFragment;
61676175

61686176
/// All top-level setters in this compilation unit.
6169-
List<SetterFragmentImpl> _setters = _Sentinel.setterElement;
6177+
List<SetterFragmentImpl> _setters = _Sentinel.setterFragment;
61706178

6171-
List<ClassFragmentImpl> _classes = const [];
6179+
List<ClassFragmentImpl> _classes = _Sentinel.classFragment;
61726180

61736181
/// A list containing all of the enums contained in this compilation unit.
6174-
List<EnumFragmentImpl> _enums = const [];
6182+
List<EnumFragmentImpl> _enums = _Sentinel.enumFragment;
61756183

61766184
/// A list containing all of the extensions contained in this compilation
61776185
/// unit.
6178-
List<ExtensionFragmentImpl> _extensions = const [];
6186+
List<ExtensionFragmentImpl> _extensions = _Sentinel.extensionFragment;
61796187

6180-
List<ExtensionTypeFragmentImpl> _extensionTypes = const [];
6188+
List<ExtensionTypeFragmentImpl> _extensionTypes =
6189+
_Sentinel.extensionTypeFragment;
61816190

61826191
/// A list containing all of the top-level functions contained in this
61836192
/// compilation unit.
6184-
List<TopLevelFunctionFragmentImpl> _functions = const [];
6193+
List<TopLevelFunctionFragmentImpl> _functions =
6194+
_Sentinel.topLevelFunctionFragment;
61856195

6186-
List<MixinFragmentImpl> _mixins = const [];
6196+
List<MixinFragmentImpl> _mixins = _Sentinel.mixinFragment;
61876197

61886198
/// A list containing all of the type aliases contained in this compilation
61896199
/// unit.
6190-
List<TypeAliasFragmentImpl> _typeAliases = const [];
6200+
List<TypeAliasFragmentImpl> _typeAliases = _Sentinel.typeAliasFragment;
61916201

61926202
/// A list containing all of the variables contained in this compilation unit.
6193-
List<TopLevelVariableFragmentImpl> _variables = const [];
6203+
List<TopLevelVariableFragmentImpl> _variables =
6204+
_Sentinel.topLevelVariableFragment;
61946205

61956206
/// The scope of this fragment, `null` if it has not been created yet.
61966207
LibraryFragmentScope? _scope;
@@ -6521,62 +6532,82 @@ class LibraryFragmentImpl extends FragmentImpl
65216532
List<TypeAliasFragment> get typeAliases2 => typeAliases;
65226533

65236534
void addClass(ClassFragmentImpl fragment) {
6524-
// TODO(scheglov): optimize
6525-
_classes = [..._classes, fragment];
6535+
if (identical(_classes, _Sentinel.classFragment)) {
6536+
_classes = [];
6537+
}
6538+
_classes.add(fragment);
65266539
fragment.enclosingFragment = this;
65276540
}
65286541

65296542
void addEnum(EnumFragmentImpl fragment) {
6530-
// TODO(scheglov): optimize
6531-
_enums = [..._enums, fragment];
6543+
if (identical(_enums, _Sentinel.enumFragment)) {
6544+
_enums = [];
6545+
}
6546+
_enums.add(fragment);
65326547
fragment.enclosingFragment = this;
65336548
}
65346549

65356550
void addExtension(ExtensionFragmentImpl fragment) {
6536-
// TODO(scheglov): optimize
6537-
_extensions = [..._extensions, fragment];
6551+
if (identical(_extensions, _Sentinel.extensionFragment)) {
6552+
_extensions = [];
6553+
}
6554+
_extensions.add(fragment);
65386555
fragment.enclosingFragment = this;
65396556
}
65406557

65416558
void addExtensionType(ExtensionTypeFragmentImpl fragment) {
6542-
// TODO(scheglov): optimize
6543-
_extensionTypes = [..._extensionTypes, fragment];
6559+
if (identical(_extensionTypes, _Sentinel.extensionTypeFragment)) {
6560+
_extensionTypes = [];
6561+
}
6562+
_extensionTypes.add(fragment);
65446563
fragment.enclosingFragment = this;
65456564
}
65466565

65476566
void addFunction(TopLevelFunctionFragmentImpl fragment) {
6548-
// TODO(scheglov): optimize
6549-
_functions = [..._functions, fragment];
6567+
if (identical(_functions, _Sentinel.topLevelFunctionFragment)) {
6568+
_functions = [];
6569+
}
6570+
_functions.add(fragment);
65506571
fragment.enclosingFragment = this;
65516572
}
65526573

65536574
void addGetter(GetterFragmentImpl fragment) {
6554-
// TODO(scheglov): optimize
6555-
_getters = [..._getters, fragment];
6575+
if (identical(_getters, _Sentinel.getterFragment)) {
6576+
_getters = [];
6577+
}
6578+
_getters.add(fragment);
65566579
fragment.enclosingFragment = this;
65576580
}
65586581

65596582
void addMixin(MixinFragmentImpl fragment) {
6560-
// TODO(scheglov): optimize
6561-
_mixins = [..._mixins, fragment];
6583+
if (identical(_mixins, _Sentinel.mixinFragment)) {
6584+
_mixins = [];
6585+
}
6586+
_mixins.add(fragment);
65626587
fragment.enclosingFragment = this;
65636588
}
65646589

65656590
void addSetter(SetterFragmentImpl fragment) {
6566-
// TODO(scheglov): optimize
6567-
_setters = [..._setters, fragment];
6591+
if (identical(_setters, _Sentinel.setterFragment)) {
6592+
_setters = [];
6593+
}
6594+
_setters.add(fragment);
65686595
fragment.enclosingFragment = this;
65696596
}
65706597

65716598
void addTopLevelVariable(TopLevelVariableFragmentImpl fragment) {
6572-
// TODO(scheglov): optimize
6573-
_variables = [..._variables, fragment];
6599+
if (identical(_variables, _Sentinel.topLevelVariableFragment)) {
6600+
_variables = [];
6601+
}
6602+
_variables.add(fragment);
65746603
fragment.enclosingFragment = this;
65756604
}
65766605

65776606
void addTypeAlias(TypeAliasFragmentImpl fragment) {
6578-
// TODO(scheglov): optimize
6579-
_typeAliases = [..._typeAliases, fragment];
6607+
if (identical(_typeAliases, _Sentinel.typeAliasFragment)) {
6608+
_typeAliases = [];
6609+
}
6610+
_typeAliases.add(fragment);
65806611
fragment.enclosingFragment = this;
65816612
}
65826613

@@ -10363,16 +10394,31 @@ abstract class VariableFragmentImpl extends FragmentImpl
1036310394
/// Instances of [List]s that are used as "not yet computed" values, they
1036410395
/// must be not `null`, and not identical to `const <T>[]`.
1036510396
class _Sentinel {
10397+
static final List<ClassFragmentImpl> classFragment = List.unmodifiable([]);
1036610398
static final List<ConstructorFragmentImpl> constructorFragment =
1036710399
List.unmodifiable([]);
10400+
static final List<EnumFragmentImpl> enumFragment = List.unmodifiable([]);
10401+
static final List<ExtensionFragmentImpl> extensionFragment =
10402+
List.unmodifiable([]);
10403+
static final List<ExtensionTypeFragmentImpl> extensionTypeFragment =
10404+
List.unmodifiable([]);
10405+
static final List<FieldFragmentImpl> fieldFragment = List.unmodifiable([]);
10406+
static final List<GetterFragmentImpl> getterFragment = List.unmodifiable([]);
10407+
static final List<MethodFragmentImpl> methodFragment = List.unmodifiable([]);
10408+
static final List<MixinFragmentImpl> mixinFragment = List.unmodifiable([]);
10409+
static final List<SetterFragmentImpl> setterFragment = List.unmodifiable([]);
10410+
static final List<TypeAliasFragmentImpl> typeAliasFragment =
10411+
List.unmodifiable([]);
10412+
static final List<TopLevelFunctionFragmentImpl> topLevelFunctionFragment =
10413+
List.unmodifiable([]);
10414+
static final List<TopLevelVariableFragmentImpl> topLevelVariableFragment =
10415+
List.unmodifiable([]);
10416+
1036810417
static final List<ConstructorElementImpl> constructorElement =
1036910418
List.unmodifiable([]);
10370-
static final List<FieldFragmentImpl> fieldElement = List.unmodifiable([]);
10371-
static final List<GetterFragmentImpl> getterElement = List.unmodifiable([]);
10419+
1037210420
static final List<LibraryExportImpl> libraryExport = List.unmodifiable([]);
1037310421
static final List<LibraryImportImpl> libraryImport = List.unmodifiable([]);
10374-
static final List<MethodFragmentImpl> methodElement = List.unmodifiable([]);
10375-
static final List<SetterFragmentImpl> setterElement = List.unmodifiable([]);
1037610422
}
1037710423

1037810424
extension on Fragment {

0 commit comments

Comments
 (0)