Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Unified Diff: pkg/analyzer/tool/summary/generate.dart

Issue 1579663002: Cache values of fields and lists in summary implementations. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analyzer/tool/summary/generate.dart
diff --git a/pkg/analyzer/tool/summary/generate.dart b/pkg/analyzer/tool/summary/generate.dart
index 5d412e4bfde5907b0e813a1efaea0e002b5baf13..19180745cd4ad9b1451700c7b7b0c5fb0b2b45bf 100644
--- a/pkg/analyzer/tool/summary/generate.dart
+++ b/pkg/analyzer/tool/summary/generate.dart
@@ -281,6 +281,8 @@ class _CodeGenerator {
out();
_generateReader(cls);
out();
+ _generateImpl(cls);
+ out();
}
}
@@ -441,41 +443,21 @@ class _CodeGenerator {
out('}');
}
- void _generateInterface(idlModel.ClassDeclaration cls) {
+ void _generateImpl(idlModel.ClassDeclaration cls) {
String name = cls.name;
- outDoc(cls.documentation);
- out('abstract class $name extends base.SummaryClass {');
- indent(() {
- if (cls.isTopLevel) {
- out('factory $name.fromBuffer(List<int> buffer) {');
- indent(() {
- out('fb.BufferPointer rootRef = new fb.BufferPointer.fromBytes(buffer);');
- out('return const _${name}Reader().read(rootRef);');
- });
- out('}');
- }
- cls.fields.asMap().forEach((index, field) {
- String fieldName = field.name;
- idlModel.FieldType type = field.type;
- out();
- outDoc(field.documentation);
- out('${dartType(type)} get $fieldName;');
- });
- });
- out('}');
- }
-
- void _generateReader(idlModel.ClassDeclaration cls) {
- String name = cls.name;
- String readerName = '_${name}Reader';
- out('class $readerName extends fb.TableReader<$readerName> implements $name {');
+ String implName = '_${name}Impl';
+ out('class $implName implements $name {');
indent(() {
out('final fb.BufferPointer _bp;');
out();
- out('const $readerName([this._bp]);');
+ out('$implName(this._bp);');
out();
- out('@override');
- out('$readerName createReader(fb.BufferPointer bp) => new $readerName(bp);');
+ // Write cache fields.
+ for (idlModel.FieldDeclaration field in cls.fields) {
+ String returnType = dartType(field.type);
+ String fieldName = field.name;
+ out('$returnType _$fieldName;');
+ }
out();
// Write toMap().
out('@override');
@@ -492,8 +474,8 @@ class _CodeGenerator {
String fieldName = field.name;
idlModel.FieldType type = field.type;
String typeName = type.typeName;
- // Prepare "readLines" or "readCode" + "def" + "readSuffix"
- List<String> readLines;
+ // Prepare "readExpr" or "readCode" + "def"
+ String readExpr;
String readCode;
String def = defaultValue(type);
if (type.isList) {
@@ -514,33 +496,68 @@ class _CodeGenerator {
} else if (typeName == 'String') {
readCode = 'const fb.StringReader()';
} else if (_idl.enums.containsKey(typeName)) {
- readLines = <String>[
- 'int index = const fb.Int32Reader().vTableGet(_bp, $index, 0);',
- 'return $typeName.values[index];'
- ];
+ readExpr =
+ '$typeName.values[const fb.Int32Reader().vTableGet(_bp, $index, 0)]';
} else if (_idl.classes.containsKey(typeName)) {
readCode = 'const _${typeName}Reader()';
}
- assert(readCode != null || readLines != null);
+ if (readExpr == null) {
+ assert(readCode != null);
+ readExpr = '$readCode.vTableGet(_bp, $index, $def)';
+ }
// Write the getter implementation.
out();
out('@override');
String returnType = dartType(type);
- if (readLines != null) {
- out('$returnType get $fieldName {');
- indent(() {
- readLines.forEach(out);
- });
- out('}');
- } else {
- String expr = '$readCode.vTableGet(_bp, $index, $def)';
- out('$returnType get $fieldName => $expr;');
- }
+ out('$returnType get $fieldName {');
+ indent(() {
+ out('_$fieldName ??= $readExpr;');
+ out('return _$fieldName;');
+ });
+ out('}');
+ });
+ });
+ out('}');
+ }
+
+ void _generateInterface(idlModel.ClassDeclaration cls) {
+ String name = cls.name;
+ outDoc(cls.documentation);
+ out('abstract class $name extends base.SummaryClass {');
+ indent(() {
+ if (cls.isTopLevel) {
+ out('factory $name.fromBuffer(List<int> buffer) {');
+ indent(() {
+ out('fb.BufferPointer rootRef = new fb.BufferPointer.fromBytes(buffer);');
+ out('return const _${name}Reader().read(rootRef);');
+ });
+ out('}');
+ }
+ cls.fields.asMap().forEach((index, field) {
+ String fieldName = field.name;
+ idlModel.FieldType type = field.type;
+ out();
+ outDoc(field.documentation);
+ out('${dartType(type)} get $fieldName;');
});
});
out('}');
}
+ void _generateReader(idlModel.ClassDeclaration cls) {
+ String name = cls.name;
+ String readerName = '_${name}Reader';
+ String implName = '_${name}Impl';
+ out('class $readerName extends fb.TableReader<$implName> {');
+ indent(() {
+ out('const $readerName();');
+ out();
+ out('@override');
+ out('$implName createObject(fb.BufferPointer bp) => new $implName(bp);');
+ });
+ out('}');
+ }
+
/**
* Return the documentation text of the given [node], or `null` if the [node]
* does not have a comment. Each line is `\n` separated.

Powered by Google App Engine
This is Rietveld 408576698