|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
/* eslint-env node */
|
|
|
|
/* eslint no-console: 0*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var fs = require("fs");
|
|
|
|
var peg = require("../lib/peg");
|
|
|
|
|
|
|
|
var benchmarks = require("./benchmarks.js");
|
|
|
|
var Runner = require("./runner.js")(peg);
|
|
|
|
|
|
|
|
/* Results Table Manipulation */
|
|
|
|
|
|
|
|
function dup(text, count) {
|
|
|
|
var result = "";
|
|
|
|
for (var i = 1; i <= count; i++) {
|
|
|
|
result += text;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function padLeft(text, length) {
|
|
|
|
return dup(" ", length - text.length) + text;
|
|
|
|
}
|
|
|
|
|
|
|
|
function padRight(text, length) {
|
|
|
|
return text + dup(" ", length - text.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
function center(text, length) {
|
|
|
|
var padLength = (length - text.length) / 2;
|
|
|
|
return dup(" ", Math.floor(padLength))
|
|
|
|
+ text
|
|
|
|
+ dup(" ", Math.ceil(padLength));
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeTableHeader() {
|
|
|
|
console.log("┌─────────────────────────────────────┬───────────┬────────────┬──────────────┐");
|
|
|
|
console.log("│ Test │ Inp. size │ Avg. time │ Avg. speed │");
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeHeading(heading) {
|
|
|
|
console.log("├─────────────────────────────────────┴───────────┴────────────┴──────────────┤");
|
|
|
|
console.log("│ " + center(heading, 75) + " │");
|
|
|
|
console.log("├─────────────────────────────────────┬───────────┬────────────┬──────────────┤");
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeResult(title, inputSize, parseTime) {
|
|
|
|
var KB = 1024;
|
|
|
|
var MS_IN_S = 1000;
|
|
|
|
|
|
|
|
console.log("│ "
|
|
|
|
+ padRight(title, 35)
|
|
|
|
+ " │ "
|
|
|
|
+ padLeft((inputSize / KB).toFixed(2), 6)
|
|
|
|
+ " kB │ "
|
|
|
|
+ padLeft(parseTime.toFixed(2), 7)
|
|
|
|
+ " ms │ "
|
|
|
|
+ padLeft(((inputSize / KB) / (parseTime / MS_IN_S)).toFixed(2), 7)
|
|
|
|
+ " kB/s │"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeSeparator() {
|
|
|
|
console.log("├─────────────────────────────────────┼───────────┼────────────┼──────────────┤");
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeTableFooter() {
|
|
|
|
console.log("└─────────────────────────────────────┴───────────┴────────────┴──────────────┘");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helpers */
|
|
|
|
|
|
|
|
function printHelp() {
|
|
|
|
console.log("Usage: run [options]");
|
|
|
|
console.log("");
|
|
|
|
console.log("Runs PEG.js benchmark suite.");
|
|
|
|
console.log("");
|
|
|
|
console.log("Options:");
|
|
|
|
console.log(" -n, --run-count <n> number of runs (default: 10)");
|
|
|
|
console.log(" --cache make tested parsers cache results");
|
|
|
|
console.log(" -o, --optimize <goal> select optimization for speed or size (default:");
|
|
|
|
console.log(" speed)");
|
|
|
|
}
|
|
|
|
|
|
|
|
function exitSuccess() {
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function exitFailure() {
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function abort(message) {
|
|
|
|
console.error(message);
|
|
|
|
exitFailure();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Arguments */
|
|
|
|
|
|
|
|
var args = process.argv.slice(2); // Trim "node" and the script path.
|
|
|
|
|
|
|
|
function isOption(arg) {
|
|
|
|
return (/^-/).test(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
function nextArg() {
|
|
|
|
args.shift();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Main */
|
|
|
|
|
|
|
|
var runCount = 10;
|
Code generator rewrite
This is a complete rewrite of the PEG.js code generator. Its goals are:
1. Allow optimizing the generated parser code for code size as well as
for parsing speed.
2. Prepare ground for future optimizations and big features (like
incremental parsing).
2. Replace the old template-based code-generation system with
something more lightweight and flexible.
4. General code cleanup (structure, style, variable names, ...).
New Architecture
----------------
The new code generator consists of two steps:
* Bytecode generator -- produces bytecode for an abstract virtual
machine
* JavaScript generator -- produces JavaScript code based on the
bytecode
The abstract virtual machine is stack-based. Originally I wanted to make
it register-based, but it turned out that all the code related to it
would be more complex and the bytecode itself would be longer (because
of explicit register specifications in instructions). The only downsides
of the stack-based approach seem to be few small inefficiencies (see
e.g. the |NIP| instruction), which seem to be insignificant.
The new generator allows optimizing for parsing speed or code size (you
can choose using the |optimize| option of the |PEG.buildParser| method
or the --optimize/-o option on the command-line).
When optimizing for size, the JavaScript generator emits the bytecode
together with its constant table and a generic bytecode interpreter.
Because the interpreter is small and the bytecode and constant table
grow only slowly with size of the grammar, the resulting parser is also
small.
When optimizing for speed, the JavaScript generator just compiles the
bytecode into JavaScript. The generated code is relatively efficient, so
the resulting parser is fast.
Internal Identifiers
--------------------
As a small bonus, all internal identifiers visible to user code in the
initializer, actions and predicates are prefixed by |peg$|. This lowers
the chance that identifiers in user code will conflict with the ones
from PEG.js. It also makes using any internals in user code ugly, which
is a good thing. This solves GH-92.
Performance
-----------
The new code generator improved parsing speed and parser code size
significantly. The generated parsers are now:
* 39% faster when optimizing for speed
* 69% smaller when optimizing for size (without minification)
* 31% smaller when optimizing for size (with minification)
(Parsing speed was measured using the |benchmark/run| script. Code size
was measured by generating parsers for examples in the |examples|
directory and adding up the file sizes. Minification was done by |uglify
--ascii| in version 1.3.4.)
Final Note
----------
This is just a beginning! The new code generator lays a foundation upon
which many optimizations and improvements can (and will) be made.
Stay tuned :-)
12 years ago
|
|
|
var options = {
|
|
|
|
cache: false,
|
|
|
|
optimize: "speed"
|
|
|
|
};
|
|
|
|
|
|
|
|
while (args.length > 0 && isOption(args[0])) {
|
|
|
|
switch (args[0]) {
|
|
|
|
case "-n":
|
|
|
|
case "--run-count":
|
|
|
|
nextArg();
|
|
|
|
if (args.length === 0) {
|
|
|
|
abort("Missing parameter of the -n/--run-count option.");
|
|
|
|
}
|
|
|
|
runCount = parseInt(args[0], 10);
|
|
|
|
if (isNaN(runCount) || runCount <= 0) {
|
|
|
|
abort("Number of runs must be a positive integer.");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "--cache":
|
|
|
|
options.cache = true;
|
|
|
|
break;
|
|
|
|
|
Code generator rewrite
This is a complete rewrite of the PEG.js code generator. Its goals are:
1. Allow optimizing the generated parser code for code size as well as
for parsing speed.
2. Prepare ground for future optimizations and big features (like
incremental parsing).
2. Replace the old template-based code-generation system with
something more lightweight and flexible.
4. General code cleanup (structure, style, variable names, ...).
New Architecture
----------------
The new code generator consists of two steps:
* Bytecode generator -- produces bytecode for an abstract virtual
machine
* JavaScript generator -- produces JavaScript code based on the
bytecode
The abstract virtual machine is stack-based. Originally I wanted to make
it register-based, but it turned out that all the code related to it
would be more complex and the bytecode itself would be longer (because
of explicit register specifications in instructions). The only downsides
of the stack-based approach seem to be few small inefficiencies (see
e.g. the |NIP| instruction), which seem to be insignificant.
The new generator allows optimizing for parsing speed or code size (you
can choose using the |optimize| option of the |PEG.buildParser| method
or the --optimize/-o option on the command-line).
When optimizing for size, the JavaScript generator emits the bytecode
together with its constant table and a generic bytecode interpreter.
Because the interpreter is small and the bytecode and constant table
grow only slowly with size of the grammar, the resulting parser is also
small.
When optimizing for speed, the JavaScript generator just compiles the
bytecode into JavaScript. The generated code is relatively efficient, so
the resulting parser is fast.
Internal Identifiers
--------------------
As a small bonus, all internal identifiers visible to user code in the
initializer, actions and predicates are prefixed by |peg$|. This lowers
the chance that identifiers in user code will conflict with the ones
from PEG.js. It also makes using any internals in user code ugly, which
is a good thing. This solves GH-92.
Performance
-----------
The new code generator improved parsing speed and parser code size
significantly. The generated parsers are now:
* 39% faster when optimizing for speed
* 69% smaller when optimizing for size (without minification)
* 31% smaller when optimizing for size (with minification)
(Parsing speed was measured using the |benchmark/run| script. Code size
was measured by generating parsers for examples in the |examples|
directory and adding up the file sizes. Minification was done by |uglify
--ascii| in version 1.3.4.)
Final Note
----------
This is just a beginning! The new code generator lays a foundation upon
which many optimizations and improvements can (and will) be made.
Stay tuned :-)
12 years ago
|
|
|
case "-o":
|
|
|
|
case "--optimize":
|
|
|
|
nextArg();
|
|
|
|
if (args.length === 0) {
|
|
|
|
abort("Missing parameter of the -o/--optimize option.");
|
|
|
|
}
|
|
|
|
if (args[0] !== "speed" && args[0] !== "size") {
|
|
|
|
abort("Optimization goal must be either \"speed\" or \"size\".");
|
|
|
|
}
|
|
|
|
options.optimize = args[0];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "-h":
|
|
|
|
case "--help":
|
|
|
|
printHelp();
|
|
|
|
exitSuccess();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
abort("Unknown option: " + args[0] + ".");
|
|
|
|
}
|
|
|
|
nextArg();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.length > 0) {
|
|
|
|
abort("No arguments are allowed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
Runner.run(benchmarks, runCount, options, {
|
|
|
|
readFile: function(file) {
|
|
|
|
return fs.readFileSync(__dirname + "/" + file, "utf8");
|
|
|
|
},
|
|
|
|
|
|
|
|
testStart: function() {
|
|
|
|
/* Nothing to do. */
|
|
|
|
},
|
|
|
|
|
|
|
|
testFinish: function(benchmark, test, inputSize, parseTime) {
|
|
|
|
writeResult(test.title, inputSize, parseTime);
|
|
|
|
},
|
|
|
|
|
|
|
|
benchmarkStart: function(benchmark) {
|
|
|
|
writeHeading(benchmark.title);
|
|
|
|
},
|
|
|
|
|
|
|
|
benchmarkFinish: function(benchmark, inputSize, parseTime) {
|
|
|
|
writeSeparator();
|
|
|
|
writeResult(benchmark.title + " total", inputSize, parseTime);
|
|
|
|
},
|
|
|
|
|
|
|
|
start: function() {
|
|
|
|
writeTableHeader();
|
|
|
|
},
|
|
|
|
|
|
|
|
finish: function(inputSize, parseTime) {
|
|
|
|
writeSeparator();
|
|
|
|
writeResult("Total", inputSize, parseTime);
|
|
|
|
writeTableFooter();
|
|
|
|
}
|
|
|
|
});
|