← Index
NYTProf Performance Profile   « line view »
For -e
  Run on Thu Mar 24 14:41:10 2016
Reported on Thu Mar 24 14:41:17 2016

Filename/zpool_host_mnt/mnt/home/s1/repos/perl-JSON-Decode-Regexp/lib/JSON/Decode/Regexp.pm
StatementsExecuted 5011 statements in 114ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
10001191.2ms91.2msJSON::Decode::Regexp::::CORE:regcompJSON::Decode::Regexp::CORE:regcomp (opcode)
1000119.39ms109msJSON::Decode::Regexp::::from_jsonJSON::Decode::Regexp::from_json
1000118.68ms8.68msJSON::Decode::Regexp::::CORE:matchJSON::Decode::Regexp::CORE:match (opcode)
1112.52ms2.57msJSON::Decode::Regexp::::BEGIN@8JSON::Decode::Regexp::BEGIN@8
111616µs701µsJSON::Decode::Regexp::::BEGIN@7JSON::Decode::Regexp::BEGIN@7
11128µs28µsJSON::Decode::Regexp::::BEGIN@6JSON::Decode::Regexp::BEGIN@6
1119µs9µsJSON::Decode::Regexp::::CORE:qrJSON::Decode::Regexp::CORE:qr (opcode)
0000s0sJSON::Decode::Regexp::::__ANON__[lib/JSON/Decode/Regexp.pm:142]JSON::Decode::Regexp::__ANON__[lib/JSON/Decode/Regexp.pm:142]
0000s0sJSON::Decode::Regexp::::_failJSON::Decode::Regexp::_fail
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package JSON::Decode::Regexp;
2
3# DATE
4# VERSION
5
6256µs128µs
# spent 28µs within JSON::Decode::Regexp::BEGIN@6 which was called: # once (28µs+0s) by main::BEGIN@0 at line 6
use 5.010001;
# spent 28µs making 1 call to JSON::Decode::Regexp::BEGIN@6
72431µs2706µs
# spent 701µs (616+85) within JSON::Decode::Regexp::BEGIN@7 which was called: # once (616µs+85µs) by main::BEGIN@0 at line 7
use strict;
# spent 701µs making 1 call to JSON::Decode::Regexp::BEGIN@7 # spent 5µs making 1 call to strict::import
823.05ms22.59ms
# spent 2.57ms (2.52+54µs) within JSON::Decode::Regexp::BEGIN@8 which was called: # once (2.52ms+54µs) by main::BEGIN@0 at line 8
use warnings;
# spent 2.57ms making 1 call to JSON::Decode::Regexp::BEGIN@8 # spent 17µs making 1 call to warnings::import
9
10#use Data::Dumper;
11
121788µsrequire Exporter;
13112µsour @ISA = qw(Exporter);
1411µsour @EXPORT_OK = qw(from_json);
15
16sub _fail { die __PACKAGE__.": $_[0] at offset ".pos()."\n" }
17
18our $FROM_JSON = qr{
19
20(?:
2110007.10ms (?&VALUE) (?{ $_ = $^R->[1] })
22|
23 \z (?{ _fail "Unexpected end of input" })
24|
25 (?{ _fail "Invalid literal" })
26)
27
28(?(DEFINE)
29
30(?<OBJECT>
31 \{\s*
32 (?{ [$^R, {}] })
33 (?:
34 (?&KV) # [[$^R, {}], $k, $v]
35 (?{ [$^R->[0][0], {$^R->[1] => $^R->[2]}] })
36 \s*
37 (?:
38 (?:
39 ,\s* (?&KV) # [[$^R, {...}], $k, $v]
40 (?{ $^R->[0][1]{ $^R->[1] } = $^R->[2]; $^R->[0] })
41 )*
42 |
43 (?:[^,\}]|\z) (?{ _fail "Expected ',' or '\x7d'" })
44 )*
45 )?
46 \s*
47 (?:
48 \}
49 |
50 (?:.|\z) (?{ _fail "Expected closing of hash" })
51 )
52)
53
54(?<KV>
55 (?&STRING) # [$^R, "string"]
56 \s*
57 (?:
58 :\s* (?&VALUE) # [[$^R, "string"], $value]
59 (?{ [$^R->[0][0], $^R->[0][1], $^R->[1]] })
60 |
61 (?:[^:]|\z) (?{ _fail "Expected ':'" })
62 )
63)
64
65(?<ARRAY>
66 \[\s*
67 (?{ [$^R, []] })
68 (?:
69 (?&VALUE) # [[$^R, []], $val]
70 (?{ [$^R->[0][0], [$^R->[1]]] })
71 \s*
72 (?:
73 (?:
74 ,\s* (?&VALUE)
75 (?{ push @{$^R->[0][1]}, $^R->[1]; $^R->[0] })
76 )*
77 |
78 (?: [^,\]]|\z ) (?{ _fail "Expected ',' or '\x5d'" })
79 )
80 )?
81 \s*
82 (?:
83 \]
84 |
85 (?:.|\z) (?{ _fail "Expected closing of array" })
86 )
87)
88
89(?<VALUE>
90 \s*
91 (
92 (?&STRING)
93 |
94 (?&NUMBER)
95 |
96 (?&OBJECT)
97 |
98 (?&ARRAY)
99 |
100 true (?{ [$^R, 1] })
101 |
102 false (?{ [$^R, 0] })
103 |
104 null (?{ [$^R, undef] })
105 )
106 \s*
107)
108
109(?<STRING>
110 (
111 "
112 (?:
113 [^\\"]+
114 |
115 \\ ["\\/bfnrt]
116# |
117# \\ u [0-9a-fA-f]{4}
118 |
119 \\ . (?{ _fail "Invalid string escape character" })
120 )*
121 (?:
122 "
123 |
124 (?:\\|\z) (?{ _fail "Expected closing of string" })
125 )
126 )
127
128 (?{ [$^R, eval $^N] })
129)
130
131(?<NUMBER>
132 (
133 -?
134 (?: 0 | [1-9]\d* )
135 (?: \. \d+ )?
136 (?: [eE] [-+]? \d+ )?
137 )
138
139 (?{ [$^R, 0+$^N] })
140)
141
142121µs19µs) }xms;
# spent 9µs making 1 call to JSON::Decode::Regexp::CORE:qr
143
144
# spent 109ms (9.39+99.9) within JSON::Decode::Regexp::from_json which was called 1000 times, avg 109µs/call: # 1000 times (9.39ms+99.9ms) by main::RUNTIME at line 1 of -e, avg 109µs/call
sub from_json {
1451000340µs local $_ = shift;
1461000102µs local $^R;
1472000102ms200099.9ms eval { m{\A$FROM_JSON\z}; } and return $_;
# spent 91.2ms making 1000 calls to JSON::Decode::Regexp::CORE:regcomp, avg 91µs/call # spent 8.68ms making 1000 calls to JSON::Decode::Regexp::CORE:match, avg 9µs/call
148 die $@ if $@;
149 die 'no match';
150}
151
15216µs1;
153# ABSTRACT: JSON parser as a single Perl Regex
154
155=head1 SYNOPSIS
156
157 use JSON::Decode::Regexp qw(from_json);
158 my $data = from_json(q([1, true, "a", {"b":null}]));
159
160
161=head1 DESCRIPTION
162
163This module is a packaging of Randal L. Schwartz' code (with some modification)
164originally posted at:
165
166 http://perlmonks.org/?node_id=995856
167
168The code is licensed "just like Perl".
169
170
171=head1 FUNCTIONS
172
173=head2 from_json($str) => DATA
174
175Decode JSON in C<$str>. Dies on error.
176
177
178=head1 FAQ
179
180=head2 How does this module compare to other JSON modules on CPAN?
181
182As of version 0.04, performance-wise this module quite on par with L<JSON::PP>
183(faster on strings and longer arrays/objects, slower on simpler JSON) and a bit
184slower than L<JSON::Tiny>. And of course all three are much slower than XS-based
185modules like L<JSON::XS>.
186
187JSON::Decode::Regexp does not yet support Unicode, and does not pinpoint exact
188location on parse error.
189
190In general, I don't see a point in using it in production (I recommend instead
191L<JSON::XS> or L<Cpanel::JSON::XS> if you can use XS modules, or L<JSON::Tiny>
192if you must use pure Perl modules). But it is a cool hack that demonstrates the
193power of Perl regular expressions and beautiful code.
194
195
196=head1 SEE ALSO
197
198Pure-perl modules: L<JSON::Tiny>, L<JSON::PP>, L<Pegex::JSON>,
199L<JSON::Decode::Marpa>.
200
201XS modules: L<JSON::XS>, L<Cpanel::JSON::XS>.
202
203=cut
 
# spent 8.68ms within JSON::Decode::Regexp::CORE:match which was called 1000 times, avg 9µs/call: # 1000 times (8.68ms+0s) by JSON::Decode::Regexp::from_json at line 147, avg 9µs/call
sub JSON::Decode::Regexp::CORE:match; # opcode
# spent 9µs within JSON::Decode::Regexp::CORE:qr which was called: # once (9µs+0s) by main::BEGIN@0 at line 142
sub JSON::Decode::Regexp::CORE:qr; # opcode
# spent 91.2ms within JSON::Decode::Regexp::CORE:regcomp which was called 1000 times, avg 91µs/call: # 1000 times (91.2ms+0s) by JSON::Decode::Regexp::from_json at line 147, avg 91µs/call
sub JSON::Decode::Regexp::CORE:regcomp; # opcode