8000 Avoid double extraction of substrings in various MATCH_ bytecodes by markw65 · Pull Request #427 · peggyjs/peggy · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Avoid double extraction of substrings in various MATCH_ bytecodes #427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Released: TBD

### Minor Changes

- [#427](https://github.com/peggyjs/peggy/pull/427) Avoid double extraction of
substrings in various MATCH_ bytecodes
- [#425](https://github.com/peggyjs/peggy/pull/425) Add a pass to simplify single-character choices
- [#420](https://github.com/peggyjs/peggy/pull/420) Updated dependencies to
avoid audit warnings. From @hildjj.
Expand Down
2 changes: 1 addition & 1 deletion docs/js/benchmark-bundle.min.js

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions docs/js/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,8 @@ function peg$parse(input, options) {
var s0, s1, s2;

s0 = peg$currPos;
if (input.substr(peg$currPos, 3).toLowerCase() === peg$c0) {
s1 = input.substr(peg$currPos, 3);
s1 = input.substr(peg$currPos, 3);
if (s1.toLowerCase() === peg$c0) {
peg$currPos += 3;
} else {
s1 = peg$FAILED;
Expand Down Expand Up @@ -468,8 +468,8 @@ function peg$parse(input, options) {
var s0, s1, s2;

s0 = peg$currPos;
if (peg$r0.test(input.charAt(peg$currPos))) {
s1 = input.charAt(peg$currPos);
s1 = input.charAt(peg$currPos);
if (peg$r0.test(s1)) {
peg$currPos++;
} else {
s1 = peg$FAILED;
Expand All @@ -491,8 +491,8 @@ function peg$parse(input, options) {
var s0, s1, s2;

s0 = peg$currPos;
if (peg$r1.test(input.charAt(peg$currPos))) {
s1 = input.charAt(peg$currPos);
s1 = input.charAt(peg$currPos);
if (peg$r1.test(s1)) {
peg$currPos++;
} else {
s1 = peg$FAILED;
Expand Down Expand Up @@ -597,8 +597,8 @@ function peg$parse(input, options) {
s0 = peg$currPos;
s1 = [];
s2 = peg$currPos;
if (peg$r2.test(input.charAt(peg$currPos))) {
s3 = input.charAt(peg$currPos);
s3 = input.charAt(peg$currPos);
if (peg$r2.test(s3)) {
peg$currPos++;
} else {
s3 = peg$FAILED;
Expand Down Expand Up @@ -626,8 +626,8 @@ function peg$parse(input, options) {
while (s2 !== peg$FAILED) {
s1.push(s2);
s2 = peg$currPos;
if (peg$r2.test(input.charAt(peg$currPos))) {
s3 = input.charAt(peg$currPos);
s3 = input.charAt(peg$currPos);
if (peg$r2.test(s3)) {
peg$currPos++;
} else {
s3 = peg$FAILED;
Expand Down Expand Up @@ -912,8 +912,8 @@ function peg$parse(input, options) {
s0 = peg$currPos;
s1 = peg$currPos;
s2 = [];
if (peg$r2.test(input.charAt(peg$currPos))) {
s3 = input.charAt(peg$currPos);
s3 = input.charAt(peg$currPos);
if (peg$r2.test(s3)) {
peg$currPos++;
} else {
s3 = peg$FAILED;
Expand All @@ -922,8 +922,8 @@ function peg$parse(input, options) {
if (s3 !== peg$FAILED) {
while (s3 !== peg$FAILED) {
s2.push(s3);
if (peg$r2.test(input.charAt(peg$currPos))) {
s3 = input.charAt(peg$currPos);
s3 = input.charAt(peg$currPos);
if (peg$r2.test(s3)) {
peg$currPos++;
} else {
s3 = peg$FAILED;
Expand Down Expand Up @@ -968,8 +968,8 @@ function peg$parse(input, options) {
s0 = peg$currPos;
s1 = peg$currPos;
s2 = [];
if (peg$r2.test(input.charAt(peg$currPos))) {
s3 = input.charAt(peg$currPos);
s3 = input.charAt(peg$currPos);
if (peg$r2.test(s3)) {
peg$currPos++;
} else {
s3 = peg$FAILED;
Expand All @@ -978,8 +978,8 @@ function peg$parse(input, options) {
if (s3 !== peg$FAILED) {
while (s3 !== peg$FAILED) {
s2.push(s3);
if (peg$r2.test(input.charAt(peg$currPos))) {
s3 = input.charAt(peg$currPos);
s3 = input.charAt(peg$currPos);
if (peg$r2.test(s3)) {
peg$currPos++;
} else {
s3 = peg$FAILED;
Expand Down Expand Up @@ -1067,8 +1067,8 @@ function peg$parse(input, options) {

s0 = peg$currPos;
s1 = peg$currPos;
if (input.substr(peg$currPos, 3).toLowerCase() === peg$c5) {
s2 = input.substr(peg$currPos, 3);
s2 = input.substr(peg$currPos, 3);
if (s2.toLowerCase() === peg$c5) {
peg$currPos += 3;
} else {
s2 = peg$FAILED;
Expand Down Expand Up @@ -1442,8 +1442,8 @@ function peg$parse(input, options) {
var s0, s1, s2;

s0 = peg$currPos;
if (peg$r3.test(input.charAt(peg$currPos))) {
s1 = input.charAt(peg$currPos);
s1 = input.charAt(peg$currPos);
if (peg$r3.test(s1)) {
peg$currPos++;
} else {
s1 = peg$FAILED;
Expand Down
2 changes: 1 addition & 1 deletion docs/js/test-bundle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/vendor/peggy/peggy.min.js

Large diffs are not rendered by default.

106 changes: 83 additions & 23 deletions lib/compiler/passes/generate-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ function generateJS(ast, options) {
const parts = [];
let value = undefined;

function compileCondition(cond, argCount) {
function compileCondition(cond, argCount, thenFn) {
const baseLength = argCount + 3;
const thenLength = bc[ip + baseLength - 2];
const elseLength = bc[ip + baseLength - 1];
Expand All @@ -331,7 +331,7 @@ function generateJS(ast, options) {
ip,
() => {
ip += baseLength;
thenCode = compile(bc.slice(ip, ip + thenLength));
thenCode = (thenFn || compile)(bc.slice(ip, ip + thenLength));
ip += thenLength;
},
(elseLength > 0)
Expand All @@ -351,6 +351,59 @@ function generateJS(ast, options) {
parts.push("}");
}

/*
MATCH_* opcodes typically do something like

if (<test>(input.substr(peg$currPos, length))) {
sN = input.substr(peg$currPos, length);
...
} else {
sN = peg$FAILED;
...
}

compileInputChunkCondition will convert that to

sN = input.substr(peg$currPos, length);
if (<test>(sN)) {
...
} else {
sN = peg$FAILED;
...
}

and avoid extracting the sub string twice.
*/
function compileInputChunkCondition(
condFn, argCount, inputChunkLength
) {
const baseLength = argCount + 3;
let inputChunk = inputChunkLength === 1
? "input.charAt(peg$currPos)"
: "input.substr(peg$currPos, " + inputChunkLength + ")";
let thenFn = null;
if (bc[ip + baseLength] === op.ACCEPT_N
&& bc[ip + baseLength + 1] === inputChunkLength) {
// Push the assignment to the next available variable.
parts.push(stack.push(inputChunk));
inputChunk = stack.pop();
thenFn = bc => {
// The bc[0] is an ACCEPT_N, and bc[1] is the N. We've already done
// the assignment (before the if), so we just need to bump the
// stack, and increment peg$currPos appropriately.
stack.sp++;
const code = compile(bc.slice(2));
code.unshift(
inputChunkLength === 1
? "peg$currPos++;"
: "peg$currPos += " + inputChunkLength + ";"
);
return code;
};
}
compileCondition(condFn(inputChunk, thenFn !== null), argCount, thenFn);
}

function compileLoop(cond) {
const baseLength = 2;
const bodyLength = bc[ip + baseLength - 1];
Expand Down Expand Up @@ -504,35 +557,42 @@ function generateJS(ast, options) {
compileCondition("input.length > peg$currPos", 0);
break;

case op.MATCH_STRING: // MATCH_STRING s, a, f, ...
compileCondition(
ast.literals[bc[ip + 1]].length > 1
? "input.substr(peg$currPos, "
+ ast.literals[bc[ip + 1]].length
+ ") === "
+ l(bc[ip + 1])
: "input.charCodeAt(peg$currPos) === "
+ ast.literals[bc[ip + 1]].charCodeAt(0),
1
case op.MATCH_STRING: { // MATCH_STRING s, a, f, ...
const litNum = bc[ip + 1];
const literal = ast.literals[litNum];
compileInputChunkCondition(
(inputChunk, optimized) => {
if (literal.length > 1) {
return `${inputChunk} === ${l(litNum)}`;
}
inputChunk = !optimized
? "input.charCodeAt(peg$currPos)"
: `${inputChunk}.charCodeAt(0)`;
return `${inputChunk} === ${literal.charCodeAt(0)}`;
},
1,
literal.length
);
break;
}

case op.MATCH_STRING_IC: // MATCH_STRING_IC s, a, f, ...
compileCondition(
"input.substr(peg$currPos, "
+ ast.literals[bc[ip + 1]].length
+ ").toLowerCase() === "
+ l(bc[ip + 1]),
1
case op.MATCH_STRING_IC: { // MATCH_STRING_IC s, a, f, ...
const litNum = bc[ip + 1];
compileInputChunkCondition(
inputChunk => `${inputChunk}.toLowerCase() === ${l(litNum)}`,
1,
ast.literals[litNum].length
);
break;
}

case op.MATCH_CHAR_CLASS: // MATCH_CHAR_CLASS c, a, f, ...
compileCondition(
r(bc[ip + 1]) + ".test(input.charAt(peg$currPos))",
1
case op.MATCH_CHAR_CLASS: { // MATCH_CHAR_CLASS c, a, f, ...
const regNum = bc[ip + 1];
compileInputChunkCondition(
inputChunk => `${r(regNum)}.test(${inputChunk})`, 1, 1
);
break;
}

case op.ACCEPT_N: // ACCEPT_N n
parts.push(stack.push(
Expand Down
Loading
0