Promises/Deferred/AE.pm untested

filter: Statementstmt 0.0% 0 / 37 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 / 60 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::AE;
2 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: An implementation of Promises in Perl
4 $Promises::Deferred::AE::VERSION = '1.05';
5 use strict;
6 use warnings;
7  
8 use AE;
9  
10 use parent 'Promises::Deferred';
11  
12 # Before the pipe-based approach used below, there was an AE::postpone-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::AE
17 # Benchmark: running one, two for at least 10 CPU seconds...
18 # one: 44 wallclock secs @ 3083.99/s (n=31210)
19 # two: 29 wallclock secs @ 1723.66/s (n=17340)
20  
21 # New approach:
22 # Backend: Promises::Deferred::AE
23 # Benchmark: running one, two for at least 10 CPU seconds...
24 # one: 11 wallclock secs @ 10457.90/s (n=108553)
25 # two: 11 wallclock secs @ 3878.69/s (n=40959)
26  
27  
28 my ($socket_pid, $socket_send, $socket_recv, $socket_io,
29 $read_buf, @io_callbacks);
30  
31 sub _do_callbacks {
32 my @cbs = @io_callbacks;
33 @io_callbacks = ();
34 sysread $socket_recv, $read_buf, 16;
35 for my $cb_grp (@cbs) {
36 my ($result, $cbs) = @$cb_grp;
37 my @r = @$result;
38 $_->(@r) for @$cbs;
39 }
40 }
41  
42 sub _notify_backend {
43 if (! $socket_pid || $socket_pid != $$) {
44 $socket_pid = $$;
45 close($socket_send) if defined $socket_send;
46 close($socket_recv) if defined $socket_recv;
47 pipe($socket_recv, $socket_send);
48 $socket_io = AE::io($socket_recv, 0, \&_do_callbacks);
49 }
50  
51 # skip signalling when there are callbacks already waiting
52 if (not @io_callbacks) {
53 syswrite $socket_send, ' ';
54 }
55 push @io_callbacks, [ $_[2], $_[1] ];
56 }
57  
58 sub _timeout {
59 my ( $self, $timeout, $callback ) = @_;
60  
61 my $id = AE::timer $timeout, 0, $callback;
62
63 return sub { undef $id };
64 }
65  
66 1;
67  
68 __END__