Routing makes far less garbage: no temporary arrays or closures in its hot loop
The group split runs over an index range with one scratch array instead of
map/reduce/forEach and slice, leftover exits go into one reused array and are
sorted in place (insertion sort for short lists; both sorts are stable, so
the order is the same), flows are split without per-flow arrays, and the hot
loops use indexes instead of for...of. Without Auto-TE, exits whose path is
already full are not called at all. One routing pass on the big network
allocates 23 MB in Chromium instead of 66 MB.
Routing results are identical. The speed test's plain path now also uses the
original group split and sort, and a new test routes around a full 10G link
with Auto-TE (and a spill script) and compares both paths; planted bugs in
the split, the sort and both full-path skips are caught.
Big network vs main, two runs: headless routing -23% to -30%, Chromium
desktop -14% to -27%, phone -15% to -27%. Steps 2-4 together vs c76dd4d:
Chromium desktop routing -49%, phone -34%, a game day -26% and -30%.
This commit is contained in:
parent
2c0d69379a
commit
4682f16730
3 changed files with 69 additions and 26 deletions
|
|
@ -6,7 +6,7 @@ All notable changes to this project are documented here. The project follows
|
|||
|
||||
## [Unreleased]
|
||||
|
||||
- Big networks are snappier again: working out each day's traffic takes about a quarter less time in the browser, so game days and clicks pause for less on phones. Traffic goes exactly where it went before.
|
||||
- Big networks are snappier again: working out each day's traffic takes about a third less time on phones and half on desktops, so game days and clicks pause for less. Traffic goes exactly where it went before, and the game makes far less garbage for the browser to clean up.
|
||||
- Your own subsea cable can no longer be longer than any optic can light (#88). New York to Sydney now says so before you pay, instead of after the ship comes home. Calling off a cable while it is still being laid returns most of the price of the part not laid yet, and an unusable cable from an older save goes back for a full refund.
|
||||
- Your own IX can now live in either building of a two-site metro, or in both with a second switch, so a dark building no longer takes your exchange down (#36). The LAG buttons that did nothing on your own IX are gone.
|
||||
- Upgrading the only router in a POP that has nothing plugged in yet no longer opens a maintenance window or a NOC warning (#105).
|
||||
|
|
|
|||
|
|
@ -1925,8 +1925,9 @@ function compute(){
|
|||
R2.cityBlocked=R2.cityBlocked||{};
|
||||
const mtuH=ev.pmtud&&ev.pmtud.until>day?ev.pmtud:null,kr=CITY[c].kr;
|
||||
for(let j=0;j<NN;j++){const n=NETS[j],a=d0*sa[j]*thr(n.id)*(rtbh?0:edge?0.9:1)*(mtuH&&mtuH.net===n.id?mtuH.f:1)*(kr&&ev.krroute>day&&n.id==='fbk'?0.8:1);if(a<=0)continue;
|
||||
const bf=d0>0?biz/d0:0,parts=TRX&&TRX.protect&&bf>0?[[a*bf,true],[a*(1-bf),false]]:[[a,false]];
|
||||
for(const [x,bz] of parts){if(x<=0)continue;if(cacheNets.has(n.id)){flow(c,ci,cn,j,x*0.85,true,false,bz);flow(c,ci,cn,j,x*0.15,false,false,bz);}
|
||||
// With `protect customers`, the business part first, then the rest (no arrays: this runs per city and network).
|
||||
const bf=d0>0?biz/d0:0,sp=!!(TRX&&TRX.protect&&bf>0),cn2=cacheNets.has(n.id);
|
||||
for(let q=0;q<(sp?2:1);q++){const x=sp?(q?a*(1-bf):a*bf):a,bz=sp&&!q;if(x<=0)continue;if(cn2){flow(c,ci,cn,j,x*0.85,true,false,bz);flow(c,ci,cn,j,x*0.15,false,false,bz);}
|
||||
else flow(c,ci,cn,j,x,false,false,bz);}}
|
||||
R2.bizDem[c]=biz*d/(d0||1);R2.bizSrv[c]=0;
|
||||
}
|
||||
|
|
@ -1998,7 +1999,7 @@ function compute(){
|
|||
const byRank={};for(const id of NET_IDS){const m=byRank[id]={};for(const e of okBy[id]){const r=TR?eff(e,id):base(e);(m[r]||(m[r]=[])).push(e);}
|
||||
for(const r in m){const L=m[r];L.hi=L.some(e=>e.hp===0);L.at=new Map();for(const e of L){const h=L.at.get(e.ci);if(h)h.push(e);else L.at.set(e.ci,[e]);}}}
|
||||
// The flow being routed, and from its POP: distances, arriving edges and the paths walked so far, by city index.
|
||||
let f=null,Dv=null,Pv=null,PP=null,vis=0;const walked=[],G=[],NEG=[];
|
||||
let f=null,Dv=null,Pv=null,PP=null,vis=0;const walked=[],G=[],REST=[],NEG=[];
|
||||
// An exit the flow can still try: not visited yet, not full when the flow started, and reachable from the POP.
|
||||
const usable=e=>e.vis!==vis&&e.used<e.cap&&Dv[e.ci]<Infinity,byDist=(a,b)=>a.hp-b.hp||Dv[a.ci]-Dv[b.ci];
|
||||
// Push up to `want` along one path to exit e; returns what fitted. Capacity only shrinks while routing, so a
|
||||
|
|
@ -2021,17 +2022,24 @@ function compute(){
|
|||
// with free capacity, as long as they are no more than twice as long.
|
||||
const send=(e,want)=>{
|
||||
let room=Math.min(want,f.rem,e.cap-e.used);if(room<=1e-6)return;
|
||||
room-=push(e,PP[e.ci]||(PP[e.ci]=walkI(Pv,f.ci,e.ci)),room,TE&&SPILL<1?1-SPILL:0);
|
||||
const P=PP[e.ci]||(PP[e.ci]=walkI(Pv,f.ci,e.ci));if(!P.dead)room-=push(e,P,room,TE&&SPILL<1?1-SPILL:0);
|
||||
if(TE&&room>1e-6&&f.rem>1e-6&&e.used<e.cap&&e.city!==f.c)for(let k=0;k<2&&room>1e-6;k++){const P=altPath(f.ci,e.ci,Dv[e.ci]);if(!P)break;const m=push(e,P,Math.min(room,e.cap-e.used));if(m<=1e-6)break;room-=m;R2.teMoved+=m;}
|
||||
};
|
||||
// Exits with the same preference and the same IGP distance form an ECMP group: spread the flow over
|
||||
// them by free capacity (like several transits in one city), then top up in order if one fills.
|
||||
const sendGroup=g=>{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=f.rem;if(tot>1e-6)g.forEach((e,k)=>send(e,want*room[k]/tot));}
|
||||
for(const e of g){if(f.rem<=1e-6)break;send(e,f.rem);}};
|
||||
// The group is g[i0..i1): plain loops and one scratch array, since this runs for most flows (same sums, same order).
|
||||
// Without Auto-TE an exit whose path from here is full takes nothing: skip the call (and the numbers it boxes).
|
||||
const gone=e=>{if(TE)return false;const P=PP[e.ci];return !!P&&P.dead===1;};
|
||||
const ROOM=[],sendGroup=(g,i0=0,i1=g.length)=>{for(let k=i0;k<i1;k++)g[k].vis=vis;
|
||||
if(i1-i0>1){let tot=0;for(let k=i0;k<i1;k++){const e=g[k],r=Math.max(0,e.cap-e.used);ROOM[k-i0]=r;tot+=r;}const want=f.rem;if(tot>1e-6)for(let k=i0;k<i1;k++)if(!gone(g[k]))send(g[k],want*ROOM[k-i0]/tot);}
|
||||
for(let k=i0;k<i1;k++){if(f.rem<=1e-6)break;if(!gone(g[k]))send(g[k],f.rem);}};
|
||||
// Stable, in place: short lists by insertion (no garbage), long ones by the built-in sort. Both are stable with the
|
||||
// same comparison, so the order is the same either way.
|
||||
const sortD=a=>{const n=a.length;if(n>24){a.sort(byDist);return;}for(let i=1;i<n;i++){const x=a[i];let j=i-1;while(j>=0&&byDist(a[j],x)>0){a[j+1]=a[j];j--;}a[j+1]=x;}};
|
||||
for(const pass of PASSES||[null])for(const rank of RANKS){
|
||||
if(TR?!E.length:!E.some(e=>base(e)===rank))continue;
|
||||
for(f of flows){
|
||||
// Index loops in here: plain for…of allocates an iterator per loop until V8 has optimised compute().
|
||||
for(let fi=0,FN=flows.length;fi<FN;fi++){f=flows[fi];
|
||||
if(f.rem<=1e-6||(rank===0&&!f.cache)||(pass&&!pass(f)))continue;
|
||||
const lst=byRank[f.n][rank];if(!lst)continue;
|
||||
Dv=SDi[f.ci];Pv=SPi[f.ci];PP=walked[f.ci]||(walked[f.ci]=[]);vis++;
|
||||
|
|
@ -2047,18 +2055,18 @@ function compute(){
|
|||
// distance only goes down, so dropping the ones it leaves behind gives exactly the exits a second scan would.
|
||||
// Exits that are full stay full for this pass: when a quarter of a list is, it is rewritten without them.
|
||||
let bh=2,bd=Infinity,full=0;
|
||||
for(const e of lst){if(e.used>=e.cap){full++;continue;}const d=Dv[e.ci];if(d===Infinity)continue;const h=e.hp;
|
||||
for(let li=0,LN=lst.length;li<LN;li++){const e=lst[li];if(e.used>=e.cap){full++;continue;}const d=Dv[e.ci];if(d===Infinity)continue;const h=e.hp;
|
||||
if(h<bh){bh=h;bd=d;G.length=0;G.push(e);}
|
||||
else if(h===bh){if(d<bd){bd=d;let k=0;for(const x of G)if(Dv[x.ci]-d<1e-6)G[k++]=x;G.length=k;G.push(e);}else if(d-bd<1e-6)G.push(e);}}
|
||||
if(full*4>=lst.length&&full){let k=0;for(const e of lst)if(e.used<e.cap)lst[k++]=e;lst.length=k;}
|
||||
else if(h===bh){if(d<bd){bd=d;let k=0;for(let gi=0;gi<G.length;gi++){const x=G[gi];if(Dv[x.ci]-d<1e-6)G[k++]=x;}G.length=k;G.push(e);}else if(d-bd<1e-6)G.push(e);}}
|
||||
if(full*4>=lst.length&&full){let k=0;for(let li=0;li<lst.length;li++){const e=lst[li];if(e.used<e.cap)lst[k++]=e;}lst.length=k;}
|
||||
if(bh>1)continue;
|
||||
if(G.length>1)G.sort(byDist);}
|
||||
sendGroup(G);
|
||||
if(G.length>1)sortD(G);}
|
||||
sendGroup(G,0,G.length);
|
||||
if(f.rem<=1e-6)continue;
|
||||
const rest=lst.filter(usable).sort(byDist);
|
||||
for(let i=0;i<rest.length&&f.rem>1e-6;){
|
||||
let j=i+1;while(j<rest.length&&rest[j].hp===rest[i].hp&&Math.abs(Dv[rest[j].ci]-Dv[rest[i].ci])<1e-6)j++;
|
||||
sendGroup(rest.slice(i,j));i=j;
|
||||
REST.length=0;for(let li=0;li<lst.length;li++)if(usable(lst[li]))REST.push(lst[li]);sortD(REST);
|
||||
for(let i=0;i<REST.length&&f.rem>1e-6;){
|
||||
let j=i+1;while(j<REST.length&&REST[j].hp===REST[i].hp&&Math.abs(Dv[REST[j].ci]-Dv[REST[i].ci])<1e-6)j++;
|
||||
sendGroup(REST,i,j);i=j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -218,23 +218,36 @@ test('the map is not drawn while the dashboard covers the whole screen', () => {
|
|||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
// #89's fast paths must route exactly like the plain path. This builds a copy of compute() with every shortcut
|
||||
// turned off (the POP's-own-exits shortcut, the first-group scan with its list clean-up, the per-network exit index
|
||||
// and the full-path mark in push) and compares the two on a busy network, with Auto-TE and then with a RouteScript
|
||||
// that keeps headroom on every link, so it keeps holding when the game's numbers change.
|
||||
test('fast routing gives exactly the same result as the plain path it replaces', () => {
|
||||
const { ev, errors } = load();
|
||||
// #89's fast paths must route exactly like the plain path. plainCompute() builds window.__plain, a copy of compute()
|
||||
// with every shortcut turned off (the POP's-own-exits shortcut, the first-group scan with its list clean-up, the
|
||||
// per-network exit index and the full-path mark), and the group split and sort written the plain way. The tests below
|
||||
// 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()');
|
||||
const plain = src
|
||||
let plain = src
|
||||
.replace('if(!lst.hi&&ALONE[f.ci])', 'if(false)')
|
||||
.replace('if(!G.length){\n // One scan', 'if(false){\n // One scan')
|
||||
.replace('for(const n of e.nets||NET_IDS)', 'for(const n of NET_IDS)')
|
||||
.replace('if(P.dead)return 0;', '').replaceAll('if(r<=1e-6){P.dead=1;return 0;}', '');
|
||||
.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 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=f.rem;if(tot>1e-6)g.forEach((e,k)=>send(e,want*room[k]/tot));}
|
||||
for(const e of g){if(f.rem<=1e-6)break;send(e,f.rem);}};\n ` + plain.slice(sd).replace(/^const sortD=a=>\{[^\n]*\};/, 'const sortD=a=>a.sort(byDist);');
|
||||
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');
|
||||
ev('window.__plain=' + plain.replace(/^function compute/, 'function'));
|
||||
}
|
||||
|
||||
test('fast routing gives exactly the same result as the plain path it replaces', () => {
|
||||
const { ev, errors } = load();
|
||||
plainCompute(ev);
|
||||
ev(`ACT.start({});S.cash=1e12;S.lvl=13;S.won=true;for(const g of GOALS)S.done[g.id]=0;ACT.launchBB({city:'fra'});
|
||||
const cs=ALLC.filter(c=>!CITY[c].cn).slice(0,40);for(const c of cs){if(!S.pops[c])ACT.buildPop({city:c});S.pops[c].rgen=10;S.pops[c].routers=4;if(!S.pops[c].bb)ACT.launchBB({city:c});S.pops[c].bb.d=CITY[c].P*0.7;}
|
||||
const L=(a,b,t)=>{UI.via=null;UI.lmode='wave';UI.route='diverse';try{openLinkModal(a,b);ACT.buildLink({tier:t});closeModal();}catch(e){}};
|
||||
|
|
@ -261,3 +274,25 @@ test('fast routing gives exactly the same result as the plain path it replaces',
|
|||
assert.ok(ev('R.served') > 1000 && ev("S.transit.length") > 14, 'traffic flows over transit, peering and caches');
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
||||
test('Auto-TE routes around a full link exactly like the plain path', () => {
|
||||
const { ev, errors } = load();
|
||||
plainCompute(ev);
|
||||
// Frankfurt reaches its only upstream in Amsterdam over a 10G wave, or the long way through Brussels on 800G.
|
||||
ev(`ACT.start({});S.cash=1e12;S.lvl=13;for(const g of GOALS)S.done[g.id]=0;for(const c of ['ams','bru'])ACT.buildPop({city:c});
|
||||
for(const c of ['fra','ams','bru']){S.pops[c].rgen=RG.length-1;S.pops[c].routers=RMAX;}if(!S.pops.fra.bb)ACT.launchBB({city:'fra'});S.pops.fra.bb.d=CITY.fra.P*0.8;
|
||||
const L=(a,b,t)=>{UI.via=null;UI.lmode='wave';UI.route='main';openLinkModal(a,b);ACT.buildLink({tier:t});closeModal();};
|
||||
L('fra','ams',0);L('fra','bru',3);L('bru','ams',3);
|
||||
S.transit=[];const t=NETS.find(n=>n.price&&n.pops.includes('ams')&&n.type==='t1');ACT.buyTransit({city:'ams',net:t.id,tier:3});
|
||||
S.ixp={};S.pni=[];S.caches=[];S.te={on:true,day:0};S.nextFail=1e9;S.nextEvent=1e9;`);
|
||||
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})');
|
||||
for (const script of ['', 'spill at 70%']) {
|
||||
ev(`S.te.script=${JSON.stringify(script)}`);
|
||||
ev('compute()'); const fast = sig();
|
||||
ev('__plain()'); const ref = sig();
|
||||
assert.equal(fast, ref, `fast and plain route the same around the full link (${script || 'no script'})`);
|
||||
assert.ok(ev('R.teMoved') > 100, 'Auto-TE moved traffic through Brussels');
|
||||
assert.ok(ev("R.load[S.links.find(l=>l.a==='fra'&&l.b==='ams').id]") >= (script ? 6.9 : 9.99), 'the direct 10G link is full (up to the spill line)');
|
||||
}
|
||||
assert.deepEqual(errors, []);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue