Skip to content

Commit

Permalink
[kernel] BinaryReader takes Uint8List, not List<int>
Browse files Browse the repository at this point in the history
In AOT this makes reading faster:

Output from `out/ReleaseX64/dart pkg/front_end/tool/benchmarker.dart --iterations=10 --snapshot=pkg/front_end/test/kernel_binary_bench.aot.1 --snapshot=pkg/front_end/test/kernel_binary_bench.aot.2 --arguments="--warmups=10" --arguments="--iterations=5" --arguments="AstFromBinaryEager" --arguments="out/ReleaseX64/vm_platform_strong.dill"`:

```
msec task-clock:u: -8.6925% +/- 0.5737% (-167.09 +/- 11.03)
page-faults:u: 0.1410% +/- 0.0051% (243.00 +/- 8.71)
cycles:u: -10.2918% +/- 0.6161% (-732576747.50 +/- 43853449.16)
instructions:u: -14.4988% +/- 0.0004% (-1636799813.90 +/- 39902.18)
branch-misses:u: -3.4891% +/- 2.1142% (-1166085.00 +/- 706582.35)
seconds time elapsed: -8.7005% +/- 0.5634% (-0.17 +/- 0.01)
seconds user: -9.9752% +/- 1.5104% (-0.17 +/- 0.03)
```

Stats running manually (run as e.g. `out/ReleaseX64/dart-sdk/bin/dartaotruntime pkg/front_end/test/kernel_binary_bench.aot.1 --warmups=10 --iterations=5 AstFromBinaryEager out/ReleaseX64/vm_platform_strong.dill`):

```
AstFromBinaryEagerCold: -12.5174% +/- 3.10688%
AstFromBinaryEagerWarmup: -8.33675% +/- 2.62433%
AstFromBinaryEager: -10.3432% +/- 3.68375%
```

I don't expect there to be much of a change (if any) in JIT as the actual type was in practise always Uint8List anyway.

TEST=Existing tests.

Change-Id: I86b16ed207343848dee2e376f42598c223bbc48f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/393740
Reviewed-by: Mayank Patke <[email protected]>
Reviewed-by: Johnni Winther <[email protected]>
Commit-Queue: Jens Johansen <[email protected]>
Reviewed-by: Morgan :) <[email protected]>
Reviewed-by: Srujan Gaddam <[email protected]>
Reviewed-by: Alexander Aprelev <[email protected]>
  • Loading branch information
jensjoha authored and Commit Queue committed Nov 8, 2024
1 parent 90b52f8 commit a946926
Show file tree
Hide file tree
Showing 58 changed files with 205 additions and 151 deletions.
4 changes: 2 additions & 2 deletions pkg/build_integration/lib/file_system/multi_root.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class MultiRootFileSystemEntity implements FileSystemEntity {
Future<Uint8List> readAsBytes() async => (await delegate).readAsBytes();

@override
Future<List<int>> readAsBytesAsyncIfPossible() async =>
Future<Uint8List> readAsBytesAsyncIfPossible() async =>
(await delegate).readAsBytes();

@override
Expand All @@ -109,7 +109,7 @@ class MissingFileSystemEntity implements FileSystemEntity {
Future.error(FileSystemException(uri, 'File not found'));

@override
Future<List<int>> readAsBytesAsyncIfPossible() => readAsBytes();
Future<Uint8List> readAsBytesAsyncIfPossible() => readAsBytes();

@override
Future<String> readAsString() =>
Expand Down
2 changes: 1 addition & 1 deletion pkg/build_integration/lib/file_system/single_root.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class SingleRootFileSystemEntity implements FileSystemEntity {
Future<Uint8List> readAsBytes() async => delegate.readAsBytes();

@override
Future<List<int>> readAsBytesAsyncIfPossible() async =>
Future<Uint8List> readAsBytesAsyncIfPossible() async =>
delegate.readAsBytesAsyncIfPossible();

@override
Expand Down
2 changes: 1 addition & 1 deletion pkg/compiler/lib/src/kernel/front_end_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class _CompilerFileSystemEntity implements fe.FileSystemEntity {
}

@override
Future<List<int>> readAsBytesAsyncIfPossible() => readAsBytes();
Future<Uint8List> readAsBytesAsyncIfPossible() => readAsBytes();

@override
Future<bool> exists() async {
Expand Down
2 changes: 1 addition & 1 deletion pkg/compiler/lib/src/phase/load_kernel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ Future<Output?> run(Input input) async {
if (component == null) return null;
if (input.forceSerialization) {
// TODO(johnniwinther): Remove this when #34942 is fixed.
List<int> data = fe.serializeComponent(component);
Uint8List data = fe.serializeComponent(component);
component = ir.Component();
BinaryBuilder(data).readComponent(component);
// Ensure we use the new deserialized entry point library.
Expand Down
7 changes: 4 additions & 3 deletions pkg/compiler/lib/src/serialization/strategies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';
import 'dart:typed_data';

import 'package:front_end/src/api_unstable/dart2js.dart' as ir
show serializeComponent, ByteSink;
Expand All @@ -22,7 +23,7 @@ import 'task.dart';
abstract class SerializationStrategy<T> {
const SerializationStrategy();

List<int> unpackAndSerializeComponent(GlobalTypeInferenceResults results) {
Uint8List unpackAndSerializeComponent(GlobalTypeInferenceResults results) {
JClosedWorld closedWorld = results.closedWorld;
ir.Component component = closedWorld.elementMap.programEnv.mainComponent;
return serializeComponent(component);
Expand All @@ -33,11 +34,11 @@ abstract class SerializationStrategy<T> {
CompilerOptions options,
SerializationIndices indices);

List<int> serializeComponent(ir.Component component) {
Uint8List serializeComponent(ir.Component component) {
return ir.serializeComponent(component);
}

ir.Component deserializeComponent(List<int> data) {
ir.Component deserializeComponent(Uint8List data) {
ir.Component component = ir.Component();
BinaryBuilder(data).readComponent(component);
return component;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';
import 'dart:typed_data';

import 'package:compiler/compiler_api.dart' as api;
import 'package:compiler/src/commandline_options.dart';
Expand Down Expand Up @@ -273,7 +274,7 @@ JClosedWorld cloneClosedWorld(Compiler compiler, JClosedWorld closedWorld,
SerializationStrategy strategy) {
SerializationIndices indices = SerializationIndices();
ir.Component component = closedWorld.elementMap.programEnv.mainComponent;
List<int> irData = strategy.serializeComponent(component);
Uint8List irData = strategy.serializeComponent(component);
final closedWorldData = strategy.serializeClosedWorld(
closedWorld, compiler.options, indices) as List<int>;
print('data size: ${closedWorldData.length}');
Expand Down Expand Up @@ -302,7 +303,7 @@ JClosedWorld cloneClosedWorld(Compiler compiler, JClosedWorld closedWorld,
GlobalTypeInferenceResults cloneInferenceResults(Compiler compiler,
GlobalTypeInferenceResults results, SerializationStrategy strategy) {
SerializationIndices indices = SerializationIndices(testMode: true);
List<int> irData = strategy.unpackAndSerializeComponent(results);
Uint8List irData = strategy.unpackAndSerializeComponent(results);
final closedWorldData = strategy.serializeClosedWorld(
results.closedWorld, compiler.options, indices) as List<int>;
ir.Component newComponent = strategy.deserializeComponent(irData);
Expand Down
2 changes: 1 addition & 1 deletion pkg/dev_compiler/lib/src/kernel/asset_file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class AssetFileSystemEntity implements FileSystemEntity {
}

@override
Future<List<int>> readAsBytesAsyncIfPossible() => readAsBytes();
Future<Uint8List> readAsBytesAsyncIfPossible() => readAsBytes();

@override
Future<String> readAsString() async {
Expand Down
2 changes: 1 addition & 1 deletion pkg/front_end/lib/src/api_prototype/file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ abstract class FileSystemEntity {
/// If an error occurs while attempting to read the file (e.g. because no such
/// file exists, or the entity is a directory), the future is completed with
/// [FileSystemException].
Future<List<int>> readAsBytesAsyncIfPossible();
Future<Uint8List> readAsBytesAsyncIfPossible();

/// Attempts to access this file system entity as a file and read its contents
/// as a string.
Expand Down
4 changes: 3 additions & 1 deletion pkg/front_end/lib/src/api_prototype/kernel_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
/// Defines the front-end API for converting source code to Dart Kernel objects.
library front_end.kernel_generator;

import 'dart:typed_data';

import 'package:_fe_analyzer_shared/src/messages/codes.dart'
show messageMissingMain, noLength;
import 'package:_fe_analyzer_shared/src/messages/severity.dart' show Severity;
Expand Down Expand Up @@ -109,7 +111,7 @@ Future<CompilerResult> kernelForModule(
/// Result object for [kernelForProgram] and [kernelForModule].
abstract class CompilerResult {
/// The generated summary bytes, if it was requested.
List<int>? get summary;
Uint8List? get summary;

/// The generated component, if it was requested.
Component? get component;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class MemoryFileSystemEntity implements FileSystemEntity {

@override
// Coverage-ignore(suite): Not run.
Future<List<int>> readAsBytesAsyncIfPossible() => readAsBytes();
Future<Uint8List> readAsBytesAsyncIfPossible() => readAsBytes();

@override
// Coverage-ignore(suite): Not run.
Expand Down
4 changes: 2 additions & 2 deletions pkg/front_end/lib/src/api_prototype/standard_file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class _IoFileSystemEntity implements FileSystemEntity {

@override
// Coverage-ignore(suite): Not run.
Future<List<int>> readAsBytesAsyncIfPossible() async {
Future<Uint8List> readAsBytesAsyncIfPossible() async {
try {
FileSystemDependencyTracker.recordDependency(tracker, uri);
return await new io.File.fromUri(uri).readAsBytes();
Expand Down Expand Up @@ -168,7 +168,7 @@ class DataFileSystemEntity implements FileSystemEntity {
Future<bool> existsAsyncIfPossible() => exists();

@override
Future<List<int>> readAsBytesAsyncIfPossible() => readAsBytes();
Future<Uint8List> readAsBytesAsyncIfPossible() => readAsBytes();

@override
Future<String> readAsString() {
Expand Down
4 changes: 3 additions & 1 deletion pkg/front_end/lib/src/api_prototype/summary_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
/// Defines the front-end API for converting source code to summaries.
library front_end.summary_generator;

import 'dart:typed_data';

import '../base/processed_options.dart';
import '../kernel_generator_impl.dart';
import 'compiler_options.dart';
Expand All @@ -28,7 +30,7 @@ import 'compiler_options.dart';
/// was compiled from sources.
///
/// The return value is a list of bytes to write to the summary file.
Future<List<int>?> summaryFor(List<Uri> sources, CompilerOptions options,
Future<Uint8List?> summaryFor(List<Uri> sources, CompilerOptions options,
{bool truncate = false}) async {
return (await generateKernel(
new ProcessedOptions(options: options, inputs: sources),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:typed_data';

import 'package:kernel/kernel.dart' show Component, CanonicalName, Library;
import 'package:kernel/target/targets.dart' show Target;
import 'package:macros/src/executor/serialization.dart' show SerializationMode;
Expand Down Expand Up @@ -212,7 +214,7 @@ Future<InitializedCompilerState> initializeIncrementalCompiler(
throw new StateError("Expected to get digest for $additionalDillUri");
}

List<int> bytes =
Uint8List bytes =
await fileSystem.entityForUri(additionalDillUri).readAsBytes();
WorkerInputComponent cachedInput = new WorkerInputComponent(
digest,
Expand Down
4 changes: 3 additions & 1 deletion pkg/front_end/lib/src/base/get_dependencies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

library fasta.get_dependencies;

import 'dart:typed_data';

import 'package:kernel/kernel.dart' show Component, loadComponentFromBytes;
import 'package:kernel/target/targets.dart' show Target;

Expand Down Expand Up @@ -41,7 +43,7 @@ Future<List<Uri>> getDependencies(Uri script,
DillTarget dillTarget =
new DillTarget(c, c.options.ticker, uriTranslator, c.options.target);
if (platform != null) {
List<int> bytes = await fileSystem.entityForUri(platform).readAsBytes();
Uint8List bytes = await fileSystem.entityForUri(platform).readAsBytes();
Component platformComponent = loadComponentFromBytes(bytes);
dillTarget.loader.appendLibraries(platformComponent);
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/front_end/lib/src/base/hybrid_file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class HybridFileSystemEntity implements FileSystemEntity {

@override
// Coverage-ignore(suite): Not run.
Future<List<int>> readAsBytesAsyncIfPossible() async =>
Future<Uint8List> readAsBytesAsyncIfPossible() async =>
(await delegate).readAsBytesAsyncIfPossible();

@override
Expand Down
8 changes: 4 additions & 4 deletions pkg/front_end/lib/src/base/incremental_compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2509,15 +2509,15 @@ class _InitializationFromSdkSummary extends _InitializationStrategy {
_ComponentProblems componentProblems,
IncrementalSerializer? incrementalSerializer,
RecorderForTesting? recorderForTesting) async {
List<int>? summaryBytes = await context.options.loadSdkSummaryBytes();
Uint8List? summaryBytes = await context.options.loadSdkSummaryBytes();
return _prepareSummary(
dillLoadedData, summaryBytes, uriTranslator, context, data);
}

// Coverage-ignore(suite): Not run.
int _prepareSummary(
DillTarget dillLoadedTarget,
List<int>? summaryBytes,
Uint8List? summaryBytes,
UriTranslator uriTranslator,
CompilerContext context,
IncrementalCompilerData data) {
Expand Down Expand Up @@ -2607,7 +2607,7 @@ class _InitializationFromUri extends _InitializationFromSdkSummary {
_ComponentProblems componentProblems,
IncrementalSerializer? incrementalSerializer,
RecorderForTesting? recorderForTesting) async {
List<int>? summaryBytes = await context.options.loadSdkSummaryBytes();
Uint8List? summaryBytes = await context.options.loadSdkSummaryBytes();
int bytesLength = _prepareSummary(
dillLoadedData, summaryBytes, uriTranslator, context, data);
try {
Expand Down Expand Up @@ -2685,7 +2685,7 @@ class _InitializationFromUri extends _InitializationFromSdkSummary {
FileSystemEntity entity =
context.options.fileSystem.entityForUri(initializeFromDillUri);
if (await entity.exists()) {
List<int> initializationBytes = await entity.readAsBytes();
Uint8List initializationBytes = await entity.readAsBytes();
if (initializationBytes.isNotEmpty) {
dillLoadedData.ticker.logMs("Read $initializeFromDillUri");
data.initializationBytes = initializationBytes;
Expand Down
14 changes: 7 additions & 7 deletions pkg/front_end/lib/src/base/processed_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ class ProcessedOptions {
return _sdkSummary;
}

List<int>? _sdkSummaryBytes;
Uint8List? _sdkSummaryBytes;
bool _triedLoadingSdkSummary = false;

// Coverage-ignore(suite): Not run.
/// Get the bytes of the SDK outline, if any.
Future<List<int>?> loadSdkSummaryBytes() async {
Future<Uint8List?> loadSdkSummaryBytes() async {
if (_sdkSummaryBytes == null && !_triedLoadingSdkSummary) {
if (sdkSummary == null) return null;
FileSystemEntity entry = fileSystem.entityForUri(sdkSummary!);
Expand Down Expand Up @@ -537,7 +537,7 @@ class ProcessedOptions {
if (_sdkSummaryComponent == null) {
// Coverage-ignore-block(suite): Not run.
if (sdkSummary == null) return null;
List<int>? bytes = await loadSdkSummaryBytes();
Uint8List? bytes = await loadSdkSummaryBytes();
if (bytes != null && bytes.isNotEmpty) {
_sdkSummaryComponent =
loadComponent(bytes, nameRoot, fileUri: sdkSummary);
Expand All @@ -563,11 +563,11 @@ class ProcessedOptions {
List<Uri> uris = _raw.additionalDills;
if (uris.isEmpty) return const <Component>[];
// TODO(sigmund): throttle # of concurrent operations.
List<List<int>?> allBytes = await Future.wait(
List<Uint8List?> allBytes = await Future.wait(
uris.map((uri) => _readAsBytes(fileSystem.entityForUri(uri))));
List<Component> result = [];
for (int i = 0; i < uris.length; i++) {
List<int>? bytes = allBytes[i];
Uint8List? bytes = allBytes[i];
if (bytes == null) continue;
result.add(loadComponent(bytes, nameRoot, fileUri: uris[i]));
}
Expand All @@ -578,7 +578,7 @@ class ProcessedOptions {

// Coverage-ignore(suite): Not run.
/// Helper to load a .dill file from [uri] using the existing [nameRoot].
Component loadComponent(List<int> bytes, CanonicalName? nameRoot,
Component loadComponent(Uint8List bytes, CanonicalName? nameRoot,
{bool? alwaysCreateNewNamedNodes, Uri? fileUri}) {
Component component =
target.configureComponent(new Component(nameRoot: nameRoot));
Expand Down Expand Up @@ -950,7 +950,7 @@ class ProcessedOptions {
}

// Coverage-ignore(suite): Not run.
Future<List<int>?> _readAsBytes(FileSystemEntity file) async {
Future<Uint8List?> _readAsBytes(FileSystemEntity file) async {
try {
return await file.readAsBytes();
} on FileSystemException catch (error) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/front_end/lib/src/kernel_generator_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
/// Defines the front-end API for converting source code to Dart Kernel objects.
library front_end.kernel_generator_impl;

import 'dart:typed_data';

import 'package:_fe_analyzer_shared/src/messages/severity.dart' show Severity;
import 'package:kernel/ast.dart';
import 'package:kernel/class_hierarchy.dart';
Expand Down Expand Up @@ -177,7 +179,7 @@ Future<InternalCompilerResult> _buildInternal(CompilerContext compilerContext,
BuildResult buildResult =
await kernelTarget.buildOutlines(nameRoot: nameRoot);
Component summaryComponent = buildResult.component!;
List<int>? summary = null;
Uint8List? summary = null;
if (buildSummary) {
// Coverage-ignore-block(suite): Not run.
if (options.verify) {
Expand Down Expand Up @@ -278,7 +280,7 @@ Future<InternalCompilerResult> _buildInternal(CompilerContext compilerContext,
class InternalCompilerResult implements CompilerResult {
/// The generated summary bytes, if it was requested.
@override
final List<int>? summary;
final Uint8List? summary;

/// The generated component, if it was requested.
@override
Expand Down
4 changes: 3 additions & 1 deletion pkg/front_end/lib/src/testing/compiler_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
/// Common compiler options and helper functions used for testing.
library front_end.testing.compiler_options_common;

import 'dart:typed_data';

import 'package:kernel/ast.dart' show Library, Component;

import '../api_prototype/front_end.dart'
Expand Down Expand Up @@ -61,7 +63,7 @@ Future<Component?> compileUnit(
/// Generate a summary for a modular compilation unit.
///
/// Wraps [summaryFor] with some default testing options (see [setup]).
Future<List<int>?> summarize(List<String> inputs, Map<String, dynamic> sources,
Future<Uint8List?> summarize(List<String> inputs, Map<String, dynamic> sources,
{List<String> additionalDills = const [],
CompilerOptions? options,
bool truncate = false}) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:io' show Platform, exit;
import 'dart:typed_data';

import 'package:kernel/binary/ast_from_binary.dart' show BinaryBuilder;
import 'package:kernel/kernel.dart' show Component;
Expand All @@ -11,7 +12,7 @@ import 'package:kernel/text/ast_to_text.dart' show componentToString;
import '../incremental_suite.dart' show normalCompileToBytes;

main() async {
List<int> bytes = await normalCompileToBytes(
Uint8List bytes = await normalCompileToBytes(
Platform.script.resolve("load_dill_twice_lib_1.dart"));

Component c = new Component();
Expand Down
Loading

0 comments on commit a946926

Please sign in to comment.