Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
new benchmark: json parsing without creating objects
  • Loading branch information
timo committed Jul 30, 2014
1 parent 563ba46 commit 2f84e2d
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
8 changes: 8 additions & 0 deletions minibenchmarks.pl
Expand Up @@ -8,6 +8,14 @@
perl6 => [qw( BENCH/perl6/parse-json DATA/panda-projects.json SCALE )],
nqp => [qw( BENCH/nqp/parse-json DATA/panda-projects.json SCALE )],
},
{
name => 'parse-json-no-obj-creation',
skip => [qw( )],
tags => [qw( io input parsing )],
scale => 1 << 3,
perl6 => [qw( BENCH/perl6/parse-json-no-obj-creation DATA/panda-projects.json SCALE )],
nqp => [qw( BENCH/nqp/parse-json-no-obj-creation DATA/panda-projects.json SCALE )],
},
{
name => 'rc-forest-fire',
skip => [qw( niecza.nqp p6.pl p6.js_v8 )],
Expand Down
56 changes: 56 additions & 0 deletions nqp/parse-json-no-obj-creation
@@ -0,0 +1,56 @@
# JSON parsing copied from JSON::Tiny with changes to work in NQP
# and other very minor changes

grammar JSON::Tiny::Grammar {
token TOP { ^ \s* [ <object> | <array> ] \s* $ }
rule object { '{' ~ '}' <pairlist> }
rule pairlist { <?> <pair> * % \, }
rule pair { <?> <string> ':' <value> }
rule array { '[' ~ ']' <arraylist> }
rule arraylist { <?> <value>* % [ \, ] }

proto token value {*}
token value:sym<number> {
'-'?
[ 0 | <[1..9]> <[0..9]>* ]
[ \. <[0..9]>+ ]?
[ <[eE]> [\+|\-]? <[0..9]>+ ]?
}
token value:sym<true> { <sym> }
token value:sym<false> { <sym> }
token value:sym<null> { <sym> }
token value:sym<object> { <object> }
token value:sym<array> { <array> }
token value:sym<string> { <string> }

token string {
\" ~ \" ( <str> | \\ <str_escape> )*
}

token str {
<-["\\\t\n]>+
}

token str_escape {
<["\\/bfnrt]> | u <xdigit>**4
}
}

sub from-json($text) {
JSON::Tiny::Grammar.parse($text);
}

sub main($json-file, $count) {
my $json := slurp($json-file);

my int $i := 0;
while $i < $count {
my $data := from-json($json);
$i := $i + 1;
}
}

sub MAIN (*@args) {
@args.shift if @args == 3;
main(|@args);
}
48 changes: 48 additions & 0 deletions perl6/parse-json-no-obj-creation
@@ -0,0 +1,48 @@
# JSON parsing copied from JSON::Tiny with very minor changes

grammar JSON::Tiny::Grammar {
token TOP { ^ \s* [ <object> | <array> ] \s* $ }
rule object { '{' ~ '}' <pairlist> }
rule pairlist { <?> <pair> * % \, }
rule pair { <?> <string> ':' <value> }
rule array { '[' ~ ']' <arraylist> }
rule arraylist { <?> <value>* % [ \, ] }

proto token value {*}
token value:sym<number> {
'-'?
[ 0 | <[1..9]> <[0..9]>* ]
[ \. <[0..9]>+ ]?
[ <[eE]> [\+|\-]? <[0..9]>+ ]?
}
token value:sym<true> { <sym> }
token value:sym<false> { <sym> }
token value:sym<null> { <sym> }
token value:sym<object> { <object> }
token value:sym<array> { <array> }
token value:sym<string> { <string> }

token string {
\" ~ \" ( <str> | \\ <str_escape> )*
}

token str {
<-["\\\t\n]>+
}

token str_escape {
<["\\/bfnrt]> | u <xdigit>**4
}
}

sub from-json($text) {
JSON::Tiny::Grammar.parse($text);
}

sub MAIN($json-file, $count) {
my $json := slurp($json-file);

for ^$count {
my $data := from-json($json); #OK
}
}

0 comments on commit 2f84e2d

Please sign in to comment.