Promises/Deferred/EV.pm untested

filter: Statementstmt 0.0% 0 / 38 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 61 CC 5 SCAR 26.9
CC
5
Cov
0%
_notify_backend
30.0
_do_callbacks
2.0
_timeout
2.0
CRAP 14.7
1 package Promises::Deferred::EV;
2 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: An implementation of Promises in Perl
4 $Promises::Deferred::EV::VERSION = '1.05';
5 use strict;
6 use warnings;
7  
8 use EV;
9  
10 use parent 'Promises::Deferred';
11  
12 # Before the pipe-based approach used below, there was an EV::timer-based
13 # approach for _notify_backend. The current code is much more performant:
14  
15 # Original code (on a laptop on battery power):
16 # Backend: Promises::Deferred::EV
17 # Benchmark: running one, two for at least 10 CPU seconds...
18 # Benchmark: running one, two for at least 10 CPU seconds...
19 # one: 67 wallclock secs @ 1755.16/s (n=17692)
20 # two: 53 wallclock secs @ 770.03/s (n=7785)
21  
22 # New approach:
23 # Backend: Promises::Deferred::EV
24 # Benchmark: running one, two for at least 10 CPU seconds...
25 # one: 10 wallclock secs @ 10949.19/s (n=115076)
26 # two: 10 wallclock secs @ 3964.58/s (n=41747)
27  
28  
29 my ($socket_pid, $socket_send, $socket_recv, $socket_io,
30 $read_buf, @io_callbacks);
31  
32 sub _do_callbacks {
33 my @cbs = @io_callbacks;
34 @io_callbacks = ();
35 sysread $socket_recv, $read_buf, 16;
36 for my $cb_grp (@cbs) {
37 my ($result, $cbs) = @$cb_grp;
38 my @r = @$result;
39 $_->(@r) for @$cbs;
40 }
41 }
42  
43 sub _notify_backend {
44 if (! $socket_pid || $socket_pid != $$) {
45 $socket_pid = $$;
46 close($socket_send) if defined $socket_send;
47 close($socket_recv) if defined $socket_recv;
48 pipe($socket_recv, $socket_send);
49 $socket_io = EV::io($socket_recv, EV::READ, \&_do_callbacks);
50 $socket_io->keepalive(0);
51 }
52  
53 # skip signalling when there are callbacks already waiting
54 if (not @io_callbacks) {
55 syswrite $socket_send, ' ';
56 }
57 push @io_callbacks, [ $_[2], $_[1] ];
58 }
59  
60 sub _timeout {
61 my ( $self, $timeout, $callback ) = @_;
62  
63 my $id = EV::timer $timeout, 0, $callback;
64  
65 return sub { undef $id };
66 }
67  
68 1;
69  
70 __END__