dart-sdk/benchmarks/MapLookup/generate_maps.dart
Daco Harkes b0bfee6627 [benchmarks/vm] Add benchmarks for #45908
The implementation of const map literals is based on linear lookups in
an array containing key/value pairs instead of a hash table.

Bug: https://github.com/dart-lang/sdk/issues/45908

Golem CL:
https://chrome-internal-review.googlesource.com/c/golem/+/3852562

Change-Id: If0a980339ee1342c41f9388e0bd7b3615a4ef4e3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/200188
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Jonas Termansen <sortie@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2021-05-27 07:54:14 +00:00

42 lines
1.5 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:io';
void main() {
final buffer = StringBuffer();
buffer.write('''
// 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.
''');
generateMap(buffer, 'const1', 1, isConst: true);
generateMap(buffer, 'final1', 1, isConst: false);
generateMap(buffer, 'const5', 5, isConst: true);
generateMap(buffer, 'final5', 5, isConst: false);
generateMap(buffer, 'const10', 10, isConst: true);
generateMap(buffer, 'final10', 10, isConst: false);
generateMap(buffer, 'const100', 100, isConst: true);
generateMap(buffer, 'final100', 100, isConst: false);
for (final folder in ['dart', 'dart2']) {
final path = Platform.script.resolve('$folder/maps.dart').toFilePath();
File(path).writeAsStringSync(buffer.toString());
Process.runSync(Platform.executable, ['format', path]);
print('Generated $path.');
}
}
void generateMap(StringBuffer buffer, String name, int mapSize,
{bool isConst = true}) {
final constOrFinal = isConst ? 'const' : 'final';
buffer.write('$constOrFinal $name = <String, String>{');
for (int i = 0; i < mapSize; i++) {
buffer.write("'$i': '${i + 1}',");
}
buffer.write('};');
buffer.write('\n\n');
}