Skip to content

Commit f94ffbf

Browse files
jensjohacommit-bot@chromium.org
authored andcommitted
[kernel] Move procedure lazy loading to function node body
Previously lazy loading was done in Procedure by lazy loading functionNode, now lazy loading is done (only through Procedure) in the functionNode by lazy loading the body. Bug: Change-Id: I25cc86d038123ed4992162b65aa95781ea2c56e8 Reviewed-on: https://dart-review.googlesource.com/15560 Commit-Queue: Jens Johansen <[email protected]> Reviewed-by: Peter von der Ahé <[email protected]>
1 parent d2d8d35 commit f94ffbf

File tree

3 files changed

+79
-75
lines changed

3 files changed

+79
-75
lines changed

pkg/front_end/lib/src/base/processed_options.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ class ProcessedOptions {
295295
Program program = new Program(nameRoot: nameRoot);
296296
// TODO(ahe): Pass file name to BinaryBuilder.
297297
// TODO(ahe): Control lazy loading via an option.
298-
new BinaryBuilder(bytes, null, true).readProgram(program);
298+
new BinaryBuilder(bytes, null, false).readProgram(program);
299299
return program;
300300
}
301301

pkg/kernel/lib/ast.dart

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,42 +1282,12 @@ class Procedure extends Member {
12821282
int flags = 0;
12831283
// function is null if and only if abstract, external,
12841284
// or builder (below) is set.
1285-
FunctionNode _function;
1286-
1287-
void Function() lazyBuilder;
1288-
1289-
void _buildLazy() {
1290-
if (lazyBuilder != null) {
1291-
var lazyBuilderLocal = lazyBuilder;
1292-
lazyBuilder = null;
1293-
lazyBuilderLocal();
1294-
}
1295-
}
1296-
1297-
void set transformerFlags(int flags) {
1298-
_buildLazy();
1299-
super.transformerFlags = flags;
1300-
}
1301-
1302-
int get transformerFlags {
1303-
_buildLazy();
1304-
return super.transformerFlags;
1305-
}
1306-
1307-
void set function(FunctionNode function) {
1308-
_buildLazy();
1309-
_function = function;
1310-
}
1311-
1312-
FunctionNode get function {
1313-
_buildLazy();
1314-
return _function;
1315-
}
1285+
FunctionNode function;
13161286

13171287
/// The uri of the source file this procedure was loaded from.
13181288
String fileUri;
13191289

1320-
Procedure(Name name, this.kind, this._function,
1290+
Procedure(Name name, this.kind, this.function,
13211291
{bool isAbstract: false,
13221292
bool isStatic: false,
13231293
bool isExternal: false,
@@ -1647,9 +1617,29 @@ class FunctionNode extends TreeNode {
16471617
@nocoq
16481618
List<VariableDeclaration> namedParameters;
16491619
DartType returnType; // Not null.
1650-
Statement body;
1620+
Statement _body;
1621+
1622+
void Function() lazyBuilder;
1623+
1624+
void _buildLazy() {
1625+
if (lazyBuilder != null) {
1626+
var lazyBuilderLocal = lazyBuilder;
1627+
lazyBuilder = null;
1628+
lazyBuilderLocal();
1629+
}
1630+
}
1631+
1632+
Statement get body {
1633+
_buildLazy();
1634+
return _body;
1635+
}
16511636

1652-
FunctionNode(this.body,
1637+
void set body(Statement body) {
1638+
_buildLazy();
1639+
_body = body;
1640+
}
1641+
1642+
FunctionNode(this._body,
16531643
{List<TypeParameter> typeParameters,
16541644
List<VariableDeclaration> positionalParameters,
16551645
List<VariableDeclaration> namedParameters,
@@ -1667,7 +1657,7 @@ class FunctionNode extends TreeNode {
16671657
setParents(this.typeParameters, this);
16681658
setParents(this.positionalParameters, this);
16691659
setParents(this.namedParameters, this);
1670-
body?.parent = this;
1660+
_body?.parent = this;
16711661
dartAsyncMarker ??= asyncMarker;
16721662
}
16731663

pkg/kernel/lib/binary/ast_from_binary.dart

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ class BinaryBuilder {
782782
var name = readName();
783783
var annotations = readAnnotationList(node);
784784
debugPath.add(node.name?.name ?? 'constructor');
785-
var function = readFunctionNode();
785+
var function = readFunctionNode(false);
786786
pushVariableDeclarations(function.positionalParameters);
787787
pushVariableDeclarations(function.namedParameters);
788788
if (shouldWriteData) {
@@ -829,12 +829,8 @@ class BinaryBuilder {
829829
bool readFunctionNodeNow =
830830
(kind == ProcedureKind.Factory && functionNodeSize <= 50) ||
831831
_disableLazyReading;
832-
var function;
833-
var transformerFlags;
834-
if (readFunctionNodeNow) {
835-
function = readFunctionNodeOption();
836-
transformerFlags = getAndResetTransformerFlags();
837-
}
832+
var function = readFunctionNodeOption(!readFunctionNodeNow);
833+
var transformerFlags = getAndResetTransformerFlags();
838834
debugPath.removeLast();
839835
if (shouldWriteData) {
840836
node.fileOffset = fileOffset;
@@ -844,25 +840,9 @@ class BinaryBuilder {
844840
node.name = name;
845841
node.fileUri = fileUri;
846842
node.annotations = annotations;
847-
if (readFunctionNodeNow) {
848-
node.function = function;
849-
function?.parent = node;
850-
node.transformerFlags = transformerFlags;
851-
} else {
852-
int offset = _byteOffset;
853-
int programStartOffset = _programStartOffset;
854-
List<TypeParameter> typeParameters = typeParameterStack.toList();
855-
node.lazyBuilder = () {
856-
_byteOffset = offset;
857-
typeParameterStack.clear();
858-
typeParameterStack.addAll(typeParameters);
859-
_programStartOffset = programStartOffset;
860-
FunctionNode functionNode = readFunctionNodeOption();
861-
node.function = functionNode;
862-
functionNode?.parent = node;
863-
node.transformerFlags = getAndResetTransformerFlags();
864-
};
865-
}
843+
node.function = function;
844+
function?.parent = node;
845+
node.transformerFlags = transformerFlags;
866846
}
867847
_byteOffset = endOffset;
868848
return node;
@@ -894,11 +874,11 @@ class BinaryBuilder {
894874
}
895875
}
896876

897-
FunctionNode readFunctionNodeOption() {
898-
return readAndCheckOptionTag() ? readFunctionNode() : null;
877+
FunctionNode readFunctionNodeOption(bool lazyLoadBody) {
878+
return readAndCheckOptionTag() ? readFunctionNode(lazyLoadBody) : null;
899879
}
900880

901-
FunctionNode readFunctionNode() {
881+
FunctionNode readFunctionNode(bool lazyLoadBody) {
902882
int tag = readByte();
903883
assert(tag == Tag.FunctionNode);
904884
int offset = readOffset();
@@ -914,12 +894,14 @@ class BinaryBuilder {
914894
var named = readAndPushVariableDeclarationList();
915895
var returnType = readDartType();
916896
int oldLabelStackBase = labelStackBase;
917-
labelStackBase = labelStack.length;
918-
var body = readStatementOption();
919-
labelStackBase = oldLabelStackBase;
920-
variableStack.length = variableStackHeight;
921-
typeParameterStack.length = typeParameterStackHeight;
922-
return new FunctionNode(body,
897+
898+
var body;
899+
if (!lazyLoadBody) {
900+
labelStackBase = labelStack.length;
901+
body = readStatementOption();
902+
}
903+
904+
FunctionNode result = new FunctionNode(body,
923905
typeParameters: typeParameters,
924906
requiredParameterCount: requiredParameterCount,
925907
positionalParameters: positional,
@@ -929,6 +911,37 @@ class BinaryBuilder {
929911
dartAsyncMarker: dartAsyncMarker)
930912
..fileOffset = offset
931913
..fileEndOffset = endOffset;
914+
915+
if (lazyLoadBody) {
916+
final int savedByteOffset = _byteOffset;
917+
final int programStartOffset = _programStartOffset;
918+
final List<TypeParameter> typeParameters = typeParameterStack.toList();
919+
final List<VariableDeclaration> variables = variableStack.toList();
920+
result.lazyBuilder = () {
921+
_byteOffset = savedByteOffset;
922+
typeParameterStack.clear();
923+
typeParameterStack.addAll(typeParameters);
924+
variableStack.clear();
925+
variableStack.addAll(variables);
926+
_programStartOffset = programStartOffset;
927+
928+
result.body = readStatementOption();
929+
result.body?.parent = result;
930+
labelStackBase = oldLabelStackBase;
931+
variableStack.length = variableStackHeight;
932+
typeParameterStack.length = typeParameterStackHeight;
933+
if (result.parent is Procedure) {
934+
Procedure parent = result.parent;
935+
parent.transformerFlags |= getAndResetTransformerFlags();
936+
}
937+
};
938+
}
939+
940+
labelStackBase = oldLabelStackBase;
941+
variableStack.length = variableStackHeight;
942+
typeParameterStack.length = typeParameterStackHeight;
943+
944+
return result;
932945
}
933946

934947
void pushVariableDeclaration(VariableDeclaration variable) {
@@ -1165,7 +1178,8 @@ class BinaryBuilder {
11651178
return new AwaitExpression(readExpression());
11661179
case Tag.FunctionExpression:
11671180
int offset = readOffset();
1168-
return new FunctionExpression(readFunctionNode())..fileOffset = offset;
1181+
return new FunctionExpression(readFunctionNode(false))
1182+
..fileOffset = offset;
11691183
case Tag.Let:
11701184
var variable = readVariableDeclaration();
11711185
int stackHeight = variableStack.length;
@@ -1329,7 +1343,7 @@ class BinaryBuilder {
13291343
int offset = readOffset();
13301344
var variable = readVariableDeclaration();
13311345
variableStack.add(variable); // Will be popped by the enclosing scope.
1332-
var function = readFunctionNode();
1346+
var function = readFunctionNode(false);
13331347
return new FunctionDeclaration(variable, function)..fileOffset = offset;
13341348
default:
13351349
throw fail('Invalid statement tag: $tag');
@@ -1737,9 +1751,9 @@ class BinaryBuilderWithMetadata extends BinaryBuilder implements BinarySource {
17371751
}
17381752

17391753
@override
1740-
FunctionNode readFunctionNode() {
1754+
FunctionNode readFunctionNode(bool lazyLoadBody) {
17411755
final nodeOffset = _byteOffset;
1742-
final result = super.readFunctionNode();
1756+
final result = super.readFunctionNode(lazyLoadBody);
17431757
return _associateMetadata(result, nodeOffset);
17441758
}
17451759

0 commit comments

Comments
 (0)