Artifactspublic-v0

ply

main
ply4 git calls454 ms42 KiB2 cachedprotocol v2
183 lines12 KiBRaw
1// Every regular expression in src/ that is applied to remote, stored or caller text costs time linear in that text.
2//
3// The rows are scripts/regex-inventory.mjs (scripts/regex-gate.mjs makes sure every pattern in src/ has one). Each row's
4// adversarial(n) is run at n, 2n and 4n characters. V8's regex engine exposes no step counter, so there is no
5// step-independent measure to assert on: the assertion is on the ratio of two times measured the same way, never on a
6// time. Four times the input costs about 4 times the work when matching is linear, 16 when it is quadratic and 64 when
7// it is cubic; the bound sits between the first two (BOUND), each time is the best of several, and a row is measured
8// again before it fails (a loaded machine can stretch one sample, not the best of fifteen).
9//
10// A row marked knownBad is asserted the other way round: its generator must show the superlinear cost, so the marker is
11// evidence and not an excuse. When its fix lands the pattern changes, the gate refuses the stale row, and it is rewritten.
12import { spawnSync } from 'node:child_process';
13import { appendFileSync, cpSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
14import { tmpdir } from 'node:os';
15import { join } from 'node:path';
16import { fileURLToPath } from 'node:url';
17import { expect, test } from 'vitest';
18import { gate, shapeProblems } from '../scripts/regex-gate.mjs';
19import { INVENTORY } from '../scripts/regex-inventory.mjs';
21const BOUND = 9; // t(4n) / t(n): linear is about 4 (or less: a fixed-length match is constant), quadratic about 16
22const N = 4000;
23const ATTEMPTS = 3;
24const OPS = { exec: (re, text) => re.exec(text), replace: (re, text) => text.replace(re, ''), split: (re, text) => text.split(re) };
26const regexOf = entry => {
27 if (entry.build !== undefined) return entry.build();
28 const end = entry.pattern.lastIndexOf('/'); // flags never contain a slash
29 return new RegExp(entry.pattern.slice(1, end), entry.pattern.slice(end + 1));
30};
31const ms = () => Number(process.hrtime.bigint()) / 1e6;
32const once = (op, re, text, reps) => {
33 const t0 = ms();
34 for (let r = 0; r < reps; r += 1) { re.lastIndex = 0; op(re, text); }
35 return ms() - t0;
36};
37const best = (op, re, text, reps) => Math.min(...Array.from({ length: 5 }, () => once(op, re, text, reps)));
39export function measure(entry, n = N) {
40 const re = regexOf(entry);
41 const op = OPS[entry.op ?? 'exec'];
42 const [a, b, c] = [n, 2 * n, 4 * n].map(size => entry.adversarial(size));
43 once(op, re, c, 1); // compile and warm up outside the measurement
44 // Repeat the call until the smallest size takes about a millisecond, so the timer and the scheduler are small against it.
45 let reps = 1;
46 while (reps < 8192 && once(op, re, a, reps) < 1) reps *= 2;
47 const t1 = best(op, re, a, reps);
48 const t2 = best(op, re, b, reps);
49 const t4 = best(op, re, c, reps);
50 return { t1, t2, t4, ratio: t4 / t1 };
53const rows = INVENTORY.filter(entry => entry.input !== 'internal');
55test('the inventory has rows to measure, and every one names a known way to apply its pattern', () => {
56 expect(rows.length).toBeGreaterThan(50);
57 for (const entry of rows) {
58 expect(typeof entry.adversarial, `${entry.file}: ${entry.pattern}`).toBe('function');
59 expect(Object.keys(OPS), `${entry.file}: ${entry.pattern}`).toContain(entry.op ?? 'exec');
60 // The generator grows with n: a generator that ignores n would make every ratio 1.
61 expect(entry.adversarial(4 * N).length, `${entry.file}: ${entry.pattern}`).toBeGreaterThanOrEqual(3 * N);
62 }
63});
65for (const entry of rows) {
66 const name = `${entry.file}: ${entry.pattern}`;
67 if (entry.knownBad === undefined) {
68 test(`linear in its ${entry.input} input: ${name}`, () => {
69 const seen = [];
70 for (let attempt = 0; attempt < ATTEMPTS; attempt += 1) {
71 const m = measure(entry);
72 seen.push(m);
73 if (m.ratio < BOUND) return;
74 }
75 expect.fail(`4x the input cost ${seen.map(m => `${m.ratio.toFixed(1)}x`).join(', ')} the time over ${ATTEMPTS} attempts (linear is about 4, the bound is ${BOUND}); n, 2n, 4n took ${seen.map(m => `${m.t1.toFixed(3)} / ${m.t2.toFixed(3)} / ${m.t4.toFixed(3)} ms`).join('; ')}`);
76 });
77 } else {
78 test(`known bad (${entry.knownBad}), and its generator shows it: ${name}`, () => {
79 const seen = [];
80 for (let attempt = 0; attempt < ATTEMPTS; attempt += 1) {
81 const m = measure(entry, N / 2);
82 seen.push(m);
83 if (m.ratio >= BOUND) return;
84 }
85 expect.fail(`marked knownBad, but 4x the input cost only ${seen.map(m => `${m.ratio.toFixed(1)}x`).join(', ')} the time: the generator does not show the defect, or the pattern was fixed and the marker should go`);
86 });
87 }
90// ---- the gate itself ------------------------------------------------------------------------------------------------
91// The second line (shape rules). The refused list is not derived from the rules: the first seven are the patterns an
92// independent reviewer timed at 16 times the input length (linear is about 16): /\/+$/ 477x, /\n+$/u 228x,
93// /[#;].*$/u 438x, /filter .* not (supported|allowed)/ 408x, /(a|a)*$/ 401x, /(a|ab)*c/ 329x, and /(a+)+$/, the
94// textbook exponential one. The eighth is quadratic by hand: on 'x' + n digits + 'z', (\d+) takes all n digits, y
95// fails, and for each of the n lengths (\d+) can give back, (\d*) tries every length of the rest: n(n+1)/2 steps.
96const REFUSED = [
97 [String.raw`\/+$`, ''], [String.raw`\n+$`, 'u'], [String.raw`[#;].*$`, 'u'], ['filter .* not (supported|allowed)', ''],
98 ['(a|a)*$', ''], ['(a|ab)*c', ''], ['(a+)+$', ''], [String.raw`x(\d+)(\d*)y`, ''],
99];
100// Linear by construction: one anchored start and one class run; a fixed count; no run at all; three runs whose
101// classes are pairwise disjoint (name, space, quoted text), so no character can be matched by two of them.
102const CLEAN = [['^[a-z]+$', ''], ['^[0-9a-f]{40}$', ''], [String.raw`\n$`, ''], [String.raw`^\[([^\]\s"]+)(\s+"([^"]*)")?\]$`, 'u']];
104test('the shape rules refuse the patterns measured superlinear and pass the linear controls', () => {
105 for (const [source, flags] of REFUSED) expect(shapeProblems(source, flags).length, `/${source}/${flags}`).toBeGreaterThan(0);
106 for (const [source, flags] of CLEAN) expect(shapeProblems(source, flags), `/${source}/${flags}`).toEqual([]);
107});
109test('the gate fails on a pattern with no row, a row with no pattern, and a known-bad marker left behind', () => {
110 const found = [{ file: 'src/x.ts', line: 7, pattern: '/^[a-z]+$/u', kind: 'literal' }];
111 const entry = { file: 'src/x.ts', pattern: '/^[a-z]+$/u', input: 'caller', linear: 'anchored at ^, one class run', adversarial: n => 'a'.repeat(n) };
112 expect(gate(found, [entry])).toEqual([]);
113 expect(gate(found, [])).toEqual(['src/x.ts:7: /^[a-z]+$/u: not in scripts/regex-inventory.mjs']);
114 expect(gate([], [entry])).toHaveLength(1);
115 expect(gate([], [entry])[0]).toContain('matches no pattern in the source');
116 expect(gate([], [{ ...entry, knownBad: 'fix/some-branch' }])[0]).toContain('its fix landed (fix/some-branch)');
117 // A row cannot claim remote text without a generator, and a refused shape is refused whatever the row says.
118 expect(gate(found, [{ ...entry, adversarial: undefined }])[0]).toContain('needs an adversarial(n) generator');
119 const slash = { file: 'src/x.ts', line: 9, pattern: String.raw`/\/+$/`, kind: 'literal' };
120 expect(gate([slash], [{ ...entry, pattern: slash.pattern }])[0]).toContain('unanchored run');
121});
123// The gate's discovery. A pattern the gate does not find gets no row, no generator, no ratio test and no shape check,
124// so every way src/ can build a RegExp other than a literal, RegExp(...) or new RegExp(...) is refused. Each form is
125// planted alone in a scratch copy of this tree's src/ and the gate is run on it the way npm run lint runs it: a child
126// process, judged by its exit code (0 clean, 1 on any problem) and by a message naming the planted file and line. The
127// lines are counted by hand from the planted text below; every pattern is the textbook exponential (x+)+$.
128const PLANT = 'src/zz-planted.ts';
129const PLANTED = [
130 // (a) a construction whose callee is not the bare identifier RegExp
131 ['new globalThis.RegExp(...)', "export const r = new globalThis.RegExp('(c+)+$', 'u');\n", 1],
132 ['globalThis.RegExp(...)', "export const r = globalThis.RegExp('(c+)+$', 'u');\n", 1],
133 ['new (RegExp)(...)', "export const r = new (RegExp)('(i+)+$', 'u');\n", 1],
134 ["new globalThis['RegExp'](...)", "export const r = new globalThis['RegExp']('(j+)+$', 'u');\n", 1],
135 // (b) RegExp as a value that is not the callee of a construction
136 ['an alias of RegExp', "const R = RegExp;\nexport const r = new R('(d+)+$', 'u');\n", 1],
137 ['Reflect.construct(RegExp, ...)', "export const r: RegExp = Reflect.construct(RegExp, ['(g+)+$', 'u']);\n", 1],
138 ['class extends RegExp', "export class R extends RegExp {}\nexport const r = new R('(h+)+$', 'u');\n", 1],
139 // (c) String#match, matchAll and search compile an argument that is not a RegExp
140 ['match of a string', "export const f = (s: string): RegExpMatchArray | null =>\n s.match('(e+)+$');\n", 2],
141 ['matchAll of a string', "export const f = (s: string): unknown => s.matchAll('(e+)+$');\n", 1],
142 ['search of a string', "export const f = (s: string): number => s.search('(f+)+$');\n", 1],
143 ['match of a variable', "const p = '(k+)+$';\n\nexport const f = (s: string): unknown => s.match(p);\n", 3],
144 // the Cache API allowance is src/packcache.ts and the receiver `cache`: the same text in another file is refused
145 ['cache.match(key) outside src/packcache.ts', "export const f = (cache: string, key: string): unknown => cache.match(key);\n", 1],
146];
148test('the gate exits 1 on every way to build a RegExp it cannot inventory, naming the file and line', { timeout: 240_000 }, () => {
149 const repo = fileURLToPath(new URL('..', import.meta.url));
150 const root = realpathSync(mkdtempSync(join(tmpdir(), 'ply-regex-gate-')));
151 const run = () => spawnSync(process.execPath, [join(repo, 'scripts/regex-gate.mjs'), '--root', root], { cwd: repo, encoding: 'utf8' });
152 try {
153 cpSync(join(repo, 'src'), join(root, 'src'), { recursive: true });
154 for (const name of ['tsconfig.json', 'tsconfig.check.json']) cpSync(join(repo, name), join(root, name));
155 const clean = run();
156 expect(clean.status, `unplanted tree: ${clean.stderr}`).toBe(0);
157 for (const [name, text, line] of PLANTED) {
158 writeFileSync(join(root, PLANT), text);
159 const planted = run();
160 expect(planted.status, `${name}: ${planted.stdout}${planted.stderr}`).toBe(1);
161 expect(planted.stderr, name).toContain(`regex-gate: ${PLANT}:${line}: `);
162 rmSync(join(root, PLANT));
163 }
164 // The allowance is not the file alone: another receiver in src/packcache.ts is refused, at the line appended.
165 const cacheFile = join(root, 'src/packcache.ts');
166 const before = readFileSync(cacheFile, 'utf8');
167 const appendedLine = before.split('\n').length; // the file ends with a newline: the appended text is the next line
168 appendFileSync(cacheFile, "export const zz = (s: string): unknown => s.match('(a+)+$');\n");
169 const other = run();
170 expect(other.status, other.stdout + other.stderr).toBe(1);
171 expect(other.stderr).toContain(`regex-gate: src/packcache.ts:${appendedLine}: `);
172 writeFileSync(cacheFile, before);
173 // Control: a regex literal argument is not refused as a string pattern. It is found as a literal, and the only
174 // problem is the missing row.
175 writeFileSync(join(root, PLANT), 'export const f = (s: string): unknown => s.match(/^zz$/u);\n');
176 const literal = run();
177 expect(literal.status).toBe(1);
178 expect(literal.stderr.split('\n').filter(l => l.includes(PLANT))).toEqual([`regex-gate: ${PLANT}:1: /^zz$/u: not in scripts/regex-inventory.mjs`]);
179 rmSync(join(root, PLANT));
180 const again = run();
181 expect(again.status, `tree restored: ${again.stderr}`).toBe(0);
182 } finally { rmSync(root, { recursive: true, force: true }); }
183});
Diagnostics4 git calls · 454 ms
CallStatusSizeTime0454 ms
discover200152 B4 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200145 B · hit4 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=4
git.response.bytes=145
git.response.cache=HIT
git.response.duration_ms=4
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=145
git.pkt.payload.max=76
git.ls_refs.count=2
git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020038 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=447
git.response.bytes=38434
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=6
git.pkt.data=4
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=38434
git.pkt.payload.max=38339
git.sideband.band1.bytes=38338
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=63
git.pack.bytes=38338
git.pack.ofs_delta=0
git.pack.ref_delta=8
git.pack.chain.max=8
git.pack.trailer=0bc36ab7adf82d3109177f11f1ea93f1ce071f77
git.pack.full=55
Open pack map
fetch2004.4 KiB447 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=4520
git.response.cache=MISS
git.response.duration_ms=447
git.fetch.wants=7c9743b1acca51ae7a3e4de014e0ca357c93ab47
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=4520
git.pkt.payload.max=4499
git.sideband.band1.bytes=4498
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply.git
Ref
refs/heads/main
Commit
19912563f540228593df0c489082e5e2508e21e4
cf-ray
a5e28b841ebfbd4a
Request
0ae037bb…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format