-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathparseLiquid.ts
More file actions
203 lines (182 loc) · 6.28 KB
/
Copy pathparseLiquid.ts
File metadata and controls
203 lines (182 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
This script is meant to exercise the LiquidHTML grammar in a similar way
to Shopify's Prettier plugin. It doesn't really do anything useful :-)
https://github.com/Shopify/theme-tools/tree/main/packages/prettier-plugin-liquid
*/
// Sample usage:
// node scripts/parseLiquid.js '/Users/pdubroy/dev/third_party/Shopify/dawn/**/*.liquid'
/* eslint-disable no-console */
import fg from 'fast-glob';
import {readFileSync} from 'node:fs';
import {join, dirname} from 'node:path';
import process from 'node:process';
import {fileURLToPath} from 'node:url';
import * as ohm from 'ohm-js-legacy';
import {Bench} from 'tinybench';
import {Grammar} from 'ohm-js';
import {compileGrammars} from '../src/api.ts';
import {unparse} from '../test/_helpers.ts';
import {createReader} from '../../runtime/src/cstReader.ts';
const __dirname = dirname(fileURLToPath(import.meta.url));
const datadir = join(__dirname, '../test/data');
const grammarSource = readFileSync(join(datadir, 'liquid-html.ohm'), 'utf8');
const liquid = ohm.grammars(grammarSource);
// Parse flags and positional args.
const flags = new Set(process.argv.slice(2).filter(a => a.startsWith('--')));
const positionalArgs = process.argv.slice(2).filter(a => !a.startsWith('--'));
// An option that makes it easy to run this script in CI.
// https://matklad.github.io/2024/03/22/basic-things.html
const smallSize = flags.has('--small-size');
const includeUnparse = flags.has('--include-unparse');
const useCstReader = flags.has('--cst-reader');
const useCstReaderPacked = flags.has('--cst-reader-packed');
// Get pattern from command line arguments
const pattern = positionalArgs[0];
(async function main() {
// Pre-read all files.
const files = [];
for (const path of fg.sync(pattern)) {
const input = readFileSync(path, 'utf8');
files.push({path, input});
if (smallSize) break;
}
const bench = new Bench({
iterations: 3,
time: 0,
warmup: false,
throws: true,
});
const opts = {
afterEach() {
process.stderr.write('.');
},
};
// Walk v17 CST, collecting terminal text.
function unparseV17(matchResult, input) {
let ans = '';
function walk(node, pos) {
if (node.isTerminal()) {
ans += input.slice(pos, pos + node.matchLength);
return;
}
for (let i = 0; i < node.children.length; i++) {
walk(node.children[i], pos + node.childOffsets[i]);
}
}
walk(matchResult._cst, matchResult._cstOffset);
return ans;
}
// Compile to Wasm.
const compileStart = bench.now();
const allBytes = compileGrammars(grammarSource);
const compileTime = bench.now() - compileStart;
const modBytes = allBytes.LiquidHTML;
const g = await Grammar.instantiate(modBytes);
const {exports} = g._instance;
let peakWasmHeapBytes = 0;
let peakWasmMemoryBytes = 0;
bench.add(
includeUnparse ? 'JS parse+unparse' : 'JS parse',
() => {
let overriddenDuration = 0;
for (const {input} of files) {
const start = bench.now();
const r = liquid.LiquidHTML.match(input);
if (!includeUnparse) overriddenDuration += bench.now() - start;
unparseV17(r, input);
if (includeUnparse) overriddenDuration += bench.now() - start;
}
return {overriddenDuration};
},
opts
);
// Walk CST using CstReader (raw handles), collecting terminal text.
function unparseCstReaderRaw(matchResult) {
const reader = createReader(matchResult);
const inp = reader.input;
let ans = '';
function walk(handle, startIdx) {
if (reader.isTerminal(handle)) {
ans += inp.slice(startIdx, startIdx + reader.matchLength(handle));
return;
}
reader.forEachChild(
handle,
(child, _leadingSpaces, offset) => {
walk(child, startIdx + offset);
},
startIdx
);
}
walk(reader.rootHandle, reader.rootStartIdx);
return ans;
}
// Walk CST using CstReader (handles with startIdx), collecting terminal text.
function unparseCstReaderPacked(matchResult) {
const reader = createReader(matchResult, {packStartIdx: true});
let ans = '';
function walk(handle) {
if (reader.isTerminal(handle)) {
ans += reader.sourceString(handle);
return;
}
reader.forEachChild(handle, (child, _leadingSpaces) => {
walk(child);
});
}
walk(reader.root);
return ans;
}
const wasmLabel = includeUnparse ? 'Wasm parse+unparse' : 'Wasm parse';
bench.add(
useCstReaderPacked
? `${wasmLabel} (CstReader packed)`
: useCstReader
? `${wasmLabel} (CstReader)`
: wasmLabel,
() => {
let overriddenDuration = 0;
for (const {input} of files) {
const start = bench.now();
const m = g.match(input);
if (!includeUnparse) overriddenDuration += bench.now() - start;
m.use(() => {
// Capture memory stats before dispose() resets the heap.
peakWasmHeapBytes = Math.max(
peakWasmHeapBytes,
exports.__offset.value - exports.__heap_base.value
);
peakWasmMemoryBytes = Math.max(
peakWasmMemoryBytes,
exports.memory.buffer.byteLength
);
return useCstReaderPacked
? unparseCstReaderPacked(m)
: useCstReader
? unparseCstReaderRaw(m)
: unparse(g);
});
if (includeUnparse) overriddenDuration += bench.now() - start;
}
return {overriddenDuration};
},
opts
);
await bench.run();
process.stderr.write('\n');
const fmt = bytes => {
if (bytes >= 1024 * 1024) return `${(bytes / 1024 / 1024).toFixed(2)} MB`;
return `${(bytes / 1024).toFixed(1)} KB`;
};
for (const task of bench.tasks) {
const {mean, sd, samplesCount} = task.result.latency;
console.log(`${task.name}: ${mean.toFixed(0)}ms ± ${sd.toFixed(0)}ms (n=${samplesCount})`);
}
const jsParse = bench.tasks[0].result.latency.mean;
const wasmParse = bench.tasks[1].result.latency.mean;
console.log(`Speedup: ${(jsParse / wasmParse).toFixed(2)}x`);
console.log(`Compile: ${compileTime.toFixed(0)}ms`);
console.log(`Compiled grammar size: ${fmt(modBytes.length)}`);
console.log(`Peak Wasm heap usage: ${fmt(peakWasmHeapBytes)}`);
console.log(`Peak Wasm linear memory: ${fmt(peakWasmMemoryBytes)}`);
})();