Use streams because this is literally just a stream recreation#1
Use streams because this is literally just a stream recreation#1miyoyo wants to merge 4 commits intoxenoken:masterfrom miyoyo:master
Conversation
|
Hey @miyoyo! Thank you very much for your interest and for your pull request. I was already working on a new stream-based implementation, and now it is available in the master branch. |
Boosts speed significantly
Vastly faster than wrapping the callback, and, since we create the streamcontroller right above, the `as` is safe.
|
Thanks, I'll still put my approach forward for a few reasons:
static void send<T>(T message) {
_mainstream.add(message);
}vs static bool send<T>(T message) {
final target = _instances[T];
target?.add(message);
return target != null;
}
static fetch<T>(Function(T arg) func) {
if (!_streams.containsKey(T)) {
_streams[T] = _mainstream.stream
.asBroadcastStream() // This creates a new stream controller
.where((event) => event is T) // This too
.cast<T>(); // So does this
}
_streams[T].listen((event) {
func(event);
});
}
This is the test rig used for both versions final s = Stopwatch();
final loops = 1000000;
var target = 0;
final complete = Completer<void>();
s.start();
A.fetch((int i) {
target++;
if (target >= loops) {
complete.complete();
}
});
for (var i = 0; i < loops; i++) {
A.send(0);
}
await complete.future;
s.stop();
print("A done: ${s.elapsedMicroseconds}, at ${s.elapsedMicroseconds / loops} per loop on average");Where A is this pull request, and B is the current master. |
|
Thanks for your insights. Based on your feedback, I made some edits to the code. |
Since streams are backed by the VM, they're even faster.