Skip to content

Commit 5ebbc49

Browse files
committed
wasm: Update parseLiquid.js
1 parent 83be542 commit 5ebbc49

1 file changed

Lines changed: 21 additions & 26 deletions

File tree

packages/wasm/scripts/parseLiquid.js

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
55
https://github.com/Shopify/theme-tools/tree/main/packages/prettier-plugin-liquid
66
*/
7+
// Sample usage:
8+
// node scripts/themeParse.js '/Users/pdubroy/dev/third_party/Shopify/dawn/**/*.liquid'
79

810
/* eslint-disable no-console */
911
import fg from 'fast-glob';
@@ -20,12 +22,12 @@ import {wasmMatcherForGrammar} from '../test/_helpers.js';
2022
const __dirname = dirname(fileURLToPath(import.meta.url));
2123
const datadir = join(__dirname, '../test/data');
2224

23-
const liquid = ohm.grammars(readFileSync(join(datadir, '_liquid-html.ohm'), 'utf8'));
25+
const liquid = ohm.grammars(readFileSync(join(datadir, '_liquid-html-mod.ohm'), 'utf8'));
2426

2527
// Get pattern from command line arguments
2628
const pattern = process.argv[2];
2729

28-
function unparse(source, root) {
30+
function unparseW(source, root) {
2931
let pos = 0;
3032
let unparsed = '';
3133
function walk(node) {
@@ -44,8 +46,11 @@ function unparse(source, root) {
4446
const matchWithInput = (m, str) => (m.setInput(str), m.match());
4547

4648
(async function main() {
47-
const times = new Map();
49+
const parsedPaths = new Set();
50+
const jsTimes = [];
51+
const wasmTimes = [];
4852

53+
const m = await wasmMatcherForGrammar(liquid.LiquidHTML);
4954
for (const path of fg.sync(pattern)) {
5055
const input = readFileSync(path, 'utf8');
5156
if (
@@ -58,38 +63,28 @@ const matchWithInput = (m, str) => (m.setInput(str), m.match());
5863
console.log(`skipping ${path}`);
5964
continue;
6065
}
61-
const m = await wasmMatcherForGrammar(liquid.LiquidHTML);
66+
parsedPaths.add(path);
6267
const start = performance.now();
6368
matchWithInput(m, input);
64-
const elapsed = performance.now() - start;
65-
times.set(path, [elapsed]);
66-
if (input.length !== unparse(input, m.getCstRoot()).length) {
67-
console.log('not equal:', path);
68-
}
69+
wasmTimes.push(performance.now() - start);
70+
assert.equal(input.length, unparseW(input, m.getCstRoot()).length);
6971
}
7072

7173
for (const path of fg.sync(pattern)) {
7274
const input = readFileSync(path, 'utf8');
73-
if (!times.has(path)) {
74-
console.log(`skipping ${path}`);
75+
if (!parsedPaths.has(path)) {
7576
continue;
7677
}
7778
const start = performance.now();
78-
const result = liquid.LiquidHTML.match(input);
79-
const elapsed = performance.now() - start;
80-
times.get(path).push(elapsed);
81-
assert.equal(result.succeeded(), true);
79+
const r = liquid.LiquidHTML.match(input);
80+
jsTimes.push(performance.now() - start);
81+
assert.equal(r.succeeded(), true);
8282
}
8383

84-
let min;
85-
let max;
86-
let total = 0;
87-
for (const [path, [wasmTime, ohmTime]] of times) {
88-
const speedup = ohmTime / wasmTime;
89-
min = Math.min(min, speedup);
90-
max = Math.max(max, speedup);
91-
total += speedup;
92-
console.log(`${path}: ${speedup.toFixed(2)}x`);
93-
}
94-
console.log({min, max, avg: total / times.size});
84+
const sum = arr => arr.reduce((a, b) => a + b, 0);
85+
const jsTotal = sum(jsTimes);
86+
const wasmTotal = sum(wasmTimes);
87+
console.log(`JS total: ${jsTotal.toFixed(0)}ms`);
88+
console.log(`Wasm avg: ${wasmTotal.toFixed(0)}ms`);
89+
console.log(`Speedup: ${(jsTotal / wasmTotal).toFixed(2)}x`);
9590
})();

0 commit comments

Comments
 (0)