-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use streams because this is literally just a stream recreation #1
base: master
Are you sure you want to change the base?
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.