| 1 | package Promises::Deferred::IO::Async; | ||
| 2 | our $AUTHORITY = 'cpan:YANICK'; | ||
| 3 | # ABSTRACT: IO::Async implementation of Promises | ||
| 4 | $Promises::Deferred::IO::Async::VERSION = '1.05'; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use IO::Async::Loop; | ||
| 9 | use IO::Async::Timer::Countdown; | ||
| 10 | |||
| 11 | use parent 'Promises::Deferred'; | ||
| 12 | |||
| 13 | our $Loop = IO::Async::Loop->new; | ||
| 14 | |||
| 15 | # Before the 'later'-based approach used below, there was an | ||
| 16 | # Async::IO::Timer::Countdown based approach for _notify_backend. | ||
| 17 | # The current code is much more performant: | ||
| 18 | |||
| 19 | # Original code | ||
| 20 | # Backend: Promises::Deferred::IO::Async | ||
| 21 | # Benchmark: running one, two for at least 10 CPU seconds... | ||
| 22 | # one: 41 wallclock secs @ 815.48/s (n=8954) | ||
| 23 | # two: 31 wallclock secs @ 373.39/s (n=3760) | ||
| 24 | |||
| 25 | # New approach: | ||
| 26 | # Backend: Promises::Deferred::IO::Async | ||
| 27 | # Benchmark: running one, two for at least 10 CPU seconds... | ||
| 28 | # one: 11 wallclock secs @ 8436.69/s (n=88754) | ||
| 29 | # two: 10 wallclock secs @ 3150.85/s (n=33273) | ||
| 30 | |||
| 31 | |||
| 32 | sub _notify_backend { | ||
| 33 | my ( $self, $callbacks, $result ) = @_; | ||
| 34 | $Loop->later(sub { $_->(@$result) for @$callbacks; }); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub _timeout { | ||
| 38 | my ( $self, $timeout, $callback ) = @_; | ||
| 39 | |||
| 40 | my $timer = IO::Async::Timer::Countdown->new( | ||
| 41 | delay => $timeout, | ||
| 42 | on_expire => $callback, | ||
| 43 | ); | ||
| 44 | |||
| 45 | $Loop->add( $timer->start ); | ||
| 46 | |||
| 47 | return sub { $timer->stop }; | ||
| 48 | } | ||
| 49 | |||
| 50 | 1; | ||
| 51 | |||
| 52 | __END__ |