dart-sdk/benchmarks/IsolateRegExp/dart/IsolateRegExp.dart
Eric Seidel 4c20cedd30 Fix analysis issues in benchmarks/ directory
Now that I'm able to open the entire SDK in VSC, I'm fixing some
of the analysis issues in various files (carefully) without changing
their meaning.

In this case, I removed unnecessary imports from benchmarks.
In regexp_benchmark I ignored one warning which likely would
have changed the behavior of the code.

BUG=https://github.com/dart-lang/sdk/issues/52419

Change-Id: I9a195a4e45121313bd9f065f2579a165c3fec05b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/303901
Auto-Submit: Eric Seidel <eric@shorebird.dev>
Reviewed-by: William Hesse <whesse@google.com>
Commit-Queue: William Hesse <whesse@google.com>
2023-05-17 09:14:50 +00:00

95 lines
2.3 KiB
Dart

// Copyright (c) 2022, 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.
//
// Measures performance of RegExp reuse between isolates.
import 'dart:async';
import 'dart:isolate';
import 'package:benchmark_harness/benchmark_harness.dart';
class SendReceiveRegExp extends AsyncBenchmarkBase {
SendReceiveRegExp(String name, this.re) : super(name);
@override
Future<void> run() async {
await helper.run(re);
}
@override
Future<void> setup() async {
helper = SendReceiveHelper();
await helper.setup();
}
@override
Future<void> teardown() async {
await helper.finalize();
}
late SendReceiveHelper helper;
RegExp re;
}
class SendReceiveHelper {
SendReceiveHelper();
Future<void> setup() async {
final port = ReceivePort();
inbox = StreamIterator<dynamic>(port);
workerExitedPort = ReceivePort();
await Isolate.spawn(isolate, port.sendPort,
onExit: workerExitedPort.sendPort);
await inbox.moveNext();
outbox = inbox.current;
}
Future<void> finalize() async {
outbox.send(null);
await workerExitedPort.first;
workerExitedPort.close();
await inbox.cancel();
}
// Send regexp to worker, get one back, repeat few times.
Future<void> run(RegExp re) async {
for (int i = 0; i < 5; i++) {
outbox.send(re);
await inbox.moveNext();
re = inbox.current;
}
}
late StreamIterator<dynamic> inbox;
late SendPort outbox;
late ReceivePort workerExitedPort;
}
Future<void> isolate(SendPort sendPort) async {
final port = ReceivePort();
final inbox = StreamIterator<dynamic>(port);
sendPort.send(port.sendPort);
while (true) {
await inbox.moveNext();
final received = inbox.current;
if (received == null) {
break;
}
// use RegExp to ensure it is compiled
final RegExp re = received as RegExp;
re.firstMatch('h' * 1000);
// send the RegExp
sendPort.send(re);
}
port.close();
}
Future<void> main() async {
await SendReceiveRegExp('IsolateRegExp.MatchFast', RegExp('h?h')).report();
await SendReceiveRegExp('IsolateRegExp.MatchSlow',
RegExp(r'(?<=\W|\b|^)(a.? b c.?) ?(\(.*\))?$'))
.report();
}