Routing keeps its structural tables between calls and runs its hot loops as small functions, identical results
compute() was one 33 KB function that V8 never kept optimised: it ran in baseline code, boxing every number, and was recompiled in the background (about a second of compile each time on a loaded machine) only to deopt. Its stages now run as closures of their own (compute's own bytecode: 7.7 KB) and the money is computeMoney(). The shortest paths and the exit lists (okBy, reach, BRJ/LIST, DSEL) are kept between calls under keys built from everything they depend on (29-route-cache.js). Exits and paths have one object shape each. The equivalence tests compare cached against uncached routing; tests/route-cache.test.js checks the keys.
This commit is contained in:
parent
935c08839f
commit
a428ed817f
6 changed files with 209 additions and 44 deletions
87
tests/route-cache.test.js
Normal file
87
tests/route-cache.test.js
Normal 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, []);
|
||||
});
|
||||
|
|
@ -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', () => {
|
||||
|
|
|
|||
|
|
@ -276,7 +276,9 @@ function plainCompute(ev) {
|
|||
.replace('if(!G.length){\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');
|
||||
|
|
@ -287,6 +289,7 @@ function plainCompute(ev) {
|
|||
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('RCON=false') && !plain.includes('RC.on'), 'the kept routing tables were switched off');
|
||||
assert.ok(plain.includes('const sortD=a=>a.sort(byDist);') && plain.includes('g.forEach('), 'the plain group split and sort are in');
|
||||
ev('window.__plain=' + plain.replace(/^function compute/, 'function'));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue