Merge perf/6-routing-design: routing keeps its structural tables and runs as small functions, about half the CPU, identical results (347 fuzzed games)

# Conflicts:
#	CHANGELOG.md
This commit is contained in:
Frederic Jung 2026-11-01 21:57:08 +01:00
commit add3d67e48
No known key found for this signature in database
12 changed files with 717 additions and 59 deletions

87
tests/route-cache.test.js Normal file
View file

@ -0,0 +1,87 @@
// What compute() keeps between calls (src/game/80-simulation/29-route-cache.js): the shortest paths and the exit lists,
// rebuilt only when their key changes. These tests make sure every kind of change that can matter changes the key (a
// stale table would route yesterday's network), that demand alone does not (or nothing is gained), and that routing
// with the kept tables is exactly routing without them.
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { load } = require('./harness');
const SAVE = fs.readFileSync(path.join(__dirname, 'fixtures', 'big-network.json'), 'utf8');
function open() {
const h = load({ seed: 777 });
h.ev(`S=migrate(JSON.parse(${JSON.stringify(SAVE)}));UI.started=true;UI.pitched=true;UI.speed=0;closeModal();S.nextFail=S.day+999;S.nextEvent=S.day+999;compute();compute();`);
return h;
}
// Applies a change, routes once with the kept tables and once without (from the same yesterday), and says which keys moved.
function step(ev, change) {
ev(change);
return JSON.parse(ev(`(()=>{const gk=RC.gk,xk=RC.xk,prior=R;compute();const a=JSON.stringify(R)+JSON.stringify(R.fl);
const g=RC.gk!==gk,x=RC.xk!==xk;R=prior;RC.on=false;try{compute();}finally{RC.on=true;}const b=JSON.stringify(R)+JSON.stringify(R.fl);
return JSON.stringify({g,x,same:a===b,len:a.length});})()`));
}
test('a change to links, POPs, ports, peering, events or RouteScript rebuilds what routing keeps; demand alone does not', () => {
const { ev, errors } = open();
assert.ok(ev('RC.gk!==null&&RC.xk!==null'), 'routing keeps its tables after a compute');
// [what changes, the graph key moves, the exits key moves]
const changes = [
// Demand and capacity only: nothing structural, the kept tables are used.
['for(const c in S.pops)if(S.pops[c].bb)S.pops[c].bb.d*=1.07', false, false],
['S.biz.forEach(b=>b.bw*=1.1)', false, false],
["S.links.find(l=>l.kind==='wave').tier=1", false, false],
['S.transit[0].tier=Math.max(0,S.transit[0].tier-1)', false, false],
['S.day++', false, false],
// The graph: a cut, a link gone, a link back, a metric, a POP down and up again, RouteScript's avoid.
['S.links[3].cut=S.day+3', true, true],
['S.links[3].cut=0', true, true],
['window.__l=S.links.splice(7,1)[0]', true, true],
['S.links.push(__l)', true, true],
["S.pairMet=S.pairMet||{};{const l=S.links[10];S.pairMet[l.a<l.b?l.a+'|'+l.b:l.b+'|'+l.a]='avoid';}", true, true],
['S.pops.sea.down=S.day+3', true, true],
['S.pops.sea.down=0', true, true],
// The exits: transit ports, PNIs, caches, exchanges, sessions.
["S.transit[2].pref='low'", false, true],
["S.transit[2].pref='high'", false, true],
['S.transit[5].carry=true', false, true],
['S.transit[4].down=S.day+3', false, true],
['S.transit.splice(1,1)', false, true],
["S.pni[0].pref='high'", false, true],
['S.pni.splice(10,25)', false, true],
['S.caches.splice(0,30)', false, true],
["S.ixPref={};S.ixPref[Object.keys(S.ixp)[1]]='low'", false, true],
['S.ev.ixdown={};S.ev.ixdown[Object.keys(S.ixp)[2]]=S.day+3', false, true],
['delete S.rs[Object.keys(S.rs)[0]]', false, true],
['delete S.ixp[Object.keys(S.ixp)[3]]', false, true],
// Events that change who reaches whom, and RouteScript.
["S.ev.rpki={nets:['ggl','nfx'],until:S.day+3}", false, true],
['S.ev.depeer=S.day+3', false, true],
["S.ev.leak={net:'fbk',until:S.day+3}", false, true],
["S.te={on:true,day:0,script:'prefer transit '+NET[S.transit[0].net].name+'\\nspill at 80%'};S.core={lv:2,run:2,legacy:true}", false, true],
["S.te.script+='\\nprefer pni for goggle'", false, true],
// avoid link: the edge's weight, so the graph key moves too.
["{const l=S.links.find(x=>!linkDown(x));S.te.script+='\\navoid link '+CITY[l.a].name.toLowerCase()+'-'+CITY[l.b].name.toLowerCase();}", true, true],
];
for (const [change, g, x] of changes) {
const r = step(ev, change);
assert.ok(r.same, `${change}: routing with the kept tables is routing without them`);
if (g !== null) assert.equal(r.g, g, `${change}: the graph key ${g ? 'moves' : 'stays'}`);
assert.equal(r.x, x, `${change}: the exits key ${x ? 'moves' : 'stays'}`);
assert.ok(r.len > 1e5, 'a big network was routed');
}
assert.deepEqual(errors, []);
});
test('days in a row reuse the kept tables and route like a fresh start every day', () => {
const { ev, errors } = open();
for (let d = 0; d < 4; d++) {
ev('dayTick()');
const r = step(ev, '');
assert.ok(r.same, `day ${d}: the same routing with and without the kept tables`);
assert.ok(!r.g && !r.x, `day ${d}: a day of demand only keeps the tables`);
}
assert.deepEqual(errors, []);
});

30
tests/route-fuzz.test.js Normal file
View file

@ -0,0 +1,30 @@
// A few seeded random games (scripts/lib/route-fuzz.js), played move for move on main's page and on this checkout's:
// after every move and day both route identically and keep the same game state, and this checkout routes the same with
// the routing cache (RC) on and off. scripts/route-fuzz.js plays hundreds of these; here a handful, so CI stays quick.
// Without git's origin/main (a bare tarball), the base is this checkout's own page: the cache on/off check still runs.
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const page = require('../scripts/lib/page');
const { openPage, playGame } = require('../scripts/lib/route-fuzz');
function pages() {
const head = path.join(os.tmpdir(), `tubes-fuzz-test-${process.pid}-head.html`);
fs.writeFileSync(head, page.assemble().html);
let base = head;
try { const b = path.join(os.tmpdir(), `tubes-fuzz-test-${process.pid}-base.html`); fs.writeFileSync(b, page.assembleRef('origin/main')); base = b; } catch (e) {}
return { base: openPage(base, 'base'), head: openPage(head, 'head') };
}
test('random games route the same as main, with and without the routing cache, move for move', () => {
const P = pages();
// Seeds that start a normal game, a sandbox, a scenario and a puzzle, with transit, exchanges, PNIs and RouteScript.
for (const seed of [20, 23, 27, 26]) {
const g = playGame(P, seed, { moves: 35 });
assert.equal(g.div, null, `seed ${seed} diverged: ${JSON.stringify(g.div, null, 1)}`);
assert.ok(g.moves >= 35, `seed ${seed} played its moves`);
assert.deepEqual(g.errors, [], `seed ${seed}: no page errors`);
}
});

View file

@ -26,7 +26,10 @@ function network(ev) {
const start = source.indexOf(' for(const pass of PASSES)for(const rank of RANKS){');
const end = source.indexOf(' for(const i of NORD)', start);
assert.ok(start >= 0 && end > start, 'allocation loop boundaries must exist');
ev(`window.referenceCompute=eval('('+${JSON.stringify(source.slice(0,start)+referenceAllocation+source.slice(end))}+')')`);
// The reference also builds every table afresh, so the comparison covers what routing keeps between calls.
assert.ok(source.includes('RCON=RC.on'), 'compute() can be told not to keep its tables');
const ref = (source.slice(0,start)+referenceAllocation+source.slice(end)).replace('RCON=RC.on', 'RCON=false');
ev(`window.referenceCompute=eval('('+${JSON.stringify(ref)}+')')`);
}
test('indexed routing matches the simple allocation loop across network changes', () => {

View file

@ -273,21 +273,24 @@ function plainCompute(ev) {
const src = ev('compute.toString()');
let plain = src
.replace('if(!lst.hi&&ALONE[fci])', 'if(false)')
.replace('if(!G.length){\n // One scan', 'if(false){\n // One scan')
.replace('if(!GN){\n // One scan', 'if(false){\n // One scan')
.replace('const ns=e.nets||NET_IDS;', 'const ns=NET_IDS;')
.replace('if(P.dead)return 0;', '').replaceAll('if(r<=1e-6){P.dead=1;return 0;}', '')
.replace('if(!P.dead)room-=push(', 'room-=push(').replace(/const gone=e=>\{[^\n]*\};/, 'const gone=e=>false;');
.replace('if(!P.dead)room-=push(', 'room-=push(').replace(/const gone=e=>\{[^\n]*\};/, 'const gone=e=>false;')
// ...and without what routing keeps between calls (29-route-cache.js): the plain path builds everything afresh.
.replace('RCON=RC.on', 'RCON=false');
// ...and the group split and sorting written the plain way, with array copies, as before they were made allocation-free
const sg = plain.indexOf('const ROOM=[],sendGroup='), sd = plain.indexOf('const sortD=');
assert.ok(sg > 0 && sd > sg, 'found the group sender and the sort');
plain = plain.slice(0, sg) + `const ROOM=[],sendGroup=(g0,i0=0,i1=g0.length)=>{const g=g0.slice(i0,i1);for(const e of g)e.vis=vis;
if(g.length>1){const room=g.map(e=>Math.max(0,e.cap-e.used)),tot=room.reduce((a,b)=>a+b,0),want=FREM[F];if(tot>1e-6)g.forEach((e,k)=>send(e,want*room[k]/tot));}
for(const e of g){if(FREM[F]<=1e-6)break;send(e,FREM[F]);}};\n ` + plain.slice(sd).replace(/^const sortD=a=>\{[^\n]*\};/, 'const sortD=a=>a.sort(byDist);');
for(const e of g){if(FREM[F]<=1e-6)break;send(e,FREM[F]);}};\n ` + plain.slice(sd).replace(/^const sortD=\(a,n=a\.length\)=>\{[^\n]*\};/, 'const sortD=(a,n=a.length)=>{const t=a.slice(0,n).sort(byDist);for(let i=0;i<n;i++)a[i]=t[i];};');
for (const [a, b] of [[src, plain]]) assert.notEqual(a, b);
assert.equal((plain.match(/if\(false\)/g) || []).length, 2, 'both shortcuts were found and switched off');
assert.ok(!plain.includes('e.nets||NET_IDS'), 'the exit index was switched off');
assert.ok(!plain.includes('P.dead'), 'the full-path mark was switched off');
assert.ok(plain.includes('const sortD=a=>a.sort(byDist);') && plain.includes('g.forEach('), 'the plain group split and sort are in');
assert.ok(plain.includes('RCON=false') && !plain.includes('RC.on'), 'the kept routing tables were switched off');
assert.ok(plain.includes('const sortD=(a,n=a.length)=>{const t=a.slice(0,n).sort(byDist);') && plain.includes('g.forEach('), 'the plain group split and sort are in');
ev('window.__plain=' + plain.replace(/^function compute/, 'function'));
}