Files
sdk/pkg/kernel/test/binary/lazy_reading_test.dart
T
Jens Johansen a9469269d7 [kernel] BinaryReader takes Uint8List, not List<int>
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 <fishythefish@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
Reviewed-by: Morgan :) <davidmorgan@google.com>
Reviewed-by: Srujan Gaddam <srujzs@google.com>
Reviewed-by: Alexander Aprelev <aam@google.com>
2024-11-08 08:23:42 +00:00

107 lines
4.2 KiB
Dart

// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// 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/binary/ast_from_binary.dart';
import 'package:kernel/src/tool/find_referenced_libraries.dart';
import 'utils.dart';
void main() {
Library lib;
{
/// Create a library with two classes (A and B) where class A - in its
/// constructor - invokes the constructor for B.
final Uri uri = Uri.parse('org-dartlang:///lib.dart');
lib = new Library(uri, fileUri: uri);
final Class classA = new Class(name: "A", fileUri: uri);
lib.addClass(classA);
final Class classB = new Class(name: "B", fileUri: uri);
lib.addClass(classB);
final Constructor classBConstructor = new Constructor(
new FunctionNode(new EmptyStatement()),
name: new Name(""),
fileUri: uri);
classB.addConstructor(classBConstructor);
final Constructor classAConstructor = new Constructor(
new FunctionNode(new ExpressionStatement(new ConstructorInvocation(
classBConstructor, new Arguments.empty()))),
name: new Name(""),
fileUri: uri);
classA.addConstructor(classAConstructor);
}
Component c = new Component(libraries: [lib]);
c.setMainMethodAndMode(null, false, NonNullableByDefaultCompiledMode.Strong);
Uint8List loadMe = serializeComponent(c);
// Load and make sure we can get at class B from class A (i.e. that it's
// loaded correctly!).
Component loadedComponent = new Component();
new BinaryBuilder(loadMe,
disableLazyReading: false, disableLazyClassReading: false)
.readSingleFileComponent(loadedComponent);
{
final Library loadedLib = loadedComponent.libraries.single;
final Class loadedClassA = loadedLib.classes.first;
final ExpressionStatement loadedConstructorA =
loadedClassA.constructors.single.function.body as ExpressionStatement;
final ConstructorInvocation loadedConstructorInvocation =
loadedConstructorA.expression as ConstructorInvocation;
final Class pointedToClass =
loadedConstructorInvocation.target.enclosingClass;
final Library pointedToLib =
loadedConstructorInvocation.target.enclosingLibrary;
Set<Library> reachable = findAllReferencedLibraries([loadedLib]);
if (reachable.length != 1 || reachable.single != loadedLib) {
throw "Expected only the single library to be reachable, "
"but found $reachable";
}
final Class loadedClassB = loadedLib.classes[1];
if (loadedClassB != pointedToClass) {
throw "Doesn't point to the right class";
}
if (pointedToLib != loadedLib) {
throw "Doesn't point to the right library";
}
}
// Attempt to load again, overwriting the old stuff. This should logically
// "relink" to the newly loaded version.
Component loadedComponent2 = new Component(nameRoot: loadedComponent.root);
new BinaryBuilder(loadMe,
disableLazyReading: false,
disableLazyClassReading: false,
alwaysCreateNewNamedNodes: true)
.readSingleFileComponent(loadedComponent2);
{
final Library loadedLib = loadedComponent2.libraries.single;
final Class loadedClassA = loadedLib.classes.first;
final ExpressionStatement loadedConstructorA =
loadedClassA.constructors.single.function.body as ExpressionStatement;
final ConstructorInvocation loadedConstructorInvocation =
loadedConstructorA.expression as ConstructorInvocation;
final Class pointedToClass =
loadedConstructorInvocation.target.enclosingClass;
final Library pointedToLib =
loadedConstructorInvocation.target.enclosingLibrary;
Set<Library> reachable = findAllReferencedLibraries([loadedLib]);
if (reachable.length != 1 || reachable.single != loadedLib) {
throw "Expected only the single library to be reachable, "
"but found $reachable";
}
final Class loadedClassB = loadedLib.classes[1];
if (loadedClassB != pointedToClass) {
throw "Doesn't point to the right class";
}
if (pointedToLib != loadedLib) {
throw "Doesn't point to the right library";
}
}
}