main
ply4 git calls255 ms42 KiB2 cachedprotocol v2
46 lines3.0 KiBRaw
1// Micro-benchmark of the loopback's per-exchange key work (classify + base64 + keyHash) on a v2 fetch body, against
2// the previous implementation inlined here. Run: node --experimental-strip-types --import ./test/register-tsx.mjs bench/gitcache-key.bench.ts
3import { classify, keyHash } from '../src/gitcache.ts';
5const pkt = (s: string) => (s.length + 4).toString(16).padStart(4, '0') + s;
6const oid = (i: number) => i.toString(16).padStart(40, 'a');
7const body = new TextEncoder().encode(`${pkt('command=fetch\n')}${pkt('agent=ply/1\n')}0001${pkt('thin-pack\n')}${pkt('ofs-delta\n')}${pkt('filter tree:0\n')}${pkt(`want ${oid(1)}\n`)}${pkt('deepen 10\n')}${pkt('done\n')}0000`);
8const url = 'https://github.com/ivarvong/ply/git-upload-pack';
10// Previous implementation (65a7c76).
11const oldLatin1 = (b: Uint8Array) => { let s = ''; for (let i = 0; i < b.length; i++) s += String.fromCharCode(b[i] as number); return s; };
12function oldClassify(method: string, u: string, b: Uint8Array | null) {
13 if (method.toUpperCase() === 'GET') return u.endsWith('/info/refs?service=git-upload-pack') && !b ? 'discover' : null;
14 if (!b) return null;
15 const t = oldLatin1(b);
16 const has = (re: RegExp) => re.test(t);
17 if (has(/[0-9a-f]{4}command=ls-refs\n/)) return 'refs';
18 if (!has(/[0-9a-f]{4}command=fetch\n/)) return null;
19 if (has(/[0-9a-f]{4}have [0-9a-f]{40}\n/) || has(/[0-9a-f]{4}want-ref /) || has(/[0-9a-f]{4}deepen-(since|not) /) || has(/[0-9a-f]{4}shallow [0-9a-f]{40}\n/)) return null;
20 return has(/[0-9a-f]{4}want [0-9a-f]{40}\n/) ? 'pack' : null;
22async function oldKeyHash(kind: string, u: string, p: string, b: Uint8Array | null) {
23 const head = new TextEncoder().encode(`ply-gitcache\n${kind}\n${u}\n${p}\n`);
24 const all = new Uint8Array(head.length + (b === null ? 0 : b.length));
25 all.set(head);
26 if (b) all.set(b, head.length);
27 const d = new Uint8Array(await crypto.subtle.digest('SHA-256', all));
28 let hex = '';
29 for (const x of d) hex += x.toString(16).padStart(2, '0');
30 return hex;
32const latin1 = (b: Uint8Array) => { let s = ''; for (let i = 0; i < b.length; i += 1024) s += String.fromCharCode.apply(null, b.subarray(i, i + 1024) as unknown as number[]); return s; };
34async function oldPath() { const k = oldClassify('POST', url, body)!; const h = await oldKeyHash(k, url, 'version=2', body); return h.length + btoa(oldLatin1(body)).length; }
35async function newPath() { const t = latin1(body); const k = classify('POST', url, body)!; void t; const h = await keyHash(k, url, 'version=2', body); return h.length + btoa(t).length; }
37if ((await oldKeyHash('pack', url, 'version=2', body)) !== (await keyHash('pack', url, 'version=2', body))) throw new Error('key changed');
39async function run(name: string, fn: () => Promise<number>, n: number) {
40 for (let i = 0; i < 2000; i++) await fn();
41 const t0 = performance.now();
42 for (let i = 0; i < n; i++) await fn();
43 const us = ((performance.now() - t0) * 1000) / n;
44 console.log(`${name}: ${us.toFixed(2)} us/exchange`);
46for (let r = 0; r < 3; r++) { await run('old', oldPath, 20000); await run('new', newPath, 20000); }
Diagnostics4 git calls · 255 ms
CallStatusSizeTime0255 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
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 1020040 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=41349
git.response.cache=HIT
git.response.duration_ms=2
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=526b305930b8ef382f2d7a1f9c0238730a585921
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=41349
git.pkt.payload.max=41254
git.sideband.band1.bytes=41253
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=61
git.pack.bytes=41253
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c
git.pack.full=61
Open pack map
fetch2001.3 KiB250 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=250
git.response.bytes=1376
git.response.cache=MISS
git.response.duration_ms=250
git.fetch.wants=7ffd738b6e0cfa531a815122bc34b72bb6227952
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=1376
git.pkt.payload.max=1355
git.sideband.band1.bytes=1354
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-web.git
Ref
refs/heads/main
Commit
526b305930b8ef382f2d7a1f9c0238730a585921
cf-ray
a5e2cab14d154b67
Request
5eb10938…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format