Skip to content

Commit 09a9a89

Browse files
jensjohaCommit Queue
authored and
Commit Queue
committed
[analyzer] Deduplicate strings in Declaration
This CL removes some duplication of Strings: * Most duplication in `relevanceTagsInFile` (`ElementKind.CLASS`, etc) when loaded from file. * Usage of `()` for `parameters`. * Empty string return type. * Known type return type. On the analyzer instance I've measured on (8 folders with analyzer etc open) this saves: Before CL: 161851 kb 2158725 _OneByteString dart:core 1: 154622 kb 2001695 _OneByteString dart:core // removes most practical duplication from relevanceTagsInFile 2: 153247 kb 1957751 _OneByteString dart:core // Removes () parameters 3: 152970 kb 1939890 _OneByteString dart:core // Removes empty string return type 3: 151287 kb 1886063 _OneByteString dart:core // removes void, String, bool, int, Future<void> return type That's a total of 10,564 kb or 272,662 instances. This is also ~1.7% of total ram usage. Change-Id: Ide2ae55ae052d9f8bc382805a204dce48bf2b263 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/273742 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Jens Johansen <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 66990ed commit 09a9a89

File tree

1 file changed

+106
-33
lines changed

1 file changed

+106
-33
lines changed

pkg/analyzer/lib/src/services/available_declarations.dart

+106-33
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,56 @@ import 'package:convert/convert.dart';
3030
import 'package:meta/meta.dart';
3131
import 'package:yaml/yaml.dart';
3232

33+
const _elementKindClass = "ElementKind.CLASS";
34+
const _elementKindConstructor = "ElementKind.CONSTRUCTOR";
35+
const _elementKindConstructorConst = "ElementKind.CONSTRUCTOR+const";
36+
const _elementKindEnum = "ElementKind.ENUM";
37+
const _elementKindEnumConstant = "ElementKind.ENUM_CONSTANT";
38+
const _elementKindEnumConstantConst = "ElementKind.ENUM_CONSTANT+const";
39+
const _elementKindExtension = "ElementKind.EXTENSION";
40+
const _elementKindField = "ElementKind.FIELD";
41+
const _elementKindFieldConst = "ElementKind.FIELD+const";
42+
const _elementKindFunction = "ElementKind.FUNCTION";
43+
const _elementKindFunctionTypeAlias = "ElementKind.FUNCTION_TYPE_ALIAS";
44+
const _elementKindMethod = "ElementKind.METHOD";
45+
const _elementKindMixin = "ElementKind.MIXIN";
46+
const _elementKindTopLevelVariable = "ElementKind.TOP_LEVEL_VARIABLE";
47+
const _elementKindTopLevelVariableConst =
48+
"ElementKind.TOP_LEVEL_VARIABLE+const";
49+
const _elementKindTypeAlias = "ElementKind.TYPE_ALIAS";
50+
const _hardcodedTags = {
51+
_elementKindClass,
52+
_elementKindConstructor,
53+
_elementKindConstructorConst,
54+
_elementKindEnum,
55+
_elementKindEnumConstant,
56+
_elementKindEnumConstantConst,
57+
_elementKindExtension,
58+
_elementKindField,
59+
_elementKindFieldConst,
60+
_elementKindFunction,
61+
_elementKindFunctionTypeAlias,
62+
_elementKindMethod,
63+
_elementKindMixin,
64+
_elementKindTopLevelVariable,
65+
_elementKindTopLevelVariableConst,
66+
_elementKindTypeAlias,
67+
_tagBool,
68+
_tagDouble,
69+
_tagInt,
70+
_tagString,
71+
_tagList,
72+
_tagMap,
73+
_tagSet
74+
};
75+
const _tagBool = "dart:core::bool";
76+
const _tagDouble = "dart:core::double";
77+
const _tagInt = "dart:core::int";
78+
const _tagList = "dart:core::List";
79+
const _tagMap = "dart:core::Map";
80+
const _tagSet = "dart:core::Set";
81+
const _tagString = "dart:core::String";
82+
3383
/// A top-level public declaration.
3484
class Declaration {
3585
final List<Declaration> children;
@@ -83,15 +133,18 @@ class Declaration {
83133
required this.locationStartColumn,
84134
required this.locationStartLine,
85135
required this.name,
86-
required this.parameters,
136+
required String? parameters,
87137
required this.parameterNames,
88138
required this.parameterTypes,
89139
required this.parent,
90140
required List<String> relevanceTagsInFile,
91141
required this.requiredParameterCount,
92-
required this.returnType,
142+
required String? returnType,
93143
required this.typeParameters,
94-
}) : _relevanceTagsInFile = relevanceTagsInFile;
144+
}) : _relevanceTagsInFile = relevanceTagsInFile,
145+
// De-duplicate the string "()".
146+
parameters = parameters == "()" ? "()" : parameters,
147+
returnType = _dedupReturnType(returnType);
95148

96149
Uri? get locationLibraryUri => _locationLibraryUri;
97150

@@ -104,6 +157,19 @@ class Declaration {
104157
String toString() {
105158
return '($kind, $name)';
106159
}
160+
161+
static String? _dedupReturnType(String? input) {
162+
if (input == null) return input;
163+
const knownReturnTypes = {
164+
"",
165+
"void",
166+
"String",
167+
"bool",
168+
"int",
169+
"Future<void>"
170+
};
171+
return knownReturnTypes.lookup(input) ?? input;
172+
}
107173
}
108174

109175
/// A kind of a top-level declaration.
@@ -930,20 +996,20 @@ class RelevanceTags {
930996

931997
static List<String>? _forExpression(Expression? expression) {
932998
if (expression is BooleanLiteral) {
933-
return const ['dart:core::bool'];
999+
return const [_tagBool];
9341000
} else if (expression is DoubleLiteral) {
935-
return const ['dart:core::double'];
1001+
return const [_tagDouble];
9361002
} else if (expression is IntegerLiteral) {
937-
return const ['dart:core::int'];
1003+
return const [_tagInt];
9381004
} else if (expression is StringLiteral) {
939-
return const ['dart:core::String'];
1005+
return const [_tagString];
9401006
} else if (expression is ListLiteral) {
941-
return const ['dart:core::List'];
1007+
return const [_tagList];
9421008
} else if (expression is SetOrMapLiteral) {
9431009
if (expression.isMap) {
944-
return const ['dart:core::Map'];
1010+
return const [_tagMap];
9451011
} else if (expression.isSet) {
946-
return const ['dart:core::Set'];
1012+
return const [_tagSet];
9471013
}
9481014
}
9491015

@@ -968,6 +1034,13 @@ class _DeclarationStorage {
9681034
var kind = kindFromIdl(d.kind);
9691035

9701036
var relevanceTags = d.relevanceTags.toList();
1037+
for (int i = 0; i < relevanceTags.length; i++) {
1038+
var tag = relevanceTags[i];
1039+
var lookedUp = _hardcodedTags.lookup(tag);
1040+
if (lookedUp != null) {
1041+
relevanceTags[i] = lookedUp;
1042+
}
1043+
}
9711044

9721045
var children = <Declaration>[];
9731046
var declaration = Declaration(
@@ -1489,8 +1562,8 @@ class _File {
14891562
parameterTypes: _getFormalParameterTypes(parameters),
14901563
parent: parent,
14911564
relevanceTags: [
1492-
'ElementKind.CONSTRUCTOR',
1493-
if (isConst) 'ElementKind.CONSTRUCTOR+const'
1565+
_elementKindConstructor,
1566+
if (isConst) _elementKindConstructorConst
14941567
],
14951568
requiredParameterCount:
14961569
_getFormalParameterRequiredCount(parameters),
@@ -1514,8 +1587,8 @@ class _File {
15141587
name: field.name,
15151588
parent: parent,
15161589
relevanceTags: [
1517-
'ElementKind.FIELD',
1518-
if (isConst) 'ElementKind.FIELD+const',
1590+
_elementKindField,
1591+
if (isConst) _elementKindFieldConst,
15191592
...?RelevanceTags._forExpression(field.initializer)
15201593
],
15211594
returnType: _getTypeAnnotationString(classMember.fields.type),
@@ -1531,7 +1604,7 @@ class _File {
15311604
kind: DeclarationKind.GETTER,
15321605
name: classMember.name,
15331606
parent: parent,
1534-
relevanceTags: ['ElementKind.FIELD'],
1607+
relevanceTags: [_elementKindField],
15351608
returnType: _getTypeAnnotationString(classMember.returnType),
15361609
);
15371610
} else if (classMember.isSetter && parameters != null) {
@@ -1544,7 +1617,7 @@ class _File {
15441617
parameterNames: _getFormalParameterNames(parameters),
15451618
parameterTypes: _getFormalParameterTypes(parameters),
15461619
parent: parent,
1547-
relevanceTags: ['ElementKind.FIELD'],
1620+
relevanceTags: [_elementKindField],
15481621
requiredParameterCount:
15491622
_getFormalParameterRequiredCount(parameters),
15501623
);
@@ -1561,7 +1634,7 @@ class _File {
15611634
parameterNames: _getFormalParameterNames(parameters),
15621635
parameterTypes: _getFormalParameterTypes(parameters),
15631636
parent: parent,
1564-
relevanceTags: ['ElementKind.METHOD'],
1637+
relevanceTags: [_elementKindMethod],
15651638
requiredParameterCount:
15661639
_getFormalParameterRequiredCount(parameters),
15671640
returnType: _getTypeAnnotationString(classMember.returnType),
@@ -1578,7 +1651,7 @@ class _File {
15781651
isDeprecated: isDeprecated,
15791652
kind: DeclarationKind.CLASS,
15801653
name: node.name,
1581-
relevanceTags: ['ElementKind.CLASS'],
1654+
relevanceTags: [_elementKindClass],
15821655
);
15831656
if (classDeclaration == null) continue;
15841657

@@ -1610,7 +1683,7 @@ class _File {
16101683
parameterNames: [],
16111684
parameterTypes: [],
16121685
parent: classDeclaration,
1613-
relevanceTagsInFile: ['ElementKind.CONSTRUCTOR'],
1686+
relevanceTagsInFile: [_elementKindConstructor],
16141687
requiredParameterCount: 0,
16151688
returnType: node.name.lexeme,
16161689
typeParameters: null,
@@ -1621,14 +1694,14 @@ class _File {
16211694
isDeprecated: isDeprecated,
16221695
kind: DeclarationKind.CLASS_TYPE_ALIAS,
16231696
name: node.name,
1624-
relevanceTags: ['ElementKind.CLASS'],
1697+
relevanceTags: [_elementKindClass],
16251698
);
16261699
} else if (node is EnumDeclaration) {
16271700
var enumDeclaration = addDeclaration(
16281701
isDeprecated: isDeprecated,
16291702
kind: DeclarationKind.ENUM,
16301703
name: node.name,
1631-
relevanceTags: ['ElementKind.ENUM'],
1704+
relevanceTags: [_elementKindEnum],
16321705
);
16331706
if (enumDeclaration == null) continue;
16341707

@@ -1641,8 +1714,8 @@ class _File {
16411714
name: constant.name,
16421715
parent: enumDeclaration,
16431716
relevanceTags: [
1644-
'ElementKind.ENUM_CONSTANT',
1645-
'ElementKind.ENUM_CONSTANT+const'
1717+
_elementKindEnumConstant,
1718+
_elementKindEnumConstantConst
16461719
],
16471720
);
16481721
}
@@ -1653,7 +1726,7 @@ class _File {
16531726
isDeprecated: isDeprecated,
16541727
kind: DeclarationKind.EXTENSION,
16551728
name: name,
1656-
relevanceTags: ['ElementKind.EXTENSION'],
1729+
relevanceTags: [_elementKindExtension],
16571730
);
16581731
}
16591732
// TODO(brianwilkerson) Should we be creating declarations for the
@@ -1666,7 +1739,7 @@ class _File {
16661739
isDeprecated: isDeprecated,
16671740
kind: DeclarationKind.GETTER,
16681741
name: node.name,
1669-
relevanceTags: ['ElementKind.FUNCTION'],
1742+
relevanceTags: [_elementKindFunction],
16701743
returnType: _getTypeAnnotationString(node.returnType),
16711744
);
16721745
} else if (node.isSetter && parameters != null) {
@@ -1677,7 +1750,7 @@ class _File {
16771750
parameters: parameters.toSource(),
16781751
parameterNames: _getFormalParameterNames(parameters),
16791752
parameterTypes: _getFormalParameterTypes(parameters),
1680-
relevanceTags: ['ElementKind.FUNCTION'],
1753+
relevanceTags: [_elementKindFunction],
16811754
requiredParameterCount:
16821755
_getFormalParameterRequiredCount(parameters),
16831756
);
@@ -1692,7 +1765,7 @@ class _File {
16921765
parameters: parameters.toSource(),
16931766
parameterNames: _getFormalParameterNames(parameters),
16941767
parameterTypes: _getFormalParameterTypes(parameters),
1695-
relevanceTags: ['ElementKind.FUNCTION'],
1768+
relevanceTags: [_elementKindFunction],
16961769
requiredParameterCount:
16971770
_getFormalParameterRequiredCount(parameters),
16981771
returnType: _getTypeAnnotationString(node.returnType),
@@ -1711,7 +1784,7 @@ class _File {
17111784
parameters: parameters.toSource(),
17121785
parameterNames: _getFormalParameterNames(parameters),
17131786
parameterTypes: _getFormalParameterTypes(parameters),
1714-
relevanceTags: ['ElementKind.FUNCTION_TYPE_ALIAS'],
1787+
relevanceTags: [_elementKindFunctionTypeAlias],
17151788
requiredParameterCount:
17161789
_getFormalParameterRequiredCount(parameters),
17171790
returnType: _getTypeAnnotationString(functionType.returnType),
@@ -1722,7 +1795,7 @@ class _File {
17221795
isDeprecated: isDeprecated,
17231796
kind: DeclarationKind.TYPE_ALIAS,
17241797
name: node.name,
1725-
relevanceTags: ['ElementKind.TYPE_ALIAS'],
1798+
relevanceTags: [_elementKindTypeAlias],
17261799
);
17271800
}
17281801
} else if (node is FunctionTypeAlias) {
@@ -1734,7 +1807,7 @@ class _File {
17341807
parameters: parameters.toSource(),
17351808
parameterNames: _getFormalParameterNames(parameters),
17361809
parameterTypes: _getFormalParameterTypes(parameters),
1737-
relevanceTags: ['ElementKind.FUNCTION_TYPE_ALIAS'],
1810+
relevanceTags: [_elementKindFunctionTypeAlias],
17381811
requiredParameterCount: _getFormalParameterRequiredCount(parameters),
17391812
returnType: _getTypeAnnotationString(node.returnType),
17401813
typeParameters: node.typeParameters?.toSource(),
@@ -1744,7 +1817,7 @@ class _File {
17441817
isDeprecated: isDeprecated,
17451818
kind: DeclarationKind.MIXIN,
17461819
name: node.name,
1747-
relevanceTags: ['ElementKind.MIXIN'],
1820+
relevanceTags: [_elementKindMixin],
17481821
);
17491822
if (mixinDeclaration == null) continue;
17501823
addClassMembers(mixinDeclaration, false, node.members);
@@ -1760,8 +1833,8 @@ class _File {
17601833
kind: DeclarationKind.VARIABLE,
17611834
name: variable.name,
17621835
relevanceTags: [
1763-
'ElementKind.TOP_LEVEL_VARIABLE',
1764-
if (isConst) 'ElementKind.TOP_LEVEL_VARIABLE+const',
1836+
_elementKindTopLevelVariable,
1837+
if (isConst) _elementKindTopLevelVariableConst,
17651838
...?RelevanceTags._forExpression(variable.initializer)
17661839
],
17671840
returnType: _getTypeAnnotationString(node.variables.type),

0 commit comments

Comments
 (0)