diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a94607..2956db2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/site/index.html b/site/index.html index 794db86..25afcc8 100644 --- a/site/index.html +++ b/site/index.html @@ -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;jday&&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.useda.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.used1e-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;k1){let tot=0;for(let k=i0;k1e-6)for(let k=i0;k{const n=a.length;if(n>24){a.sort(byDist);return;}for(let i=1;i=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=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=e.cap){full++;continue;}const d=Dv[e.ci];if(d===Infinity)continue;const h=e.hp; if(h=lst.length&&full){let k=0;for(const e of lst)if(e.used=lst.length&&full){let k=0;for(let li=0;li1)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;i1e-6;){ - let j=i+1;while(j1e-6;){ + let j=i+1;while(j { 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, []); +});