← 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/JSON/MaybeXS.pm
StatementsExecuted 5035 statements in 28.5ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
10023324.3ms26.3msJSON::MaybeXS::::newJSON::MaybeXS::new
111729µs1.09msJSON::MaybeXS::::_choose_json_moduleJSON::MaybeXS::_choose_json_module
1118µs1.14msJSON::MaybeXS::::BEGIN@29JSON::MaybeXS::BEGIN@29
1118µs10µsJSON::MaybeXS::::BEGIN@3JSON::MaybeXS::BEGIN@3
1116µs66µsJSON::MaybeXS::::BEGIN@5JSON::MaybeXS::BEGIN@5
1114µs24µsJSON::MaybeXS::::BEGIN@4JSON::MaybeXS::BEGIN@4
1113µs3µsJSON::MaybeXS::::BEGIN@51JSON::MaybeXS::BEGIN@51
1112µs2µsJSON::MaybeXS::::BEGIN@63JSON::MaybeXS::BEGIN@63
0000s0sJSON::MaybeXS::::JSONJSON::MaybeXS::JSON
0000s0sJSON::MaybeXS::::from_jsonJSON::MaybeXS::from_json
0000s0sJSON::MaybeXS::::is_boolJSON::MaybeXS::is_bool
0000s0sJSON::MaybeXS::::to_jsonJSON::MaybeXS::to_json
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package JSON::MaybeXS;
2
3214µs212µs
# spent 10µs (8+2) within JSON::MaybeXS::BEGIN@3 which was called: # once (8µs+2µs) by CHI::Util::BEGIN@9 at line 3
use strict;
# spent 10µs making 1 call to JSON::MaybeXS::BEGIN@3 # spent 2µs making 1 call to strict::import
4215µs243µs
# spent 24µs (4+19) within JSON::MaybeXS::BEGIN@4 which was called: # once (4µs+19µs) by CHI::Util::BEGIN@9 at line 4
use warnings FATAL => 'all';
# spent 24µs making 1 call to JSON::MaybeXS::BEGIN@4 # spent 19µs making 1 call to warnings::import
52114µs2125µs
# spent 66µs (6+60) within JSON::MaybeXS::BEGIN@5 which was called: # once (6µs+60µs) by CHI::Util::BEGIN@9 at line 5
use base qw(Exporter);
# spent 66µs making 1 call to JSON::MaybeXS::BEGIN@5 # spent 60µs making 1 call to base::import
6
71400nsour $VERSION = '1.003005';
8111µs$VERSION = eval $VERSION;
# spent 2µs executing statements in string eval
9
10
# spent 1.09ms (729µs+359µs) within JSON::MaybeXS::_choose_json_module which was called: # once (729µs+359µs) by JSON::MaybeXS::BEGIN@29 at line 30
sub _choose_json_module {
111200ns return 'Cpanel::JSON::XS' if $INC{'Cpanel/JSON/XS.pm'};
121100ns return 'JSON::XS' if $INC{'JSON/XS.pm'};
13
141100ns my @err;
15
163152µs return 'Cpanel::JSON::XS' if eval { require Cpanel::JSON::XS; 1; };
17 push @err, "Error loading Cpanel::JSON::XS: $@";
18
19 return 'JSON::XS' if eval { require JSON::XS; 1; };
20 push @err, "Error loading JSON::XS: $@";
21
22 return 'JSON::PP' if eval { require JSON::PP; 1 };
23 push @err, "Error loading JSON::PP: $@";
24
25 die join( "\n", "Couldn't load a JSON module:", @err );
26
27}
28
29
# spent 1.14ms (8µs+1.13) within JSON::MaybeXS::BEGIN@29 which was called: # once (8µs+1.13ms) by CHI::Util::BEGIN@9 at line 32
BEGIN {
301800ns11.09ms our $JSON_Class = _choose_json_module();
# spent 1.09ms making 1 call to JSON::MaybeXS::_choose_json_module
3114µs147µs $JSON_Class->import(qw(encode_json decode_json));
# spent 47µs making 1 call to Exporter::import
321102µs11.14ms}
# spent 1.14ms making 1 call to JSON::MaybeXS::BEGIN@29
33
341700nsour @EXPORT = qw(encode_json decode_json JSON);
351300nsmy @EXPORT_ALL = qw(is_bool);
361300nsour @EXPORT_OK = qw(is_bool to_json from_json);
3712µsour %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_ALL ],
38 legacy => [ @EXPORT, @EXPORT_OK ],
39 );
40
41sub JSON () { our $JSON_Class }
42
43
# spent 26.3ms (24.3+2.07) within JSON::MaybeXS::new which was called 1002 times, avg 26µs/call: # 1000 times (24.2ms+2.07ms) by BenchmarkAnything::Storage::Search::Elasticsearch::Serializer::JSON::DontTouchMyUTF8::__ANON__[/home/ss5/perl5/perlbrew/perls/perl-5.22.0/lib/site_perl/5.22.0/BenchmarkAnything/Storage/Search/Elasticsearch/Serializer/JSON/DontTouchMyUTF8.pm:13] at line 13 of BenchmarkAnything/Storage/Search/Elasticsearch/Serializer/JSON/DontTouchMyUTF8.pm, avg 26µs/call # once (16µs+3µs) by CHI::Stats::BEGIN@3 at line 180 of CHI/Util.pm # once (10µs+2µs) by Search::Elasticsearch::Serializer::JSON::__ANON__[/home/ss5/perl5/perlbrew/perls/perl-5.22.0/lib/site_perl/5.22.0/Search/Elasticsearch/Serializer/JSON.pm:6] at line 6 of Search/Elasticsearch/Serializer/JSON.pm
sub new {
441002296µs shift;
4510022.01ms my %args = @_ == 1 ? %{$_[0]} : @_;
46100220.1ms10022.07ms my $new = (our $JSON_Class)->new;
# spent 2.07ms making 1002 calls to Cpanel::JSON::XS::new, avg 2µs/call
4710021.76ms2700ns $new->$_($args{$_}) for keys %args;
# spent 400ns making 1 call to Cpanel::JSON::XS::utf8 # spent 300ns making 1 call to Cpanel::JSON::XS::canonical
4810023.57ms return $new;
49}
50
51255µs13µs
# spent 3µs within JSON::MaybeXS::BEGIN@51 which was called: # once (3µs+0s) by CHI::Util::BEGIN@9 at line 51
use Scalar::Util ();
# spent 3µs making 1 call to JSON::MaybeXS::BEGIN@51
52
53sub is_bool {
54 die 'is_bool is not a method' if $_[1];
55
56 Scalar::Util::blessed($_[0])
57 and ($_[0]->isa('JSON::XS::Boolean')
58 or $_[0]->isa('Cpanel::JSON::XS::Boolean')
59 or $_[0]->isa('JSON::PP::Boolean'));
60}
61
62# (mostly) CopyPasta from JSON.pm version 2.90
632205µs12µs
# spent 2µs within JSON::MaybeXS::BEGIN@63 which was called: # once (2µs+0s) by CHI::Util::BEGIN@9 at line 63
use Carp ();
# spent 2µs making 1 call to JSON::MaybeXS::BEGIN@63
64
65sub from_json ($@) {
66 if ( ref($_[0]) =~ /^JSON/ or $_[0] =~ /^JSON/ ) {
67 Carp::croak "from_json should not be called as a method.";
68 }
69 my $json = JSON()->new;
70
71 if (@_ == 2 and ref $_[1] eq 'HASH') {
72 my $opt = $_[1];
73 for my $method (keys %$opt) {
74 $json->$method( $opt->{$method} );
75 }
76 }
77
78 return $json->decode( $_[0] );
79}
80
81sub to_json ($@) {
82 if (
83 ref($_[0]) =~ /^JSON/
84 or (@_ > 2 and $_[0] =~ /^JSON/)
85 ) {
86 Carp::croak "to_json should not be called as a method.";
87 }
88 my $json = JSON()->new;
89
90 if (@_ == 2 and ref $_[1] eq 'HASH') {
91 my $opt = $_[1];
92 for my $method (keys %$opt) {
93 $json->$method( $opt->{$method} );
94 }
95 }
96
97 $json->encode($_[0]);
98}
99
10014µs1;
101
102=head1 NAME
103
104JSON::MaybeXS - Use L<Cpanel::JSON::XS> with a fallback to L<JSON::XS> and L<JSON::PP>
105
106=head1 SYNOPSIS
107
108 use JSON::MaybeXS;
109
110 my $data_structure = decode_json($json_input);
111
112 my $json_output = encode_json($data_structure);
113
114 my $json = JSON->new;
115
116 my $json_with_args = JSON::MaybeXS->new(utf8 => 1); # or { utf8 => 1 }
117
118=head1 DESCRIPTION
119
120This module first checks to see if either L<Cpanel::JSON::XS> or
121L<JSON::XS> is already loaded, in which case it uses that module. Otherwise
122it tries to load L<Cpanel::JSON::XS>, then L<JSON::XS>, then L<JSON::PP>
123in order, and either uses the first module it finds or throws an error.
124
125It then exports the C<encode_json> and C<decode_json> functions from the
126loaded module, along with a C<JSON> constant that returns the class name
127for calling C<new> on.
128
129If you're writing fresh code rather than replacing L<JSON.pm|JSON> usage, you might
130want to pass options as constructor args rather than calling mutators, so
131we provide our own C<new> method that supports that.
132
133=head1 EXPORTS
134
135C<encode_json>, C<decode_json> and C<JSON> are exported by default; C<is_bool>
136is exported on request.
137
138To import only some symbols, specify them on the C<use> line:
139
140 use JSON::MaybeXS qw(encode_json decode_json is_bool); # functions only
141
142 use JSON::MaybeXS qw(JSON); # JSON constant only
143
144To import all available sensible symbols (C<encode_json>, C<decode_json>, and
145C<is_bool>), use C<:all>:
146
147 use JSON::MaybeXS ':all';
148
149To import all symbols including those needed by legacy apps that use L<JSON::PP>:
150
151 use JSON::MaybeXS ':legacy';
152
153This imports the C<to_json> and C<from_json> symbols as well as everything in
154C<:all>. NOTE: This is to support legacy code that makes extensive
155use of C<to_json> and C<from_json> which you are not yet in a position to
156refactor. DO NOT use this import tag in new code, in order to avoid
157the crawling horrors of getting UTF8 support subtly wrong. See the
158documentation for L<JSON> for further details.
159
160=head2 encode_json
161
162This is the C<encode_json> function provided by the selected implementation
163module, and takes a perl data structure which is serialised to JSON text.
164
165 my $json_text = encode_json($data_structure);
166
167=head2 decode_json
168
169This is the C<decode_json> function provided by the selected implementation
170module, and takes a string of JSON text to deserialise to a perl data structure.
171
172 my $data_structure = decode_json($json_text);
173
174=head2 to_json, from_json
175
176See L<JSON> for details. These are included to support legacy code
177B<only>.
178
179=head2 JSON
180
181The C<JSON> constant returns the selected implementation module's name for
182use as a class name - so:
183
184 my $json_obj = JSON->new; # returns a Cpanel::JSON::XS or JSON::PP object
185
186and that object can then be used normally:
187
188 my $data_structure = $json_obj->decode($json_text); # etc.
189
190=head2 is_bool
191
192 $is_boolean = is_bool($scalar)
193
194Returns true if the passed scalar represents either C<true> or
195C<false>, two constants that act like C<1> and C<0>, respectively
196and are used to represent JSON C<true> and C<false> values in Perl.
197
198Since this is a bare sub in the various backend classes, it cannot be called as
199a class method like the other interfaces; it must be called as a function, with
200no invocant. It supports the representation used in all JSON backends.
201
202=head1 CONSTRUCTOR
203
204=head2 new
205
206With L<JSON::PP>, L<JSON::XS> and L<Cpanel::JSON::XS> you are required to call
207mutators to set options, such as:
208
209 my $json = $class->new->utf8(1)->pretty(1);
210
211Since this is a trifle irritating and noticeably un-perlish, we also offer:
212
213 my $json = JSON::MaybeXS->new(utf8 => 1, pretty => 1);
214
215which works equivalently to the above (and in the usual tradition will accept
216a hashref instead of a hash, should you so desire).
217
218=head1 BOOLEANS
219
220To include JSON-aware booleans (C<true>, C<false>) in your data, just do:
221
222 use JSON::MaybeXS;
223 my $true = JSON->true;
224 my $false = JSON->false;
225
226=head1 CAVEATS
227
228The C<new()> method in this module is technically a factory, not a
229constructor, because the objects it returns will I<NOT> be blessed into the
230C<JSON::MaybeXS> class.
231
232If you are using an object returned by this module as a Moo(se) attribute,
233this type constraint code:
234
235 is 'json' => ( isa => 'JSON::MaybeXS' );
236
237will I<NOT> do what you expect. Instead, either rely on the C<JSON> class
238constant described above, as so:
239
240 is 'json' => ( isa => JSON::MaybeXS::JSON() );
241
242Alternatively, you can use duck typing:
243
244 use Moose::Util::TypeConstraints 'duck_type';
245 is 'json' => ( isa => Object , duck_type([qw/ encode decode /]));
246
247=head1 AUTHOR
248
249mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
250
251=head1 CONTRIBUTORS
252
253=over 4
254
255=item * Clinton Gormley <drtech@cpan.org>
256
257=item * Karen Etheridge <ether@cpan.org>
258
259=item * Kieren Diment <diment@gmail.com>
260
261=back
262
263=head1 COPYRIGHT
264
265Copyright (c) 2013 the C<JSON::MaybeXS> L</AUTHOR> and L</CONTRIBUTORS>
266as listed above.
267
268=head1 LICENSE
269
270This library is free software and may be distributed under the same terms
271as perl itself.
272
273=cut