Profile of base.pm

← Index
NYTProf Performance Profile «line view»
For examples/benchmark4.pl
Run on Thu Aug 28 19:01:43 2014
Reported on Thu Aug 28 19:02:42 2014

Filename | /usr/share/perl/5.18/base.pm
Statements | Executed 56 statements in 851µs

Subroutines
Calls | P | F | Exclusive Time | Inclusive Time | Subroutine
--- | --- | --- | --- | --- | ---
1 | 1 | 1 | 88µs | 101µs | base::::importbase::import
1 | 1 | 1 | 12µs | 23µs | base::::BEGIN@3base::BEGIN@3
1 | 1 | 1 | 6µs | 24µs | base::::BEGIN@4base::BEGIN@4
2 | 1 | 1 | 5µs | 5µs | base::::has_fieldsbase::has_fields
2 | 1 | 1 | 4µs | 4µs | base::::has_attrbase::has_attr
0 | 0 | 0 | 0s | 0s | base::::__ANON__[:47]base::__ANON__[:47]
0 | 0 | 0 | 0s | 0s | base::::__ANON__[:54]base::__ANON__[:54]
0 | 0 | 0 | 0s | 0s | base::::get_attrbase::get_attr
0 | 0 | 0 | 0s | 0s | base::::inherit_fieldsbase::inherit_fields

Call graph for these subroutines as a Graphviz dot language file.

Line | State ments | Time on line | Calls | Time in subs | Code
--- | --- | --- | --- | --- | ---
1 |  |  |  |  | package base;
2 |  |  |  |  |
3 | 2 | 27µs | 2 | 34µs | # spent 23µs (12+11) within base::BEGIN@3 which was called: # once (12µs+11µs) by Socket6::BEGIN@215 at line 3 use strict 'vars'; # spent 23µs making 1 call to base::BEGIN@3 # spent 11µs making 1 call to strict::import
4 | 2 | 704µs | 2 | 41µs | # spent 24µs (6+17) within base::BEGIN@4 which was called: # once (6µs+17µs) by Socket6::BEGIN@215 at line 4 use vars qw($VERSION); # spent 24µs making 1 call to base::BEGIN@4 # spent 17µs making 1 call to vars::import
5 | 1 | 600ns |  |  | $VERSION = '2.18';
6 | 1 | 11µs |  |  | $VERSION = eval $VERSION; # spent 2µs executing statements in string eval
7 |  |  |  |  |
8 |  |  |  |  | # constant.pm is slow
9 |  |  |  |  | sub SUCCESS () { 1 }
10 |  |  |  |  |
11 |  |  |  |  | sub PUBLIC () { 2**0 }
12 |  |  |  |  | sub PRIVATE () { 2**1 }
13 |  |  |  |  | sub INHERITED () { 2**2 }
14 |  |  |  |  | sub PROTECTED () { 2**3 }
15 |  |  |  |  |
16 | 1 | 600ns |  |  | my $Fattr = \%fields::attr;
17 |  |  |  |  |
18 |  |  |  |  | # spent 5µs within base::has_fields which was called 2 times, avg 3µs/call: # 2 times (5µs+0s) by base::import at line 100, avg 3µs/call sub has_fields {
19 | 2 | 900ns |  |  | my($base) = shift;
20 | 2 | 2µs |  |  | my $fglob = ${"$base\::"}{FIELDS};
21 | 2 | 7µs |  |  | return( ($fglob && 'GLOB' eq ref($fglob) && *$fglob{HASH}) ? 1 : 0 );
22 |  |  |  |  | }
23 |  |  |  |  |
24 |  |  |  |  | # spent 4µs within base::has_attr which was called 2 times, avg 2µs/call: # 2 times (4µs+0s) by base::import at line 100, avg 2µs/call sub has_attr {
25 | 2 | 500ns |  |  | my($proto) = shift;
26 | 2 | 500ns |  |  | my($class) = ref $proto || $proto;
27 | 2 | 5µs |  |  | return exists $Fattr->{$class};
28 |  |  |  |  | }
29 |  |  |  |  |
30 |  |  |  |  | sub get_attr {
31 |  |  |  |  | $Fattr->{$_[0]} = [1] unless $Fattr->{$_[0]};
32 |  |  |  |  | return $Fattr->{$_[0]};
33 |  |  |  |  | }
34 |  |  |  |  |
35 | 1 | 1µs |  |  | if ($] < 5.009) {
36 |  |  |  |  | *get_fields = sub {
37 |  |  |  |  | # Shut up a possible typo warning.
38 |  |  |  |  | () = \%{$_[0].'::FIELDS'};
39 |  |  |  |  | my $f = \%{$_[0].'::FIELDS'};
40 |  |  |  |  |
41 |  |  |  |  | # should be centralized in fields? perhaps
42 |  |  |  |  | # fields::mk_FIELDS_be_OK. Peh. As long as %{ $package . '::FIELDS' }
43 |  |  |  |  | # is used here anyway, it doesn't matter.
44 |  |  |  |  | bless $f, 'pseudohash' if (ref($f) ne 'pseudohash');
45 |  |  |  |  |
46 |  |  |  |  | return $f;
47 |  |  |  |  | }
48 |  |  |  |  | }
49 |  |  |  |  | else {
50 |  |  |  |  | *get_fields = sub {
51 |  |  |  |  | # Shut up a possible typo warning.
52 |  |  |  |  | () = \%{$_[0].'::FIELDS'};
53 |  |  |  |  | return \%{$_[0].'::FIELDS'};
54 |  |  |  |  | }
55 | 1 | 3µs |  |  | }
56 |  |  |  |  |
57 |  |  |  |  | # spent 101µs (88+13) within base::import which was called: # once (88µs+13µs) by Socket6::BEGIN@215 at line 215 of Socket6.pm sub import {
58 | 1 | 500ns |  |  | my $class = shift;
59 |  |  |  |  |
60 | 1 | 300ns |  |  | return SUCCESS unless @_;
61 |  |  |  |  |
62 |  |  |  |  | # List of base classes from which we will inherit %FIELDS.
63 | 1 | 100ns |  |  | my $fields_base;
64 |  |  |  |  |
65 | 1 | 500ns |  |  | my $inheritor = caller(0);
66 |  |  |  |  |
67 | 1 | 100ns |  |  | my @bases;
68 | 1 | 900ns |  |  | foreach my $base (@_) {
69 | 2 | 500ns |  |  | if ( $inheritor eq $base ) {
70 |  |  |  |  | warn "Class '$inheritor' tried to inherit from itself\n";
71 |  |  |  |  | }
72 |  |  |  |  |
73 | 2 | 21µs | 3 | 4µs | next if grep $_->isa($base), ($inheritor, @bases); # spent 4µs making 3 calls to UNIVERSAL::isa, avg 1µs/call
74 |  |  |  |  |
75 |  |  |  |  | # Following blocks help isolate $SIG{__DIE__} changes
76 |  |  |  |  | {
77 | 4 | 600ns |  |  | my $sigdie;
78 |  |  |  |  | {
79 | 4 | 3µs |  |  | local $SIG{__DIE__};
80 | 2 | 28µs |  |  | eval "require $base"; # spent 2µs executing statements in string eval # spent 2µs executing statements in string eval
81 |  |  |  |  | # Only ignore "Can't locate" errors from our eval require.
82 |  |  |  |  | # Other fatal errors (syntax etc) must be reported.
83 | 2 | 300ns |  |  | die if $@ && $@ !~ /^Can't locate .*? at \(eval /;
84 | 2 | 4µs |  |  | unless (%{"$base\::"}) {
85 |  |  |  |  | require Carp;
86 |  |  |  |  | local $" = " ";
87 |  |  |  |  | Carp::croak(<<ERROR);
88 |  |  |  |  | Base class package "$base" is empty.
89 |  |  |  |  | (Perhaps you need to 'use' the module which defines that package first,
90 |  |  |  |  | or make that module available in \@INC (\@INC contains: @INC).
91 |  |  |  |  | ERROR
92 |  |  |  |  | }
93 | 2 | 5µs |  |  | $sigdie = $SIG{__DIE__} || undef;
94 |  |  |  |  | }
95 |  |  |  |  | # Make sure a global $SIG{__DIE__} makes it out of the localization.
96 | 2 | 700ns |  |  | $SIG{__DIE__} = $sigdie if defined $sigdie;
97 |  |  |  |  | }
98 | 2 | 1µs |  |  | push @bases, $base;
99 |  |  |  |  |
100 | 2 | 6µs | 4 | 9µs | if ( has_fields($base) || has_attr($base) ) { # spent 5µs making 2 calls to base::has_fields, avg 3µs/call # spent 4µs making 2 calls to base::has_attr, avg 2µs/call
101 |  |  |  |  | # No multiple fields inheritance *suck*
102 |  |  |  |  | if ($fields_base) {
103 |  |  |  |  | require Carp;
104 |  |  |  |  | Carp::croak("Can't multiply inherit fields");
105 |  |  |  |  | } else {
106 |  |  |  |  | $fields_base = $base;
107 |  |  |  |  | }
108 |  |  |  |  | }
109 |  |  |  |  | }
110 |  |  |  |  | # Save this until the end so it's all or nothing if the above loop croaks.
111 | 1 | 7µs |  |  | push @{"$inheritor\::ISA"}, @bases;
112 |  |  |  |  |
113 | 1 | 4µs |  |  | if( defined $fields_base ) {
114 |  |  |  |  | inherit_fields($inheritor, $fields_base);
115 |  |  |  |  | }
116 |  |  |  |  | }
117 |  |  |  |  |
118 |  |  |  |  | sub inherit_fields {
119 |  |  |  |  | my($derived, $base) = @_;
120 |  |  |  |  |
121 |  |  |  |  | return SUCCESS unless $base;
122 |  |  |  |  |
123 |  |  |  |  | my $battr = get_attr($base);
124 |  |  |  |  | my $dattr = get_attr($derived);
125 |  |  |  |  | my $dfields = get_fields($derived);
126 |  |  |  |  | my $bfields = get_fields($base);
127 |  |  |  |  |
128 |  |  |  |  | $dattr->[0] = @$battr;
129 |  |  |  |  |
130 |  |  |  |  | if( keys %$dfields ) {
131 |  |  |  |  | warn <<"END";
132 |  |  |  |  | $derived is inheriting from $base but already has its own fields!
133 |  |  |  |  | This will cause problems. Be sure you use base BEFORE declaring fields.
134 |  |  |  |  | END
135 |  |  |  |  |
136 |  |  |  |  | }
137 |  |  |  |  |
138 |  |  |  |  | # Iterate through the base's fields adding all the non-private
139 |  |  |  |  | # ones to the derived class. Hang on to the original attribute
140 |  |  |  |  | # (Public, Private, etc...) and add Inherited.
141 |  |  |  |  | # This is all too complicated to do efficiently with add_fields().
142 |  |  |  |  | while (my($k,$v) = each %$bfields) {
143 |  |  |  |  | my $fno;
144 |  |  |  |  | if ($fno = $dfields->{$k} and $fno != $v) {
145 |  |  |  |  | require Carp;
146 |  |  |  |  | Carp::croak ("Inherited fields can't override existing fields");
147 |  |  |  |  | }
148 |  |  |  |  |
149 |  |  |  |  | if( $battr->[$v] & PRIVATE ) {
150 |  |  |  |  | $dattr->[$v] = PRIVATE | INHERITED;
151 |  |  |  |  | }
152 |  |  |  |  | else {
153 |  |  |  |  | $dattr->[$v] = INHERITED | $battr->[$v];
154 |  |  |  |  | $dfields->{$k} = $v;
155 |  |  |  |  | }
156 |  |  |  |  | }
157 |  |  |  |  |
158 |  |  |  |  | foreach my $idx (1..$#{$battr}) {
159 |  |  |  |  | next if defined $dattr->[$idx];
160 |  |  |  |  | $dattr->[$idx] = $battr->[$idx] & INHERITED;
161 |  |  |  |  | }
162 |  |  |  |  | }
163 |  |  |  |  |
164 | 1 | 4µs |  |  | 1;
165 |  |  |  |  |
166 |  |  |  |  | __END__