Skip to content

Commit 0e06fcf

Browse files
authored
[record_use] Remove definition loading unit (dart-lang#3061)
In `dart2js`, definitions currently include a `loading_unit` property which typically represents the "dominating" loading unit (the shared ancestor of all loading units where that definition is used). When assets are associated with a definition, using this definition-level `loading_unit` can lead to assets being loaded too early. For example, if a definition is used in two different deferred loading units, its dominating unit might be the main bundle. Loading the asset in the main bundle defeats the purpose of deferring it. To fix this, we want to postpone asset loading until one of the actual loading units that uses the definition is loaded. Since every recorded `Call` and `Instance` already includes its own `loading_unit`, the property on the `Definition` itself is redundant and misleading for the web backend. Closes: dart-lang#2986 Issues to be addressed in follow up PRs: * dart-lang#3022 * dart-lang#2888 ## Changes ### 1. JSON Schema & Syntax - Removed `loading_unit` from the `Definition` object in `record_use.schema.json`. - Updated `Recording` to use `identifier` directly as its key property. ### 2. Dart API - Removed the `Definition` class. `Recordings` now uses `Identifier` as the primary key for the `callsForDefinition` and `instancesForDefinition` maps. - Added a `TODO` to `Identifier` to rename it to `Definition` in a future refactor ([dart-lang#2888](dart-lang#2888)).
1 parent 51d34eb commit 0e06fcf

68 files changed

Lines changed: 560 additions & 1015 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎pkgs/hooks_runner/test/build_runner/resources_test.dart‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ void main() async {
2424
final resourcesUri = tempUri.resolve('treeshaking_info.json');
2525
final recordings = Recordings(
2626
metadata: Metadata(version: Version(1, 0, 0), comment: 'Empty'),
27-
callsForDefinition: {},
28-
instancesForDefinition: {},
27+
calls: {},
28+
instances: {},
2929
);
3030
await File.fromUri(
3131
resourcesUri,
@@ -120,12 +120,10 @@ void main() async {
120120
/// bin/pirate_adventure.dart.
121121
final _pirateAdventureRecordings = Recordings(
122122
metadata: Metadata(version: Version(1, 0, 0), comment: 'Filtering test'),
123-
callsForDefinition: {
124-
const Definition(
125-
identifier: Identifier(
126-
importUri: 'package:pirate_speak/src/definitions.dart',
127-
name: 'pirateSpeak',
128-
),
123+
calls: {
124+
const Identifier(
125+
importUri: 'package:pirate_speak/src/definitions.dart',
126+
name: 'pirateSpeak',
129127
): [
130128
const CallWithArguments(
131129
loadingUnit: 'root',
@@ -138,11 +136,9 @@ final _pirateAdventureRecordings = Recordings(
138136
namedArguments: {},
139137
),
140138
],
141-
const Definition(
142-
identifier: Identifier(
143-
importUri: 'package:pirate_technology/src/definitions.dart',
144-
name: 'useCannon',
145-
),
139+
const Identifier(
140+
importUri: 'package:pirate_technology/src/definitions.dart',
141+
name: 'useCannon',
146142
): [
147143
const CallWithArguments(
148144
loadingUnit: 'root',
@@ -151,5 +147,5 @@ final _pirateAdventureRecordings = Recordings(
151147
),
152148
],
153149
},
154-
instancesForDefinition: {},
150+
instances: {},
155151
);

‎pkgs/hooks_runner/test_data/pirate_technology/hook/link.dart‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ Future<Recordings> _loadRecordings(Uri file) async {
4949

5050
Set<String> _extractUsedTechnologies(Recordings recordings) {
5151
final usedTechnologies = <String>{};
52-
for (final def in recordings.callsForDefinition.keys) {
53-
if (def.identifier.importUri ==
52+
for (final identifier in recordings.calls.keys) {
53+
if (identifier.importUri ==
5454
'package:pirate_technology/src/definitions.dart') {
5555
// Map function name to tech key (simple capitalization)
5656
// e.g. useCannon -> Cannon
57-
final name = def.identifier.name;
57+
final name = identifier.name;
5858
if (name.startsWith('use')) {
5959
usedTechnologies.add(name.substring(3));
6060
}

‎pkgs/record_use/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 0.5.0-wip
22

33
- Completely overhauled the JSON format.
4+
- Completely overhauled the Dart API.
45

56
## 0.4.2
67

‎pkgs/record_use/README.md‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,31 +78,32 @@ import 'package:hooks/hooks.dart';
7878
import 'package:record_use/record_use_internal.dart';
7979
8080
final methodId = Identifier(
81-
uri: 'myfile.dart',
81+
importUri: 'myfile.dart',
8282
name: 'myMethod',
8383
);
8484
8585
final classId = Identifier(
86-
uri: 'myfile.dart',
86+
importUri: 'myfile.dart',
8787
name: 'myClass',
8888
);
8989
9090
void main(List<String> arguments){
91-
link(arguments, (config, output) async {
92-
final usesUri = config.recordedUses;
93-
final usesJson = await File,fromUri(usesUri).readAsString();
94-
final uses = UsageRecord.fromJson(jsonDecode(usesJson));
91+
link(arguments, (input, output) async {
92+
final usesUri = input.recordedUsagesFile;
93+
if (usesUri == null) return;
94+
final usesJson = await File.fromUri(usesUri).readAsString();
95+
final uses = RecordedUsages.fromJson(jsonDecode(usesJson));
9596
96-
final args = uses.argumentsTo(methodId));
97+
final args = uses.constArgumentsFor(methodId);
9798
//[args] is an iterable of arguments, in this case containing "42"
9899
99-
final fields = uses.instancesOf(classId);
100-
//[fields] is an iterable of the fields of the class, in this case
100+
final fields = uses.constantsOf(classId);
101+
//[fields] is an iterable of the constants of the class, in this case
101102
//containing
102-
// {"arguments": "leroyjenkins"}
103-
// {"arguments": 3.14}
104-
// {"arguments": 42}
105-
// {"arguments": true}
103+
// {"metadata": "leroyjenkins"}
104+
// {"metadata": 3.14}
105+
// {"metadata": 42}
106+
// {"metadata": true}
106107
107108
... // Do something with the information, such as tree-shaking native assets
108109
});

‎pkgs/record_use/doc/schema/record_use.schema.json‎

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -393,19 +393,8 @@
393393
"$ref": "#/definitions/Call"
394394
}
395395
},
396-
"definition": {
397-
"type": "object",
398-
"properties": {
399-
"identifier": {
400-
"$ref": "#/definitions/Identifier"
401-
},
402-
"loading_unit": {
403-
"type": "string"
404-
}
405-
},
406-
"required": [
407-
"identifier"
408-
]
396+
"identifier": {
397+
"$ref": "#/definitions/Identifier"
409398
},
410399
"instances": {
411400
"type": "array",
@@ -415,7 +404,7 @@
415404
}
416405
},
417406
"required": [
418-
"definition"
407+
"identifier"
419408
]
420409
}
421410
}

‎pkgs/record_use/lib/record_use_internal.dart‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export 'src/constant.dart'
1515
NullConstant,
1616
StringConstant,
1717
UnsupportedConstant;
18-
export 'src/definition.dart' show Definition;
1918
export 'src/identifier.dart' show Identifier;
2019
export 'src/metadata.dart' show Metadata;
2120
export 'src/record_use.dart' show RecordedUsages;

‎pkgs/record_use/lib/src/definition.dart‎

Lines changed: 0 additions & 82 deletions
This file was deleted.

‎pkgs/record_use/lib/src/identifier.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import 'syntax.g.dart';
1717
/// method or field). This is optional, as not all elements have parents (e.g.
1818
/// top-level functions).
1919
/// - `name`: The name of the element itself.
20+
// TODO(https://github.com/dart-lang/native/issues/2888): Rename to Definition.
2021
class Identifier {
2122
/// The URI of the library where the element is defined.
2223
///

0 commit comments

Comments
 (0)