Skip to content

Commit c478dfe

Browse files
authored
[record_use] Canonicalize and sort references (dart-lang#3216)
Deduplicates identical references from the same loading unit. Also sorts the references. Closes: * dart-lang#3092 AI transparency: * Generated with Gemini CLI. Architecture mine. Implementation AI. Iterated on with follow up prompts. Code has been reviewed by me.
1 parent ebcfec5 commit c478dfe

9 files changed

Lines changed: 362 additions & 163 deletions

File tree

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -890,9 +890,7 @@ final class InstanceConstant extends Constant {
890890

891891
@override
892892
int _compareToSameType(InstanceConstant other) {
893-
final definitionCompare = definition.toString().compareTo(
894-
other.definition.toString(),
895-
);
893+
final definitionCompare = definition.compareTo(other.definition);
896894
if (definitionCompare != 0) return definitionCompare;
897895
final lengthCompare = fields.length.compareTo(other.fields.length);
898896
if (lengthCompare != 0) return lengthCompare;
@@ -1056,9 +1054,7 @@ final class EnumConstant extends Constant {
10561054

10571055
@override
10581056
int _compareToSameType(EnumConstant other) {
1059-
final definitionCompare = definition.toString().compareTo(
1060-
other.definition.toString(),
1061-
);
1057+
final definitionCompare = definition.compareTo(other.definition);
10621058
if (definitionCompare != 0) return definitionCompare;
10631059
final indexCompare = index.compareTo(other.index);
10641060
if (indexCompare != 0) return indexCompare;

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ class Definition {
9696
int get hashCode =>
9797
cacheHashCode(() => Object.hash(library, Object.hashAll(path)));
9898

99+
// This should align with [toString] ordering.
100+
int _compareTo(Definition other) => toString().compareTo(other.toString());
101+
99102
/// Returns a URI representation of this definition.
100103
///
101104
/// The [library] is the base URI and the [path] is the fragment.
@@ -147,8 +150,7 @@ class Name {
147150
name,
148151
kind: kind,
149152
disambiguators: Set.from(
150-
disambiguators.toList()
151-
..sort((a, b) => a.toString().compareTo(b.toString())),
153+
disambiguators.toList()..sort((a, b) => a.compareTo(b)),
152154
),
153155
);
154156
}
@@ -182,8 +184,7 @@ class Name {
182184
}
183185
buffer.write(name);
184186
if (disambiguators.isNotEmpty) {
185-
final sorted = disambiguators.toList()
186-
..sort((a, b) => a.toString().compareTo(b.toString()));
187+
final sorted = disambiguators.toList()..sort((a, b) => a.compareTo(b));
187188
for (final disambiguator in sorted) {
188189
buffer.write('@$disambiguator');
189190
}
@@ -237,10 +238,20 @@ final class DefinitionKind {
237238
@override
238239
int get hashCode => _name.hashCode;
239240

241+
int _compareTo(DefinitionKind other) => _name.compareTo(other._name);
242+
240243
@override
241244
String toString() => _name;
242245
}
243246

247+
/// Package private (protected) methods for [DefinitionKind].
248+
///
249+
/// This avoids bloating the public API and public API docs and prevents
250+
/// internal types from leaking from the API.
251+
extension DefinitionKindProtected on DefinitionKind {
252+
int compareTo(DefinitionKind other) => _compareTo(other);
253+
}
254+
244255
/// Extra metadata to disambiguate between elements that might have the same
245256
/// name and kind.
246257
///
@@ -282,10 +293,20 @@ final class DefinitionDisambiguator {
282293
@override
283294
int get hashCode => _name.hashCode;
284295

296+
int _compareTo(DefinitionDisambiguator other) => _name.compareTo(other._name);
297+
285298
@override
286299
String toString() => _name;
287300
}
288301

302+
/// Package private (protected) methods for [DefinitionDisambiguator].
303+
///
304+
/// This avoids bloating the public API and public API docs and prevents
305+
/// internal types from leaking from the API.
306+
extension DefinitionDisambiguatorProtected on DefinitionDisambiguator {
307+
int compareTo(DefinitionDisambiguator other) => _compareTo(other);
308+
}
309+
289310
/// Package private (protected) methods for [Definition].
290311
///
291312
/// This avoids bloating the public API and public API docs and prevents
@@ -296,6 +317,8 @@ extension DefinitionProtected on Definition {
296317
Definition canonicalizeChildren(CanonicalizationContext context) =>
297318
_canonicalizeChildren(context);
298319

320+
int compareTo(Definition other) => _compareTo(other);
321+
299322
static Definition fromSyntax(DefinitionSyntax syntax) =>
300323
Definition._fromSyntax(syntax);
301324
}

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

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -329,28 +329,30 @@ Error: $e
329329
);
330330
}
331331

332-
Recordings _canonicalizeChildren(CanonicalizationContext context) {
333-
final newCalls = <Definition, List<CallReference>>{};
334-
for (final entry in calls.entries) {
335-
final definition = context.canonicalizeDefinition(entry.key);
336-
final references = [
337-
for (final r in entry.value) r.canonicalizeChildren(context),
338-
];
339-
newCalls.putIfAbsent(definition, () => []).addAll(references);
340-
}
341-
final newInstances = <Definition, List<InstanceReference>>{};
342-
for (final entry in instances.entries) {
332+
Recordings _canonicalizeChildren(CanonicalizationContext context) =>
333+
Recordings(
334+
metadata: metadata,
335+
calls: _canonicalizeReferences(context, calls),
336+
instances: _canonicalizeReferences(context, instances),
337+
);
338+
339+
Map<Definition, List<R>> _canonicalizeReferences<R extends Reference>(
340+
CanonicalizationContext context,
341+
Map<Definition, List<R>> references,
342+
) {
343+
final map = <Definition, Set<R>>{};
344+
for (final entry in references.entries) {
343345
final definition = context.canonicalizeDefinition(entry.key);
344-
final references = [
345-
for (final r in entry.value) r.canonicalizeChildren(context),
346-
];
347-
newInstances.putIfAbsent(definition, () => []).addAll(references);
346+
final set = map.putIfAbsent(definition, () => {});
347+
for (final reference in entry.value) {
348+
set.add(reference.canonicalizeChildren(context) as R);
349+
}
348350
}
349-
return Recordings(
350-
metadata: metadata,
351-
calls: newCalls,
352-
instances: newInstances,
353-
);
351+
final sortedKeys = map.keys.toList()..sort((a, b) => a.compareTo(b));
352+
return <Definition, List<R>>{
353+
for (final key in sortedKeys)
354+
key: map[key]!.toList()..sort((a, b) => a.compareTo(b)),
355+
};
354356
}
355357

356358
static LoadingUnitDeserializationContext _deserializeLoadingUnits(
@@ -414,7 +416,7 @@ Error: $e
414416
final sortedLoadingUnits = canonContext.loadingUnits.toList()
415417
..sort((a, b) => a.name.compareTo(b.name));
416418
final sortedDefinitions = canonContext.definitions.toList()
417-
..sort((a, b) => a.toString().compareTo(b.toString()));
419+
..sort((a, b) => a.compareTo(b));
418420
final sortedConstants = canonContext.constants.toList()
419421
..sort((a, b) => a.compareTo(b));
420422

@@ -425,28 +427,20 @@ Error: $e
425427
);
426428

427429
final callRecordings = <CallRecordingSyntax>[];
428-
for (final definition in context.definitions.keys) {
429-
final callsForDefinition = canon.calls[definition];
430-
if (callsForDefinition == null || callsForDefinition.isEmpty) continue;
430+
for (final entry in canon.calls.entries) {
431431
callRecordings.add(
432432
CallRecordingSyntax(
433-
definitionIndex: context.definitions[definition]!,
434-
uses: callsForDefinition
435-
.map((call) => call.toSyntax(context))
436-
.toList(),
433+
definitionIndex: context.definitions[entry.key]!,
434+
uses: entry.value.map((call) => call.toSyntax(context)).toList(),
437435
),
438436
);
439437
}
440438
final instanceRecordings = <InstanceRecordingSyntax>[];
441-
for (final definition in context.definitions.keys) {
442-
final instancesForDefinition = canon.instances[definition];
443-
if (instancesForDefinition == null || instancesForDefinition.isEmpty) {
444-
continue;
445-
}
439+
for (final entry in canon.instances.entries) {
446440
instanceRecordings.add(
447441
InstanceRecordingSyntax(
448-
definitionIndex: context.definitions[definition]!,
449-
uses: instancesForDefinition
442+
definitionIndex: context.definitions[entry.key]!,
443+
uses: entry.value
450444
.map((instance) => instance.toSyntax(context))
451445
.toList(),
452446
),
@@ -735,6 +729,11 @@ Error: $e
735729
}
736730
}
737731

732+
extension RecordingsProtected on Recordings {
733+
Recordings canonicalizeChildren(CanonicalizationContext context) =>
734+
_canonicalizeChildren(context);
735+
}
736+
738737
extension MapifyIterableExtension<T> on Iterable<T> {
739738
/// Transform list to map, faster than using list.indexOf
740739
Map<T, int> get asMapToIndices {

0 commit comments

Comments
 (0)