dart-sdk/pkg/kernel/test/type_hashcode_quality.dart
Johnni Winther 4ff04f641b [kernel] Add always_declare_return_types lint
Change-Id: Icdcc648f42393b28daf648f752e3b4d61f51aa10
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/212043
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
2021-09-01 16:50:27 +00:00

50 lines
1.4 KiB
Dart

// Copyright (c) 2016, 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 'package:kernel/kernel.dart';
import 'dart:io';
String usage = '''
Usage: type_hashcode_quality FILE.dill
Counts the number of hash collisions between DartTypes in the given file.
''';
void main(List<String> args) {
if (args.length == 0) {
print(usage);
exit(1);
}
Component component = loadComponentFromBinary(args[0]);
var visitor = new DartTypeCollector();
component.accept(visitor);
print('''
Types: ${visitor.numberOfTypes}
Collisions: ${visitor.numberOfCollisions}''');
}
class DartTypeCollector extends RecursiveVisitor {
final Set<DartType> seenTypes = new Set<DartType>();
final Map<int, DartType> table = <int, DartType>{};
int numberOfCollisions = 0;
int numberOfTypes = 0;
@override
void defaultDartType(DartType node) {
if (!seenTypes.add(node)) return;
++numberOfTypes;
int hash = node.hashCode;
if (hash == 0) {
print('Type has a hash code of zero: $node');
}
DartType? existing = table[hash];
if (existing == null) {
table[hash] = node;
} else if (existing != node) {
print('Collision between $existing and $node');
++numberOfCollisions;
}
}
}