Merge pull request #320 from antt0n/perf/routing-core

This commit is contained in:
Frederic Jung 2026-11-09 10:21:20 +00:00
commit 249ad4d478
No known key found for this signature in database
10 changed files with 408 additions and 159 deletions

View file

@ -1,7 +1,8 @@
// Deliberately scan all exits for every flow, without byRank, local-city
// shortcuts or first-group selection. Stable sorting preserves E order on ties.
// Without Auto-TE, an exit whose shortest path from this POP is already full fits nothing, so it counts as
// unusable, like a full exit (#210).
// unusable, like a full exit (#210). It runs inside the routing core (29-route-core.js), which reads what exits have
// used from its columns (USED, CAPX) and sends to exits by number (xi).
module.exports = `
// A carrier's own port (dr) is also a plain transit exit. A POP with a Prefer-marked transit port keeps what Prefer
// says, and a port not set to carry only takes its own POP's traffic.
@ -13,14 +14,14 @@ module.exports = `
for(F=0;F<NF;F++){fci=FCI[F];fnj=FNJ[F];const n=NET_IDS[fnj];
if(FREM[F]<=1e-6||(rank===0&&!FCA[F])||(pass===1&&!FBZ[F])||(pass===2&&FBZ[F]))continue;
Dv=SDi[fci];Pv=SPi[fci];PP=walked[fci]||(walked[fci]=[]);vis++;
const candidates=E.filter(e=>e.used<e.cap&&e.ok(n)&&
const candidates=E.filter(e=>USED[e.xi]<CAPX[e.xi]&&e.ok(n)&&
((TR?eff(e,n):base(e,n))===rank||(dr(e,fci,n)||-1)===rank)&&
(rank!==DIRECT_RANK[1][1]&&rank!==DIRECT_RANK[2][1]||CIDX[e.city]===fci)&&Dv[CIDX[e.city]]<Infinity&&!gone(e))
(rank!==DIRECT_RANK[1][1]&&rank!==DIRECT_RANK[2][1]||CIDX[e.city]===fci)&&Dv[CIDX[e.city]]<Infinity&&!gone(e.xi))
.sort((a,b)=>(a.pref==='high'?0:1)-(b.pref==='high'?0:1)||Dv[CIDX[a.city]]-Dv[CIDX[b.city]]+(tie(a)-tie(b)));
for(let i=0;i<candidates.length&&FREM[F]>1e-6;){
let j=i+1;while(j<candidates.length&&(candidates[j].pref==='high')===(candidates[i].pref==='high')&&
Math.abs(Dv[CIDX[candidates[j].city]]-Dv[CIDX[candidates[i].city]]+(tie(candidates[j])-tie(candidates[i])))<1e-6)j++;
sendGroup(candidates.slice(i,j));i=j;
sendGroup(candidates.slice(i,j).map(e=>e.xi));i=j;
}
}
}

View file

@ -86,3 +86,45 @@ test('days in a row reuse the kept tables and route like a fresh start every day
}
assert.deepEqual(errors, []);
});
test('with Auto-TE, kept orders and the no-detour marks route like a fresh start every day', () => {
// The routing core (29-route-core.js) keeps each list's order for each city with the tables, and Auto-TE marks the
// pairs with no detour: both have to give exactly what scanning and searching afresh give. The network is made busy
// (half its link capacity) so that flows spill, detours are searched and many fail.
const { ev, errors } = open();
ev("S.core.run=2;S.te={on:true,day:S.day,script:'spill at 85%'};for(const b of S.biz)if(BIZ_T[b.type].l2&&b.city2)b.route='scenic';for(const l of S.links)if(l.kind==='wave')l.cap=(l.cap||0)/2;compute();compute();");
assert.equal(ev('!!(S.te.on&&coreTE())'), true);
let tried = 0;
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`);
tried = Math.max(tried, ev('R.teMoved'));
}
assert.ok(tried > 0, 'Auto-TE moved traffic onto detours');
assert.deepEqual(errors, []);
});
// When the exits change but the graph does not (a network leaves an exchange because its PNI came up), the lists that
// did not change keep the orders routing worked out for them (the same arrays, used the same day), the ones that did
// start again, and routing is still exactly routing without the kept tables, day after day.
test('lists an exit change leaves alone keep their orders; routing stays the same as without the kept tables', () => {
const { ev, errors } = open();
// Two days on the kept build: the lists get their orders.
ev('compute();compute();');
ev('window.__ords=new Set(RC.x.lists.flatMap(d=>d.cs?d.ord.filter(Boolean):[d.ord]).filter(o=>Object.keys(o).length));');
assert.ok(ev('__ords.size') > 5, `orders worked out: ${ev('__ords.size')}`);
// One network leaves one exchange: the exits key moves, the graph does not.
const r = step(ev, `(()=>{const ix=Object.keys(S.ixp).find(x=>ixSessions(x).size>3),n=[...ixSessions(ix)][0];window.__left=n+'@'+ix;
S.peers[n+'@'+ix]=false;delete S.peers[n+'@'+ix];S.rs[ix]=false;S.pni.push({id:'pk1',net:n,city:ixCity(ix),tier:1,n:1});})()`);
assert.equal(r.g, false, 'same graph');
assert.equal(r.x, true, 'the exits changed');
assert.equal(r.same, true, 'the same routing as without the kept tables');
const kept = ev('RC.x.lists.flatMap(d=>d.cs?d.ord.filter(Boolean):[d.ord]).filter(o=>__ords.has(o)).length');
const all = ev('RC.x.lists.length');
assert.ok(kept > 0 && kept < ev('__ords.size') + 1, `lists that kept their orders: ${kept} of ${all}`);
// And a few more days, with the next change between them.
for (let d = 0; d < 3; d++) assert.equal(step(ev, 'for(const c in S.pops)if(S.pops[c].bb)S.pops[c].bb.d*=1.03').same, true, `day ${d + 1} after`);
assert.deepEqual(errors, []);
});

View file

@ -22,15 +22,16 @@ function network(ev) {
}
compute();`);
// Share the model and path sender, but replace indexed exit selection with
// the original full scan and stable sort for every flow.
const source = ev('compute.toString()');
const start = source.indexOf(' for(const pass of PASSES)for(const rank of RANKS){');
const end = source.indexOf(' for(const i of NORD)', start);
// the original full scan and stable sort for every flow: in the routing core (29-route-core.js), the passes of run().
const core = ev('routeCore.toString()'), source = ev('compute.toString()');
const start = core.indexOf(' for(const pass of PASSES)for(const rank of RANKS){');
const end = core.indexOf(' for(let x=0;x<NX;x++)E[x].used=USED[x];', start);
assert.ok(start >= 0 && end > start, 'allocation loop boundaries must exist');
// 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('('+${lit(ref)}+')')`);
const refCore = (core.slice(0, start) + referenceAllocation + core.slice(end)).replace(/^function routeCore/, 'function');
ev(`window.referenceCore=(${refCore})();window.referenceRaw=(0,eval)('('+${lit(source.replace('RCON=RC.on', 'RCON=false').replace(/^function compute/, 'function'))}+')');
window.referenceCompute=function(){const r=ROUTE;ROUTE=referenceCore;try{referenceRaw();}finally{ROUTE=r;}};`);
}
test('indexed routing matches the simple allocation loop across network changes', () => {

View file

@ -289,28 +289,41 @@ test('the map is not drawn while the dashboard covers the whole screen', () => {
// compare the two on a busy network (Auto-TE, then a RouteScript that keeps headroom on every link) and on a small one
// where Auto-TE has to route around a full link, so it keeps holding when the game's numbers change.
function plainCompute(ev) {
const src = ev('compute.toString()');
let plain = src
const src = ev('compute.toString()'), csrc = ev('routeCore.toString()');
const plain = src
.replace('const ns=e.nets||NET_IDS;', 'const ns=NET_IDS;')
// ...and without what routing keeps between calls (29-route-cache.js): the plain path builds everything afresh,
// which also leaves the lists without kept orders, so the first group and the rest are scanned and sorted.
.replace('RCON=RC.on', 'RCON=false');
// The routing core (29-route-core.js) with its shortcuts off: Auto-TE's no-detour marks and its searches picked up
// where the last one stopped back to the cache lookup and a fresh dijkstra() for every detour.
let core = csrc
.replace('if(!lst.hi&&!lst.mx&&ALONE[fci])', 'if(false)')
.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;')
// ...and without what routing keeps between calls (29-route-cache.js): the plain path builds everything afresh.
.replace('RCON=RC.on', 'RCON=false');
.replace('if(!P.dead)room-=push(', 'room-=push(').replace(/const gone=x=>\{[^\n]*\};/, 'const gone=x=>false;')
// ...and every exit is called, even one whose path is full with no detour (dud()).
.replace(/const dud=x=>\{[^\n]*\};/, 'const dud=x=>false;')
.replace('if(TEF[key]===1)return null;const c0=teCache.get(key);', 'const c0=teCache.get(key);if(c0===false)return null;')
.replace('const d=teSearch(src,dst);', 'const d=dijkstraTo(adj,src,wW,(v,e)=>e.res<=1e-3||(HAS[v]&&NRES[v]<=1e-3)||(CORE&&OUT[v]&&v!==dst),dst);')
.replace('const d=dijkstraTo(adj,src,wW,', 'const {D:D0,P:P0}=dijkstra(adj,src,wW,').replace(',dst);\n if(d===Infinity', ',dst),d=D0[dst];\n if(d===Infinity')
.replace('walkP(DJ.P,src,dst)', 'walkP(P0,src,dst)');
// ...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=');
const sg = core.indexOf('const ROOM=[],sendGroup='), sd = core.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=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');
core = core.slice(0, sg) + `const ROOM=[],sendGroup=(g0,i0=0,i1=g0.length)=>{const g=g0.slice(i0,i1);for(const x of g)VISX[x]=vis;
if(g.length>1){const room=g.map(x=>Math.max(0,CAPX[x]-USED[x])),tot=room.reduce((a,b)=>a+b,0),want=FREM[F];if(tot>1e-6)g.forEach((x,k)=>send(x,want*room[k]/tot));}
for(const x of g){if(FREM[F]<=1e-6)break;send(x,FREM[F]);}};\n ` + core.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];};');
assert.notEqual(src, plain); assert.notEqual(csrc, core);
assert.equal((core.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(!core.includes('P.dead'), 'the full-path mark was switched off');
assert.ok(core.includes('const dud=x=>false;'), 'every exit of a group is called');
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'));
assert.ok(!core.includes('TEF[key]===1') && !core.includes('dijkstraTo(') && !core.includes('const d=teSearch(') && core.includes('if(c0===false)return null;'), "Auto-TE's shortcuts were switched off");
assert.ok(core.includes('const sortD=(a,n=a.length)=>{const t=a.slice(0,n).sort(byDist);') && core.includes('g.forEach('), 'the plain group split and sort are in');
ev('window.__plainC=' + plain.replace(/^function compute/, 'function') + ';window.__plainR=(' + core.replace(/^function routeCore/, 'function') + ')();');
ev('window.__plain=function(){const r=ROUTE;ROUTE=__plainR;try{__plainC();}finally{ROUTE=r;}};');
}
test('fast routing gives exactly the same result as the plain path it replaces', () => {
@ -365,6 +378,51 @@ test('Auto-TE routes around a full link exactly like the plain path', () => {
assert.deepEqual(errors, []);
});
// Auto-TE's searches for a detour, on meshes of thin and thick links where POPs fill up and some sit outside the core
// (TE never steers through them, but may end there): a search from a city picks up where the last one from there
// stopped until a link or router runs out of room, and an exit whose full path has no detour is not called. Several
// seeded networks, from light to heavy load, must route exactly as the plain path that searches every time.
test('Auto-TE detours route like the plain path on crowded meshes with POPs outside the core', () => {
const { ev, errors } = load();
plainCompute(ev);
// The fast core, counting the searches that were picked up: answered at once, or gone on from where the last stopped.
const csrc = ev('routeCore.toString()'), counted = csrc.replace('if(go){g=LRg;if(DJ.done[dst]===g)return DJ.D[dst];', 'if(go){g=LRg;if(DJ.done[dst]===g){__TE.had++;return DJ.D[dst];}__TE.went++;');
assert.notEqual(counted, csrc, 'found the picked-up search');
ev('window.__TE={had:0,went:0};ROUTE=(' + counted.replace(/^function routeCore/, 'function') + ')();');
const CS = ['fra', 'ams', 'bru', 'par', 'lon', 'zrh', 'mil', 'vie', 'prg', 'ber', 'muc', 'mad', 'cph', 'waw', 'mrs', 'ham', 'dus'];
let moved = 0, outs = 0;
for (let seed = 1; seed <= 6; seed++) {
let a = seed * 7919;
const rnd = () => { a = (a * 16807) % 2147483647; return a / 2147483647; }, int = (n) => Math.floor(rnd() * n);
const cs = CS.filter(() => rnd() < 0.75), links = [];
for (let i = 1; i < cs.length; i++) links.push([cs[int(i)], cs[i], int(4)]);
for (let k = 0; k < cs.length; k++) { const x = cs[int(cs.length)], y = cs[int(cs.length)]; if (x !== y) links.push([x, y, int(4)]); }
const out = cs.filter(() => rnd() < 0.3), up = cs.filter((c) => out.includes(c) || rnd() < 0.3).concat(cs[0]);
ev(`ACT.start({});S.cash=1e12;S.lvl=13;for(const g of GOALS)S.done[g.id]=0;S.pops={};S.links=[];S.transit=[];
const cs=${JSON.stringify(cs)},out=${JSON.stringify(out)},IN=RG.findIndex(g=>g.cr>=3&&!g.noMpls),OUT=RG.findIndex(g=>g.cr<2);
for(const c of cs){ACT.buildPop({city:c});const p=S.pops[c];p.rgen=RG_TOP;p.routers=RMAX;if(!p.bb)ACT.launchBB({city:c});}
const L=(a,b,t)=>{UI.via=null;UI.lmode='wave';UI.route='main';try{openLinkModal(a,b);ACT.buildLink({tier:t});}catch(e){}closeModal();};
for(const [a,b,t] of ${JSON.stringify(links)})L(a,b,t);
for(const c of ${JSON.stringify(up)}){const t=NETS.find(n=>n.price&&n.pops.includes(c)&&(n.type==='t1'||n.type==='t2'));if(t)ACT.buyTransit({city:c,net:t.id,tier:3});}
// Then the routers: some too old for the core, and few enough that POPs fill up too.
for(const c of cs){const p=S.pops[c];p.rgen=out.includes(c)?OUT:IN;p.routers=1+${int(3)};}
S.ixp={};S.pni=[];S.caches=[];S.biz=[];S.te={on:true,day:0};S.core={lv:3,run:2};S.nextFail=1e9;S.nextEvent=1e9;`);
assert.ok(ev('coreOn()&&coreTE()'), 'the core rules apply and Auto-TE runs');
outs += ev('Object.keys(S.pops).filter(c=>!inCore(c)).length');
const sig = () => ev('JSON.stringify({served:R.served,teMoved:R.teMoved,egT:R.egT,load:S.links.map(l=>R.load[l.id]||0),node:R.nodeLoad,t:R.useT,f:R.fl.map(f=>[f.got,f.via])})');
for (const load of [0.3, 0.8, 1.6, 3]) {
ev(`for(const c in S.pops)S.pops[c].bb.d=CITY[c].P*${load}`);
ev('compute()'); const fast = sig(); moved += ev('R.teMoved');
ev('__plain()'); const ref = sig();
assert.equal(fast, ref, `network ${seed} at load ${load}: the fast and the plain path route the same`);
}
}
assert.ok(moved > 100, 'Auto-TE found detours');
assert.ok(outs > 0, 'some POPs are outside the core');
assert.ok(ev('__TE.had') > 0 && ev('__TE.went') > 0, 'searches were picked up: some answered at once, some gone on');
assert.deepEqual(errors, []);
});
test('fiber customers are checked by link id the same way with or without the lookup tables', () => {
const { ev, errors } = load();
ev("ACT.start({});S.cash=1e8;for(const g of GOALS)S.done[g.id]=0;ACT.launchBB({city:'fra'});ACT.buildPop({city:'ams'})");