Filename | /home/ss5/perl5/perlbrew/perls/perl-5.22.0/lib/site_perl/5.22.0/CHI/Util.pm |
Statements | Executed 36 statements in 993µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 2.52ms | 2.93ms | BEGIN@5 | CHI::Util::
1 | 1 | 1 | 552µs | 1.82ms | BEGIN@9 | CHI::Util::
1 | 1 | 1 | 548µs | 901µs | BEGIN@6 | CHI::Util::
1 | 1 | 1 | 338µs | 795µs | BEGIN@10 | CHI::Util::
1 | 1 | 1 | 320µs | 441µs | BEGIN@8 | CHI::Util::
1 | 1 | 1 | 244µs | 740µs | BEGIN@7 | CHI::Util::
1 | 1 | 1 | 7µs | 37µs | BEGIN@144 | CHI::Util::
1 | 1 | 1 | 7µs | 18µs | BEGIN@4 | CHI::Util::
1 | 1 | 1 | 7µs | 27µs | BEGIN@11 | CHI::Util::
1 | 1 | 1 | 6µs | 26µs | BEGIN@3 | CHI::Util::
1 | 1 | 1 | 5µs | 6µs | BEGIN@12 | CHI::Util::
1 | 1 | 1 | 4µs | 35µs | BEGIN@14 | CHI::Util::
1 | 1 | 1 | 3µs | 6µs | BEGIN@13 | CHI::Util::
0 | 0 | 0 | 0s | 0s | __ANON__[:46] | CHI::Util::
0 | 0 | 0 | 0s | 0s | __ANON__[:54] | CHI::Util::
0 | 0 | 0 | 0s | 0s | can_load | CHI::Util::
0 | 0 | 0 | 0s | 0s | dump_one_line | CHI::Util::
0 | 0 | 0 | 0s | 0s | fast_catdir | CHI::Util::
0 | 0 | 0 | 0s | 0s | fast_catfile | CHI::Util::
0 | 0 | 0 | 0s | 0s | json_decode | CHI::Util::
0 | 0 | 0 | 0s | 0s | json_encode | CHI::Util::
0 | 0 | 0 | 0s | 0s | parse_memory_size | CHI::Util::
0 | 0 | 0 | 0s | 0s | read_dir | CHI::Util::
0 | 0 | 0 | 0s | 0s | read_file | CHI::Util::
0 | 0 | 0 | 0s | 0s | unique_id | CHI::Util::
0 | 0 | 0 | 0s | 0s | write_file | CHI::Util::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package CHI::Util; | ||||
2 | 1 | 300ns | $CHI::Util::VERSION = '0.60'; | ||
3 | 2 | 18µs | 2 | 45µs | # spent 26µs (6+20) within CHI::Util::BEGIN@3 which was called:
# once (6µs+20µs) by CHI::Stats::BEGIN@3 at line 3 # spent 26µs making 1 call to CHI::Util::BEGIN@3
# spent 20µs making 1 call to Exporter::import |
4 | 2 | 15µs | 2 | 30µs | # spent 18µs (7+12) within CHI::Util::BEGIN@4 which was called:
# once (7µs+12µs) by CHI::Stats::BEGIN@3 at line 4 # spent 18µs making 1 call to CHI::Util::BEGIN@4
# spent 12µs making 1 call to Module::Runtime::import |
5 | 2 | 76µs | 2 | 2.95ms | # spent 2.93ms (2.52+414µs) within CHI::Util::BEGIN@5 which was called:
# once (2.52ms+414µs) by CHI::Stats::BEGIN@3 at line 5 # spent 2.93ms making 1 call to CHI::Util::BEGIN@5
# spent 18µs making 1 call to Exporter::import |
6 | 2 | 58µs | 2 | 924µs | # spent 901µs (548+353) within CHI::Util::BEGIN@6 which was called:
# once (548µs+353µs) by CHI::Stats::BEGIN@3 at line 6 # spent 901µs making 1 call to CHI::Util::BEGIN@6
# spent 23µs making 1 call to Exporter::import |
7 | 2 | 84µs | 2 | 1.07ms | # spent 740µs (244+496) within CHI::Util::BEGIN@7 which was called:
# once (244µs+496µs) by CHI::Stats::BEGIN@3 at line 7 # spent 740µs making 1 call to CHI::Util::BEGIN@7
# spent 334µs making 1 call to Exporter::import |
8 | 2 | 57µs | 2 | 474µs | # spent 441µs (320+121) within CHI::Util::BEGIN@8 which was called:
# once (320µs+121µs) by CHI::Stats::BEGIN@3 at line 8 # spent 441µs making 1 call to CHI::Util::BEGIN@8
# spent 32µs making 1 call to Exporter::import |
9 | 2 | 62µs | 2 | 1.84ms | # spent 1.82ms (552µs+1.27) within CHI::Util::BEGIN@9 which was called:
# once (552µs+1.27ms) by CHI::Stats::BEGIN@3 at line 9 # spent 1.82ms making 1 call to CHI::Util::BEGIN@9
# spent 21µs making 1 call to Exporter::import |
10 | 2 | 68µs | 2 | 807µs | # spent 795µs (338+457) within CHI::Util::BEGIN@10 which was called:
# once (338µs+457µs) by CHI::Stats::BEGIN@3 at line 10 # spent 795µs making 1 call to CHI::Util::BEGIN@10
# spent 12µs making 1 call to Exporter::Lite::import |
11 | 2 | 17µs | 2 | 47µs | # spent 27µs (7+20) within CHI::Util::BEGIN@11 which was called:
# once (7µs+20µs) by CHI::Stats::BEGIN@3 at line 11 # spent 27µs making 1 call to CHI::Util::BEGIN@11
# spent 20µs making 1 call to Exporter::import |
12 | 2 | 10µs | 2 | 7µs | # spent 6µs (5+1) within CHI::Util::BEGIN@12 which was called:
# once (5µs+1µs) by CHI::Stats::BEGIN@3 at line 12 # spent 6µs making 1 call to CHI::Util::BEGIN@12
# spent 1µs making 1 call to strict::import |
13 | 2 | 12µs | 2 | 9µs | # spent 6µs (3+3) within CHI::Util::BEGIN@13 which was called:
# once (3µs+3µs) by CHI::Stats::BEGIN@3 at line 13 # spent 6µs making 1 call to CHI::Util::BEGIN@13
# spent 3µs making 1 call to warnings::import |
14 | 2 | 348µs | 2 | 65µs | # spent 35µs (4+30) within CHI::Util::BEGIN@14 which was called:
# once (4µs+30µs) by CHI::Stats::BEGIN@3 at line 14 # spent 35µs making 1 call to CHI::Util::BEGIN@14
# spent 30µs making 1 call to base::import |
15 | |||||
16 | 1 | 1µs | our @EXPORT_OK = qw( | ||
17 | can_load | ||||
18 | dump_one_line | ||||
19 | fast_catdir | ||||
20 | fast_catfile | ||||
21 | has_moose_class | ||||
22 | json_decode | ||||
23 | json_encode | ||||
24 | parse_duration | ||||
25 | parse_memory_size | ||||
26 | read_file | ||||
27 | read_dir | ||||
28 | unique_id | ||||
29 | write_file | ||||
30 | ); | ||||
31 | |||||
32 | 1 | 200ns | my $Fetch_Flags = O_RDONLY | O_BINARY; | ||
33 | 1 | 0s | my $Store_Flags = O_WRONLY | O_CREAT | O_BINARY; | ||
34 | |||||
35 | sub can_load { | ||||
36 | |||||
37 | # Load $class_name if possible. Return 1 if successful, 0 if it could not be | ||||
38 | # found, and rethrow load error (other than not found). | ||||
39 | # | ||||
40 | my ($class_name) = @_; | ||||
41 | |||||
42 | my $result; | ||||
43 | try { | ||||
44 | require_module($class_name); | ||||
45 | $result = 1; | ||||
46 | } | ||||
47 | catch { | ||||
48 | if ( /Can\'t locate .* in \@INC/ && !/Compilation failed/ ) { | ||||
49 | $result = 0; | ||||
50 | } | ||||
51 | else { | ||||
52 | die $_; | ||||
53 | } | ||||
54 | }; | ||||
55 | return $result; | ||||
56 | } | ||||
57 | |||||
58 | sub dump_one_line { | ||||
59 | my ($value) = @_; | ||||
60 | |||||
61 | return Data::Dumper->new( [$value] )->Indent(0)->Sortkeys(1)->Quotekeys(0) | ||||
62 | ->Terse(1)->Dump(); | ||||
63 | } | ||||
64 | |||||
65 | # Simplified read_dir cribbed from File::Slurp | ||||
66 | sub read_dir { | ||||
67 | my ($dir) = @_; | ||||
68 | |||||
69 | ## no critic (RequireInitializationForLocalVars) | ||||
70 | local *DIRH; | ||||
71 | opendir( DIRH, $dir ) or croak "cannot open '$dir': $!"; | ||||
72 | return grep { $_ ne "." && $_ ne ".." } readdir(DIRH); | ||||
73 | } | ||||
74 | |||||
75 | sub read_file { | ||||
76 | my ($file) = @_; | ||||
77 | |||||
78 | # Fast slurp, adapted from File::Slurp::read, with unnecessary options removed | ||||
79 | # | ||||
80 | my $buf = ""; | ||||
81 | my $read_fh; | ||||
82 | unless ( sysopen( $read_fh, $file, $Fetch_Flags ) ) { | ||||
83 | croak "read_file '$file' - sysopen: $!"; | ||||
84 | } | ||||
85 | my $size_left = -s $read_fh; | ||||
86 | while (1) { | ||||
87 | my $read_cnt = sysread( $read_fh, $buf, $size_left, length $buf ); | ||||
88 | if ( defined $read_cnt ) { | ||||
89 | last if $read_cnt == 0; | ||||
90 | $size_left -= $read_cnt; | ||||
91 | last if $size_left <= 0; | ||||
92 | } | ||||
93 | else { | ||||
94 | croak "read_file '$file' - sysread: $!"; | ||||
95 | } | ||||
96 | } | ||||
97 | return $buf; | ||||
98 | } | ||||
99 | |||||
100 | sub write_file { | ||||
101 | my ( $file, $data, $file_create_mode ) = @_; | ||||
102 | $file_create_mode = oct(666) if !defined($file_create_mode); | ||||
103 | |||||
104 | # Fast spew, adapted from File::Slurp::write, with unnecessary options removed | ||||
105 | # | ||||
106 | { | ||||
107 | my $write_fh; | ||||
108 | unless ( sysopen( $write_fh, $file, $Store_Flags, $file_create_mode ) ) | ||||
109 | { | ||||
110 | croak "write_file '$file' - sysopen: $!"; | ||||
111 | } | ||||
112 | my $size_left = length($data); | ||||
113 | my $offset = 0; | ||||
114 | do { | ||||
115 | my $write_cnt = syswrite( $write_fh, $data, $size_left, $offset ); | ||||
116 | unless ( defined $write_cnt ) { | ||||
117 | croak "write_file '$file' - syswrite: $!"; | ||||
118 | } | ||||
119 | $size_left -= $write_cnt; | ||||
120 | $offset += $write_cnt; | ||||
121 | } while ( $size_left > 0 ); | ||||
122 | } | ||||
123 | } | ||||
124 | |||||
125 | { | ||||
126 | |||||
127 | # For efficiency, use Data::UUID to generate an initial unique id, then suffix it to | ||||
128 | # generate a series of 0x10000 unique ids. Not to be used for hard-to-guess ids, obviously. | ||||
129 | |||||
130 | 1 | 100ns | my $uuid; | ||
131 | 1 | 300ns | my $suffix = 0; | ||
132 | |||||
133 | sub unique_id { | ||||
134 | if ( !$suffix || !defined($uuid) ) { | ||||
135 | my $ug = Data::UUID->new(); | ||||
136 | $uuid = $ug->create_hex(); | ||||
137 | } | ||||
138 | my $hex = sprintf( '%s%04x', $uuid, $suffix ); | ||||
139 | $suffix = ( $suffix + 1 ) & 0xffff; | ||||
140 | return $hex; | ||||
141 | } | ||||
142 | } | ||||
143 | |||||
144 | 2 | 4µs | 1 | 30µs | # spent 37µs (7+30) within CHI::Util::BEGIN@144 which was called:
# once (7µs+30µs) by CHI::Stats::BEGIN@3 at line 145 # spent 30µs making 1 call to constant::import |
145 | 1 | 154µs | 1 | 37µs | ( $File::Spec::ISA[0] eq 'File::Spec::Unix' ); # spent 37µs making 1 call to CHI::Util::BEGIN@144 |
146 | |||||
147 | sub fast_catdir { | ||||
148 | if (_FILE_SPEC_USING_UNIX) { | ||||
149 | return join '/', @_; | ||||
150 | } | ||||
151 | else { | ||||
152 | return catdir(@_); | ||||
153 | } | ||||
154 | } | ||||
155 | |||||
156 | sub fast_catfile { | ||||
157 | if (_FILE_SPEC_USING_UNIX) { | ||||
158 | return join '/', @_; | ||||
159 | } | ||||
160 | else { | ||||
161 | return catfile(@_); | ||||
162 | } | ||||
163 | } | ||||
164 | |||||
165 | 1 | 1µs | my %memory_size_units = ( 'k' => 1024, 'm' => 1024 * 1024 ); | ||
166 | |||||
167 | sub parse_memory_size { | ||||
168 | my $size = shift; | ||||
169 | if ( $size =~ /^\d+b?$/ ) { | ||||
170 | return $size; | ||||
171 | } | ||||
172 | elsif ( my ( $quantity, $unit ) = ( $size =~ /^(\d+)\s*([km])b?$/i ) ) { | ||||
173 | return $quantity * $memory_size_units{ lc($unit) }; | ||||
174 | } | ||||
175 | else { | ||||
176 | croak "cannot parse memory size '$size'"; | ||||
177 | } | ||||
178 | } | ||||
179 | |||||
180 | 1 | 2µs | 1 | 19µs | my $json = JSON::MaybeXS->new( utf8 => 1, canonical => 1 ); # spent 19µs making 1 call to JSON::MaybeXS::new |
181 | |||||
182 | sub json_decode { | ||||
183 | $json->decode( $_[0] ); | ||||
184 | } | ||||
185 | |||||
186 | sub json_encode { | ||||
187 | $json->encode( $_[0] ); | ||||
188 | } | ||||
189 | |||||
190 | 1 | 4µs | 1; | ||
191 | |||||
192 | __END__ |