Skip to content

Commit 869a0a8

Browse files
Markzipandart-scoped@luci-project-accounts.iam.gserviceaccount.com
authored andcommitted
[ddc] Optimize generated JS scans in DDC.
This addresses some of the DDC Library Bundle-specific degradations in flutter/flutter#192687 AI-discovered + verified by hand. Saves ~5% DDC compile time for modestly sized apps. 1) In DdcLibraryBundleBuilder, we used to replace the first instance of metricLocationID, then every subsequent instance with '0'. This effectively costs us a full-JS-library scan per library. Now we just emit the metricsLocationID for the first library only. 2) In jsProgramToCode, use _replaceLast to avoid double-scanning the generated JS. We use _replaceLast since the sourcemap position is closer to the end of most libraries than the beginning. 3) Add encodedSourceMap to JSCode to avoid re-encoding the source map to JSON. Change-Id: I57311aabc66954d5b56d395b584ddda2333412cd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/556520 Reviewed-by: Nicholas Shahan <[email protected]> Commit-Queue: Mark Zhou <[email protected]> Reviewed-by: Johnni Winther <[email protected]>
1 parent ee6c4fd commit 869a0a8

4 files changed

Lines changed: 52 additions & 28 deletions

File tree

‎pkg/dev_compiler/lib/src/command/command.dart‎

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -553,10 +553,9 @@ Future<CompilerResult> _compile(
553553
);
554554

555555
outFiles.add(file.writeAsString(jsCode.code));
556-
if (jsCode.sourceMap != null) {
557-
outFiles.add(
558-
File('$output.map').writeAsString('${json.encode(jsCode.sourceMap)}\n'),
559-
);
556+
var encodedSourceMap = jsCode.encodedSourceMap;
557+
if (encodedSourceMap != null) {
558+
outFiles.add(File('$output.map').writeAsString('$encodedSourceMap\n'));
560559
}
561560
if (jsCode.metadata != null) {
562561
outFiles.add(
@@ -713,10 +712,9 @@ Future<CompilerResult> compileSdkFromDill(List<String> args) async {
713712
);
714713

715714
outFiles.add(file.writeAsString(jsCode.code));
716-
if (jsCode.sourceMap != null) {
717-
outFiles.add(
718-
File('$output.map').writeAsString(json.encode(jsCode.sourceMap)),
719-
);
715+
var encodedSourceMap = jsCode.encodedSourceMap;
716+
if (encodedSourceMap != null) {
717+
outFiles.add(File('$output.map').writeAsString(encodedSourceMap));
720718
}
721719
}
722720
await Future.wait(outFiles);
@@ -741,11 +739,16 @@ class JSCode {
741739
/// comment at end of the file.
742740
final String code;
743741

744-
/// The JSON of the source map, if generated, otherwise `null`.
742+
/// The JSON-encoded source map, if generated, otherwise `null`.
743+
final String? encodedSourceMap;
744+
745+
/// The decoded JSON of [encodedSourceMap], if generated, otherwise `null`.
745746
///
746-
/// The source paths will initially be absolute paths. They can be adjusted
747-
/// using [placeSourceMap].
748-
final Map<String, Object?>? sourceMap;
747+
/// Decoded lazily on first access. Prefer [encodedSourceMap] to avoid
748+
/// unnecessary decoding.
749+
late final Map<String, Object?>? sourceMap = encodedSourceMap == null
750+
? null
751+
: json.decode(encodedSourceMap!) as Map<String, Object?>;
749752

750753
/// Module and library information
751754
///
@@ -760,7 +763,7 @@ class JSCode {
760763
/// helping the debugger map between dart and JS objects.
761764
final ModuleSymbols? symbols;
762765

763-
JSCode(this.code, this.sourceMap, {this.symbols, this.metadata});
766+
JSCode(this.code, this.encodedSourceMap, {this.symbols, this.metadata});
764767
}
765768

766769
/// Converts [moduleTree] to [JSCode], using [format].
@@ -829,10 +832,6 @@ JSCode jsProgramToCode(
829832

830833
var text = printer.getText();
831834
var encodedMap = json.encode(builtMap);
832-
var rawSourceMap = inlineSourceMap
833-
? js.escapedString(encodedMap, "'").value
834-
: 'null';
835-
text = text.replaceFirst(ProgramCompiler.sourceMapLocationID, rawSourceMap);
836835

837836
// This is intended to be used by our build/debug tools to gather metrics.
838837
// See pkg/dev_compiler/lib/js/ddc/ddc_module_loader.js for runtime code that
@@ -847,24 +846,30 @@ JSCode jsProgramToCode(
847846
// compilation server, not the browser. We don't yet have the infra for that.
848847
if (ModuleFormat.ddcLibraryBundle == format) {
849848
// TODO(nshahan): Find a better solution for the size of the source map
850-
// per library. For now we just use the size of the full source map and only
851-
// count it once in the runtime when the first library gets defined.
852-
text = text.replaceFirst(
853-
ProgramCompiler.metricsLocationID,
849+
// per library. For now we just use the size of the full source map and
850+
// attach it to the last library in the bundle.
851+
text = _replaceLast(
852+
text,
853+
LibraryCompiler.metricsLocationID,
854854
'${encodedMap.length}',
855855
);
856-
text = text.replaceAll(LibraryCompiler.metricsLocationID, '0');
857856
} else {
858857
var compileTimeStatistics = {
859858
'dartSize': _computeDartSize(component!),
860859
'sourceMapSize': encodedMap.length,
861860
};
862-
text = text.replaceFirst(
861+
text = _replaceLast(
862+
text,
863863
ProgramCompiler.metricsLocationID,
864864
'$compileTimeStatistics',
865865
);
866866
}
867867

868+
var rawSourceMap = inlineSourceMap
869+
? js.escapedString(encodedMap, "'").value
870+
: 'null';
871+
text = _replaceLast(text, ProgramCompiler.sourceMapLocationID, rawSourceMap);
872+
868873
var debugMetadata = emitDebugMetadata
869874
? _emitMetadata(moduleTree, component!, mapUrl!, jsUrl!, fullDillUri)
870875
: null;
@@ -878,7 +883,19 @@ JSCode jsProgramToCode(
878883
)
879884
: null;
880885

881-
return JSCode(text, builtMap, symbols: debugSymbols, metadata: debugMetadata);
886+
return JSCode(
887+
text,
888+
builtMap == null ? null : encodedMap,
889+
symbols: debugSymbols,
890+
metadata: debugMetadata,
891+
);
892+
}
893+
894+
/// Replaces the last occurrence of [from] with [to] in [text].
895+
String _replaceLast(String text, String from, String to) {
896+
var index = text.lastIndexOf(from);
897+
if (index == -1) return text;
898+
return text.replaceRange(index, index + from.length, to);
882899
}
883900

884901
/// Assembles symbol information describing the nodes from the AST [component]

‎pkg/dev_compiler/lib/src/compiler/module_builder.dart‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,8 @@ class DdcLibraryBundleBuilder extends _ModuleBuilder {
585585
}
586586
var body = <ModuleItem>[];
587587
// Collect imports/exports/statements.
588-
for (var library in module.libraries) {
588+
for (var i = 0; i < module.libraries.length; i++) {
589+
var library = module.libraries[i];
589590
// Handle each library separately.
590591
imports.clear();
591592
statements.clear();
@@ -617,7 +618,11 @@ class DdcLibraryBundleBuilder extends _ModuleBuilder {
617618
js.string(library.name!),
618619
initFunction,
619620
js.number(library.dartSize!),
620-
LibraryCompiler.metricsLocationID,
621+
// `metricsLocationID` is replaced in command.dart via a backwards
622+
// scan, so we place it at the last library.
623+
i == module.libraries.length - 1
624+
? LibraryCompiler.metricsLocationID
625+
: js.number(0),
621626
],
622627
);
623628
body.add(resultModule);

‎pkg/dev_compiler/test/expression_compiler/test_compiler.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class TestExpressionCompiler {
126126
component: component,
127127
);
128128
var codeBytes = utf8.encode(code.code);
129-
var sourceMapBytes = utf8.encode(json.encode(code.sourceMap));
129+
var sourceMapBytes = utf8.encode(code.encodedSourceMap!);
130130

131131
File(output.toFilePath()).writeAsBytesSync(codeBytes);
132132
File('${output.toFilePath()}.map').writeAsBytesSync(sourceMapBytes);

‎pkg/frontend_server/lib/src/javascript_bundle.dart‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,9 @@ class IncrementalJavaScriptBundler {
410410
packageConfig: useDebuggerModuleNames ? null : packageConfig,
411411
);
412412
final Uint8List codeBytes = utf8.encode(code.code);
413-
final Uint8List sourceMapBytes = utf8.encode(json.encode(code.sourceMap));
413+
final Uint8List sourceMapBytes = utf8.encode(
414+
code.encodedSourceMap ?? 'null',
415+
);
414416
final Uint8List? metadataBytes = emitDebugMetadata
415417
? utf8.encode(json.encode(code.metadata))
416418
: null;

0 commit comments

Comments
 (0)