main
1// readConfig drops a `#` or `;` comment from each line of a stored .git/config in time linear in the line.
2//
3// The oracle for WHAT is dropped is the pattern main (ec337c5) applied, `/[#;].*$/u`, run here by the regex engine on
4// short lines where its cost does not matter: the comment starts at the first `#` or `;` that has no LF, CR, U+2028 or
5// U+2029 after it (`.` stops at those, and `$` without the m flag is only the end of the line). The oracle for the COST
6// is a ratio of two times measured the same way, never a time.
7import { test } from 'vitest';
8import assert from 'node:assert/strict';
9import { configCommentStart } from '../src/transport.js';
10import { readConfig } from '../src/repo-node.js';
12test('configCommentStart cuts exactly where main\'s pattern cut, for every line of up to 5 characters over an 8-letter alphabet', () => {
13 const alphabet = ['a', ' ', '#', ';', '\n', '\r', '\u2028', '\u2029'];
14 let checked = 0;
15 const walk = (line, left) => {
16 assert.equal(line.slice(0, configCommentStart(line)), line.replace(/[#;].*$/u, ''), JSON.stringify(line));
17 checked += 1;
18 if (left > 0) for (const c of alphabet) walk(line + c, left - 1);
19 };
20 walk('', 5);
21 assert.equal(checked, (8 ** 6 - 1) / 7); // 1 + 8 + ... + 8^5 lines: the walk reached every one
22 // An astral character and a lone surrogate are one `.` each under the u flag: neither ends a comment.
23 for (const line of ['k = v # \u{1F600} x', 'k = v ; \uD83D x', '# \uDE00\r; y', 'k = "a#b"']) assert.equal(line.slice(0, configCommentStart(line)), line.replace(/[#;].*$/u, ''), JSON.stringify(line));
24});
26test('readConfig: values main read are unchanged, with LF and with CRLF line ends', () => {
27 // Hand-derived from the rule above. With CRLF the CR ends the line after the comment, so main kept the comment in the
28 // value (git itself drops it); this change keeps main's reading and only bounds its cost.
29 const lf = readConfig('[core]\n\tbare = false ; comment\n# [x]\n[Extensions]\n\tObjectFormat = sha256 # why\n');
30 assert.deepEqual([...lf], [['core.bare', 'false'], ['extensions.objectformat', 'sha256']]);
31 const crlf = readConfig('[core]\r\n\tbare = false ; comment\r\n# [x]\r\n[Extensions]\r\n\tObjectFormat = sha256\r\n');
32 assert.deepEqual([...crlf], [['core.bare', 'false ; comment'], ['extensions.objectformat', 'sha256']]);
33});
35test('readConfig: a CRLF line of 64x the comment characters costs under 300x the time (linear is 64x, quadratic 4096x)', () => {
36 // One call on a line of 64n `#` against 64 calls on a line of n: linear work is equal, quadratic work is 64 times
37 // more. Best of 5 each, so load only ever narrows the ratio's noise, never widens it.
38 const N = 500;
39 const small = `[core]\n${'#'.repeat(N)}\r\n`;
40 const big = `[core]\n${'#'.repeat(64 * N)}\r\n`;
41 const best = (fn) => { let b = Infinity; for (let i = 0; i < 5; i++) { const t0 = performance.now(); fn(); b = Math.min(b, performance.now() - t0); } return b; };
42 const smallMs = best(() => { for (let i = 0; i < 64; i++) readConfig(small); }) / 64;
43 const bigMs = best(() => readConfig(big));
44 assert.ok(bigMs < 300 * smallMs, `64x the comment characters cost ${(bigMs / smallMs).toFixed(0)}x the time (${bigMs.toFixed(3)} ms against ${smallMs.toFixed(5)} ms)`);
45});
Diagnostics4 git calls · 705 ms
CallStatusSizeTime0705 ms
discover200152 B3 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 · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=3 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 · hit7 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=695 git.response.bytes=38434 git.response.cache=HIT git.response.duration_ms=7 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=55Open pack map
fetch2001.5 KiB695 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1518 git.response.cache=MISS git.response.duration_ms=695 git.fetch.wants=74ee15b1ae522eb33a1dbc999efc5cf7ccb73b2c 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=1518 git.pkt.payload.max=1497 git.sideband.band1.bytes=1496 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
- a5e2bf72ca2c89da
- Request
- c07c24e5…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format