Skip to content

Commit 211190e

Browse files
alexmarkovcommit-bot@chromium.org
authored andcommitted
[vm/bytecode] Reuse ConstantEvaluator instance in bytecode generator
By reusing ConstantEvaluator instance among members we avoid recalculating values of constant static fields. Change-Id: I5c13489a353bf101cd4a01d5993e44842042c2d6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/109892 Reviewed-by: Régis Crelier <[email protected]> Reviewed-by: Ryan Macnak <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
1 parent c2af59f commit 211190e

File tree

1 file changed

+11
-28
lines changed

1 file changed

+11
-28
lines changed

pkg/vm/lib/bytecode/gen_bytecode.dart

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ library vm.bytecode.gen_bytecode;
77
// TODO(askesc): We should not need to call the constant evaluator
88
// explicitly once constant-update-2018 is shipped.
99
import 'package:front_end/src/api_prototype/constant_evaluator.dart'
10-
show ConstantEvaluator, EvaluationEnvironment, ErrorReporter;
10+
show ConstantEvaluator, EvaluationEnvironment;
1111
import 'package:front_end/src/api_unstable/vm.dart'
1212
show
1313
CompilerContext,
@@ -23,7 +23,6 @@ import 'package:kernel/core_types.dart' show CoreTypes;
2323
import 'package:kernel/external_name.dart'
2424
show getExternalName, getNativeExtensionUris;
2525
import 'package:kernel/library_index.dart' show LibraryIndex;
26-
import 'package:kernel/target/targets.dart' show ConstantsBackend;
2726
import 'package:kernel/type_algebra.dart'
2827
show Substitution, containsTypeVariable;
2928
import 'package:kernel/type_environment.dart' show TypeEnvironment;
@@ -80,10 +79,12 @@ void generateBytecode(
8079
final typeEnvironment = new TypeEnvironment(coreTypes, hierarchy);
8180
final constantsBackend = new VmConstantsBackend(coreTypes);
8281
final errorReporter = new ForwardConstantEvaluationErrors();
82+
final constantEvaluator = new ConstantEvaluator(constantsBackend,
83+
options.environmentDefines, typeEnvironment, errorReporter);
8384
libraries ??= component.libraries;
8485
try {
8586
final bytecodeGenerator = new BytecodeGenerator(component, coreTypes,
86-
hierarchy, typeEnvironment, constantsBackend, options, errorReporter);
87+
hierarchy, typeEnvironment, constantEvaluator, options);
8788
for (var library in libraries) {
8889
bytecodeGenerator.visitLibrary(library);
8990
}
@@ -98,9 +99,8 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
9899
final CoreTypes coreTypes;
99100
final ClassHierarchy hierarchy;
100101
final TypeEnvironment typeEnvironment;
101-
final ConstantsBackend constantsBackend;
102+
final ConstantEvaluator constantEvaluator;
102103
final BytecodeOptions options;
103-
final ErrorReporter errorReporter;
104104
final BytecodeMetadataRepository metadata = new BytecodeMetadataRepository();
105105
final RecognizedMethods recognizedMethods;
106106
final int formatVersion;
@@ -123,7 +123,6 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
123123
Set<TypeParameter> functionTypeParametersSet;
124124
List<DartType> instantiatorTypeArguments;
125125
LocalVariables locals;
126-
ConstantEvaluator constantEvaluator;
127126
Map<LabeledStatement, Label> labeledStatements;
128127
Map<SwitchCase, Label> switchCases;
129128
Map<TryCatch, TryBlock> tryCatches;
@@ -141,14 +140,8 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
141140
List<int> savedMaxSourcePositions;
142141
int maxSourcePosition;
143142

144-
BytecodeGenerator(
145-
ast.Component component,
146-
this.coreTypes,
147-
this.hierarchy,
148-
this.typeEnvironment,
149-
this.constantsBackend,
150-
this.options,
151-
this.errorReporter)
143+
BytecodeGenerator(ast.Component component, this.coreTypes, this.hierarchy,
144+
this.typeEnvironment, this.constantEvaluator, this.options)
152145
: recognizedMethods = new RecognizedMethods(typeEnvironment),
153146
formatVersion = currentBytecodeFormatVersion,
154147
astUriToSource = component.uriToSource {
@@ -378,14 +371,8 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
378371
if (nodes.isEmpty) {
379372
return const Annotations(null, false);
380373
}
381-
final savedConstantEvaluator = constantEvaluator;
382-
if (constantEvaluator == null) {
383-
constantEvaluator = new ConstantEvaluator(constantsBackend,
384-
options.environmentDefines, typeEnvironment, errorReporter)
385-
..env = new EvaluationEnvironment();
386-
}
387-
List<Constant> constants = nodes.map(_evaluateConstantExpression).toList();
388-
constantEvaluator = savedConstantEvaluator;
374+
List<Constant> constants = constantEvaluator.withNewEnvironment(
375+
() => nodes.map(_evaluateConstantExpression).toList());
389376
bool hasPragma = constants.any(_isPragma);
390377
if (!options.emitAnnotations) {
391378
if (hasPragma) {
@@ -1346,11 +1333,7 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
13461333
new List<TypeParameter>.from(enclosingFunction.typeParameters);
13471334
functionTypeParametersSet = functionTypeParameters.toSet();
13481335
}
1349-
// TODO(alexmarkov): improve caching in ConstantEvaluator and reuse it
1350-
constantEvaluator = new ConstantEvaluator(constantsBackend,
1351-
options.environmentDefines, typeEnvironment, errorReporter)
1352-
..env = new EvaluationEnvironment();
1353-
1336+
constantEvaluator.env = new EvaluationEnvironment();
13541337
if (node.isAbstract || node is Field && !hasInitializerCode(node)) {
13551338
return;
13561339
}
@@ -1469,7 +1452,7 @@ class BytecodeGenerator extends RecursiveVisitor<Null> {
14691452
functionTypeParametersSet = null;
14701453
instantiatorTypeArguments = null;
14711454
locals = null;
1472-
constantEvaluator = null;
1455+
constantEvaluator.env = null;
14731456
labeledStatements = null;
14741457
switchCases = null;
14751458
tryCatches = null;

0 commit comments

Comments
 (0)