Skip to content

Commit ff09699

Browse files
kevmooCommit Queue
authored andcommitted
[dart2js_info] deduplicate holdings in JSON serialization
Reduces file size and makes scanning easier Change-Id: I50af1bcc58670c25b77af4310c7455952778198e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/419781 Reviewed-by: Srujan Gaddam <[email protected]> Auto-Submit: Kevin Moore <[email protected]> Commit-Queue: Kevin Moore <[email protected]>
1 parent de08455 commit ff09699

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

pkg/dart2js_info/lib/json_info_codec.dart

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -440,29 +440,20 @@ class AllInfoToJsonConverter extends Converter<AllInfo, Map>
440440
};
441441
}
442442

443-
Map visitDependencyInfo(DependencyInfo info) => {
444-
'id': idFor(info.target).serializedId,
445-
if (info.mask != null) 'mask': info.mask,
446-
};
443+
Map visitDependencyInfo(DependencyInfo info) =>
444+
_createFromDepInfo(info).toJson();
445+
446+
_InfoClass _createFromDepInfo(DependencyInfo info) =>
447+
_InfoClass._(idFor(info.target).serializedId, info.mask);
447448

448449
Map _visitAllInfoHolding(AllInfo allInfo) {
449-
var map = SplayTreeMap<String, List>(compareNatural);
450+
var map = SplayTreeMap<String, List<Map<String, dynamic>>>(compareNatural);
450451
void helper(CodeInfo info) {
451452
if (info.uses.isEmpty) return;
452453
map[idFor(info).serializedId] =
453-
info.uses.map(visitDependencyInfo).toList()..sort((a, b) {
454-
final value = a['id'].compareTo(b['id']);
455-
if (value != 0) return value;
456-
final aMask = a['mask'] as String?;
457-
final bMask = b['mask'] as String?;
458-
if (aMask == null) {
459-
return bMask == null ? 0 : 1;
460-
}
461-
if (bMask == null) {
462-
return -1;
463-
}
464-
return aMask.compareTo(bMask);
465-
});
454+
SplayTreeSet.of(
455+
info.uses.map(_createFromDepInfo),
456+
).map((e) => e.toJson()).toList();
466457
}
467458

468459
allInfo.functions.forEach(helper);
@@ -675,3 +666,33 @@ class Id {
675666

676667
String get serializedId => '${kindToString(kind)}/$id';
677668
}
669+
670+
/// Represents the serialization information of [DependencyInfo] with equality
671+
/// and comparison implemented.
672+
final class _InfoClass implements Comparable<_InfoClass> {
673+
final String id;
674+
final String? mask;
675+
676+
_InfoClass._(this.id, this.mask);
677+
678+
Map<String, dynamic> toJson() => {'id': id, if (mask != null) 'mask': mask};
679+
680+
@override
681+
int compareTo(_InfoClass other) {
682+
final value = id.compareTo(other.id);
683+
if (value != 0) {
684+
return value;
685+
}
686+
687+
final myMask = mask, otherMask = other.mask;
688+
689+
if (myMask == null) {
690+
return otherMask == null ? 0 : 1;
691+
}
692+
if (otherMask == null) {
693+
return -1;
694+
}
695+
696+
return myMask.compareTo(otherMask);
697+
}
698+
}

0 commit comments

Comments
 (0)