← Index
NYTProf Performance Profile   « line view »
For /home/ss5/perl5/perlbrew/perls/perl-5.22.0/bin/benchmarkanything-storage
  Run on Mon Jan 29 16:55:34 2018
Reported on Mon Jan 29 16:57:07 2018

Filename/home/ss5/perl5/perlbrew/perls/perl-5.22.0/lib/site_perl/5.22.0/CHI/Util.pm
StatementsExecuted 36 statements in 993µs
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
1112.52ms2.93msCHI::Util::::BEGIN@5CHI::Util::BEGIN@5
111552µs1.82msCHI::Util::::BEGIN@9CHI::Util::BEGIN@9
111548µs901µsCHI::Util::::BEGIN@6CHI::Util::BEGIN@6
111338µs795µsCHI::Util::::BEGIN@10CHI::Util::BEGIN@10
111320µs441µsCHI::Util::::BEGIN@8CHI::Util::BEGIN@8
111244µs740µsCHI::Util::::BEGIN@7CHI::Util::BEGIN@7
1117µs37µsCHI::Util::::BEGIN@144CHI::Util::BEGIN@144
1117µs18µsCHI::Util::::BEGIN@4CHI::Util::BEGIN@4
1117µs27µsCHI::Util::::BEGIN@11CHI::Util::BEGIN@11
1116µs26µsCHI::Util::::BEGIN@3CHI::Util::BEGIN@3
1115µs6µsCHI::Util::::BEGIN@12CHI::Util::BEGIN@12
1114µs35µsCHI::Util::::BEGIN@14CHI::Util::BEGIN@14
1113µs6µsCHI::Util::::BEGIN@13CHI::Util::BEGIN@13
0000s0sCHI::Util::::__ANON__[:46]CHI::Util::__ANON__[:46]
0000s0sCHI::Util::::__ANON__[:54]CHI::Util::__ANON__[:54]
0000s0sCHI::Util::::can_loadCHI::Util::can_load
0000s0sCHI::Util::::dump_one_lineCHI::Util::dump_one_line
0000s0sCHI::Util::::fast_catdirCHI::Util::fast_catdir
0000s0sCHI::Util::::fast_catfileCHI::Util::fast_catfile
0000s0sCHI::Util::::json_decodeCHI::Util::json_decode
0000s0sCHI::Util::::json_encodeCHI::Util::json_encode
0000s0sCHI::Util::::parse_memory_sizeCHI::Util::parse_memory_size
0000s0sCHI::Util::::read_dirCHI::Util::read_dir
0000s0sCHI::Util::::read_fileCHI::Util::read_file
0000s0sCHI::Util::::unique_idCHI::Util::unique_id
0000s0sCHI::Util::::write_fileCHI::Util::write_file
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package CHI::Util;
21300ns$CHI::Util::VERSION = '0.60';
3218µs245µ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
use Carp qw( croak longmess );
# spent 26µs making 1 call to CHI::Util::BEGIN@3 # spent 20µs making 1 call to Exporter::import
4215µs230µ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
use Module::Runtime qw(require_module);
# spent 18µs making 1 call to CHI::Util::BEGIN@4 # spent 12µs making 1 call to Module::Runtime::import
5276µs22.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
use Data::Dumper;
# spent 2.93ms making 1 call to CHI::Util::BEGIN@5 # spent 18µs making 1 call to Exporter::import
6258µs2924µ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
use Data::UUID;
# spent 901µs making 1 call to CHI::Util::BEGIN@6 # spent 23µs making 1 call to Exporter::import
7284µs21.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
use Fcntl qw( :DEFAULT );
# spent 740µs making 1 call to CHI::Util::BEGIN@7 # spent 334µs making 1 call to Exporter::import
8257µs2474µ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
use File::Spec::Functions qw(catdir catfile);
# spent 441µs making 1 call to CHI::Util::BEGIN@8 # spent 32µs making 1 call to Exporter::import
9262µs21.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
use JSON::MaybeXS;
# spent 1.82ms making 1 call to CHI::Util::BEGIN@9 # spent 21µs making 1 call to Exporter::import
10268µs2807µ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
use Time::Duration::Parse;
# spent 795µs making 1 call to CHI::Util::BEGIN@10 # spent 12µs making 1 call to Exporter::Lite::import
11217µs247µ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
use Try::Tiny;
# spent 27µs making 1 call to CHI::Util::BEGIN@11 # spent 20µs making 1 call to Exporter::import
12210µs27µ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
use strict;
# spent 6µs making 1 call to CHI::Util::BEGIN@12 # spent 1µs making 1 call to strict::import
13212µs29µ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
use warnings;
# spent 6µs making 1 call to CHI::Util::BEGIN@13 # spent 3µs making 1 call to warnings::import
142348µs265µ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
use base qw(Exporter);
# spent 35µs making 1 call to CHI::Util::BEGIN@14 # spent 30µs making 1 call to base::import
15
1611µsour @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
321200nsmy $Fetch_Flags = O_RDONLY | O_BINARY;
3310smy $Store_Flags = O_WRONLY | O_CREAT | O_BINARY;
34
35sub 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
58sub 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
66sub 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
75sub 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
100sub 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
1301100ns my $uuid;
1311300ns 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
14424µs130µ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
use constant _FILE_SPEC_USING_UNIX =>
# spent 30µs making 1 call to constant::import
1451154µs137µs ( $File::Spec::ISA[0] eq 'File::Spec::Unix' );
# spent 37µs making 1 call to CHI::Util::BEGIN@144
146
147sub fast_catdir {
148 if (_FILE_SPEC_USING_UNIX) {
149 return join '/', @_;
150 }
151 else {
152 return catdir(@_);
153 }
154}
155
156sub fast_catfile {
157 if (_FILE_SPEC_USING_UNIX) {
158 return join '/', @_;
159 }
160 else {
161 return catfile(@_);
162 }
163}
164
16511µsmy %memory_size_units = ( 'k' => 1024, 'm' => 1024 * 1024 );
166
167sub 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
18012µs119µsmy $json = JSON::MaybeXS->new( utf8 => 1, canonical => 1 );
# spent 19µs making 1 call to JSON::MaybeXS::new
181
182sub json_decode {
183 $json->decode( $_[0] );
184}
185
186sub json_encode {
187 $json->encode( $_[0] );
188}
189
19014µs1;
191
192__END__