diff --git a/src/game/80-simulation/10-dijkstra.js b/src/game/80-simulation/10-dijkstra.js index 1ef9be7..5b53685 100644 --- a/src/game/80-simulation/10-dijkstra.js +++ b/src/game/80-simulation/10-dijkstra.js @@ -15,3 +15,5 @@ function dijkstra(adj,src,wf,skip,stop){const n=adj.length,D=new Float64Array(n) return {D,P};} // The edges of a dijkstra() path back from city index B to A, and the cities on it (B first), as indices. function walkI(P,A,B){const path=[],ni=[B];let v=B;while(v!==A){const l=P[v];path.push(l);v=l.ai===v?l.bi:l.ai;ni.push(v);}return {path,ni};} +// The same for compute()'s sender, with the marks it keeps on a path (full, its latency, its far-exit traffic) there from the start: one object shape. +function walkP(P,A,B){const path=[],ni=[B];let v=B;while(v!==A){const l=P[v];path.push(l);v=l.ai===v?l.bi:l.ai;ni.push(v);}return {path,ni,dead:0,ms:-1,fg:0};} diff --git a/src/game/80-simulation/29-route-cache.js b/src/game/80-simulation/29-route-cache.js new file mode 100644 index 0000000..7f134dc --- /dev/null +++ b/src/game/80-simulation/29-route-cache.js @@ -0,0 +1,52 @@ +/* What compute() works out from the network's shape alone, kept from one call to the next: most days only demand moves, + and on a big network these are a third of the work. Each part has a key made of everything it is built from, and any + other key rebuilds it, so it is never stale: + - the graph (RC.gk): the POPs, the remote cities and every edge with its ends and weight, in the order compute() made + them. Kept: the shortest-path tables from every POP, ALONE and the islands (COMP). + - the exits (RC.xk): the graph key, RouteScript, the events that change who reaches whom, and every exit in order with + its kind, id, network, city, preference, rank, carry and prepend marks and what it can reach. Kept: okBy, what is + reachable, and each network's exit lists by rank (BRJ, with the lists networks share) with DSEL and RSET. + Only numbers are kept (exit and edge indices): compute() makes fresh exits, lists and paths from them every call, + since routing writes on those. RC.on=false (the equivalence tests) always rebuilds. */ +const RC={on:true,gk:null,g:null,xk:null,x:null}; +// The graph key: EA lists the edges in the order compute() made them (so adj, and the order Dijkstra settles ties, follow). +function rcGraphKey(pops,remote,EA){let k=pops.join(',')+'|'+remote.join(',');for(let i=0;i=0)P[x]=EA[k];}SDi[i]=SPD[s]=G.D[i];SPi[i]=SPP[s]=P;} + return {SPD,SPP,SDi,SPi,ALONE:G.ALONE,COMP:G.COMP,NCOMP:G.NCOMP};} + // ALONE[i]: no other city lies within 1e-6 of POP i (so only its own exits can be at distance 0 from it). + const ALONE=new Uint8Array(NC),PIs=[]; + for(const s of pops){const i=CIDX[s],r=dijkstra(adj,i,wW);SDi[i]=SPD[s]=r.D;SPi[i]=SPP[s]=r.P;ALONE[i]=1;for(let x=0;x=0)continue;const D=SDi[i],k=NCOMP++;for(let x=0;x{k+=n+(d&&!d.has(n)?'^,':',');});}else if(e.k==='c')k+=e.nets[0];} + return k;} +// The exits' lists, with a sort key: an exit's index in E. +const rcIx=(L,E)=>{const a=new Int32Array(L.length);for(let i=0;i{const L=new Array(a.length);for(let i=0;ie.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]);}} +// What reaching networks gave R (taken right after compute() worked it out, before anything else adds to R). +function rcReachTake(R2){return {reachable:{...R2.reachable},nt:{...R2.nt},cone:{...R2.cone},reach:R2.reach,reachNT:R2.reachNT};} +function rcReachPut(X,R2){const r=X.reach;Object.assign(R2.reachable,r.reachable);Object.assign(R2.nt,r.nt);Object.assign(R2.cone,r.cone);R2.reach=r.reach;R2.reachNT=r.reachNT;} +// Keep what was built (on a miss): okBy, the exit lists (each shared list once), each network's lists by rank, DSEL, RSET. +function rcExitKeep(key,E,okBy,reach,LIST,BRJ,DSEL,RSET){const ok=[];for(let j=0;j{const o=[];for(const r in m)o.push(r,id.get(m[r]));return o;}); + RC.xk=key;RC.x={ok,reach,lists,brj,DSEL,RSET:[...RSET]};} +// Fresh lists for today's exits from a kept build. +function rcExitLists(X,E,BRJ,DSEL,RSET){const V=X.lists.map(d=>{if(d.cs){const cs={cs:[]};for(let k=0;kadj[CIDX[p]]=[]); - const edges={},TRX=teRules(),live=[];let NE=0;R2.bizDem={};R2.bizSrv={}; + const edges={},EA=[],TRX=teRules(),live=[];let NE=0;R2.bizDem={};R2.bizSrv={}; for(const l of S.links){const ai=CIDX[l.a],bi=CIDX[l.b];if(linkDown(l)||!adj[ai]||!adj[bi])continue;live.push(l);const key=l.ae.w,wLat=e=>e.lat+0.05; - for(const s of pops){const i=CIDX[s],r=dijkstra(adj,i,wW);SDi[i]=SPD[s]=r.D;SPi[i]=SPP[s]=r.P;ALONE[i]=1;for(let x=0;x=0)continue;const D=SDi[i],k=NCOMP++;for(let x=0;xe.w,wLat=e=>e.lat+0.05,RCON=RC.on,GK=RCON?rcGraphKey(pops,remote,EA):null, + {SPD,SPP,SDi,SPi,ALONE,COMP,NCOMP}=rcGraph(GK,pops,adj,EA,NC,wW); + const ev=S.ev; const depeer=ev.depeer>day||scnRule('spat'), leak=ev.leak&&ev.leak.until>day?ev.leak.net:null; // Sender pays (Gangnam Style Peering): no carrier hands traffic to the Korean incumbents for you, only a paid PNI does. const snd=scnRule('sender'); + // Stages with loops run as functions of their own (see rankPass below), here the exits. + const E=(()=>{const E=[]; for(const t of S.transit){if(!popUp(t.city)||t.down>day||!liveN(t.city,t.site,t.n))continue; // AS-path prepending (#96) moves part of the port to a last-resort exit of its own. const cap=Math.min(liveCap(PORT[t.tier].g,t.city,t.site,t.n)*lagF(t),TRX&&TRX.cap[t.net]!==undefined?TRX.cap[t.net]:Infinity),pf=TRX&&TRX.prep[t.net]?PREPEND_F[TRX.prep[t.net]]:0; - const ex={k:'t',net:t.net,clean:!!t.clean,prem:premOn(t),pref:t.pref||'normal',carry:!!t.carry,lab:labOf(N_('common.net_transit'),'net',NET[t.net].name),id:t.id,city:t.city,cap:cap*(1-pf),used:0,rank:3,ok:n=>!(depeer&&t.net==='cog'&&n==='ggl')&&n!==leak&&!(snd&&NET[n].kr)}; - E.push(ex);if(pf)E.push({...ex,cap:cap*pf,prep:true});} + const ex=mkEx('t',t.id,t.city,cap*(1-pf),3,t.pref||'normal',labOf(N_('common.net_transit'),'net',NET[t.net].name),n=>!(depeer&&t.net==='cog'&&n==='ggl')&&n!==leak&&!(snd&&NET[n].kr),null,t.net); + ex.clean=!!t.clean;ex.prem=premOn(t);ex.carry=!!t.carry;E.push(ex);if(pf){const x=Object.assign(mkEx(),ex);x.cap=cap*pf;x.prep=true;E.push(x);}} // `nets`: every network ok() can say yes to, so it need not be asked about the other hundred. for(const c in S.ixp){const ct=ixCity(c);if(!(popUp(ct)||remote.includes(ct))||(S.ev.ixdown&&S.ev.ixdown[c]>day)||!(S.myIx[c]?myIxLive(c):liveN(ct,(S.ixSite||{})[c],(S.ixn||{})[c])))continue;const set=ixSessions(c);R2.sessions+=set.size; // A peer announces its customers too (#85): the session reaches its cone, after the networks peering directly. let all=set;for(const id of set)if(DOWN[id].length){if(all===set)all=new Set(set);for(const x of DOWN[id])all.add(x);} - E.push({k:'ix',pref:(S.ixPref&&S.ixPref[c])||'normal',lab:labOf(N_('common.ix_peering'),'ix',ixName(c)),id:c,city:ct,cap:ixCap(c),used:0,rank:2,ok:n=>all.has(n),nets:all,dir:all===set?null:set});} - for(const p of S.pni){if(!pniOn(p)||!popUp(p.city)||!liveN(p.city,p.site,p.n))continue;R2.sessions++;E.push({k:'p',sac:!!p.sacrif,pnc:!!NET[p.net].cn,pref:p.pref||'normal',lab:labOf(N_('common.pni_with_net'),'net',NET[p.net].name),id:p.id,city:p.city,cap:liveCap(pniG(p),p.city,p.site,p.n)*lagF(p)*(p.sacrif?0.5:1),used:0,rank:1,net:p.net,ok:n=>CONE_S[p.net].has(n),nets:CONE[p.net]});} - for(const c of S.caches){if(!popUp(c.city)||siteDown(c.city,c.site==='b'?'b':'a'))continue;E.push({k:'c',pref:'normal',lab:labOf(N_('common.name_cache'),'name',NET[c.net].cache.name),id:c.id,city:c.city,cap:cacheG(c),used:0,rank:0,ok:n=>n===c.net,nets:[c.net]});} + const x=mkEx('ix',c,ct,ixCap(c),2,(S.ixPref&&S.ixPref[c])||'normal',labOf(N_('common.ix_peering'),'ix',ixName(c)),n=>all.has(n),all);x.dir=all===set?null:set;E.push(x);} + for(const p of S.pni){if(!pniOn(p)||!popUp(p.city)||!liveN(p.city,p.site,p.n))continue;R2.sessions++; + const x=mkEx('p',p.id,p.city,liveCap(pniG(p),p.city,p.site,p.n)*lagF(p)*(p.sacrif?0.5:1),1,p.pref||'normal',labOf(N_('common.pni_with_net'),'net',NET[p.net].name),n=>CONE_S[p.net].has(n),CONE[p.net],p.net);x.sac=!!p.sacrif;x.pnc=!!NET[p.net].cn;E.push(x);} + for(const c of S.caches){if(!popUp(c.city)||siteDown(c.city,c.site==='b'?'b':'a'))continue;E.push(mkEx('c',c.id,c.city,cacheG(c),0,'normal',labOf(N_('common.name_cache'),'name',NET[c.net].cache.name),n=>n===c.net,[c.net]));} // After an update adds networks, a free courtesy route reaches the new ones you have no other way to (#46). R2.graceNets=0;if(S.grace&&S.grace.until>day){const gs=new Set(S.grace.nets.filter(n=>NET[n]&&!E.some(e=>e.k!=='c'&&e.ok(n)))),hc=popUp(S.home)?S.home:pops.find(popUp); - R2.graceNets=gs.size;if(gs.size&&hc)E.push({k:'ix',pref:'normal',lab:tr('compute.courtesy_route_to_new_networks'),id:'~grace',city:hc,cap:1e9,used:0,rank:2,ok:n=>gs.has(n),nets:gs});} - {const rp=ev.rpki&&ev.rpki.until>day?new Set(ev.rpki.nets):null;if(rp)for(const e of E){if(e.k==='c')continue;const o=e.ok;e.ok=n=>!rp.has(n)&&o(n);}} + R2.graceNets=gs.size;if(gs.size&&hc)E.push(mkEx('ix','~grace',hc,1e9,2,'normal',tr('compute.courtesy_route_to_new_networks'),n=>gs.has(n),gs));} + return E;})(); + const rp=ev.rpki&&ev.rpki.until>day?new Set(ev.rpki.nets):null;if(rp)for(const e of E){if(e.k==='c')continue;const o=e.ok;e.ok=n=>!rp.has(n)&&o(n);} + // Who reaches whom and each network's exit lists, kept while the exits are the same (XC, 29-route-cache.js). + const XK=RCON?rcExitKey(GK,E,(depeer?1:0)+'|'+leak+'|'+(snd?1:0)+'|'+(rp?ev.rpki.nets.join(','):'')+'|'+(TRX?S.te.script:'')):null,XC=XK!==null&&RC.xk===XK?RC.x:null; // coneOf: does exit e reach network n only through a peer's customer cone (#85)? const coneOf=(e,n)=>e.k==='p'?e.net!==n:!!e.dir&&!e.dir.has(n); // ok() cannot change during one compute, so ask each exit once per network, not once per flow and rank. // okBy[net] lists the exits that reach it in E's order, like E.filter(e=>e.ok(net)). - const okBy={};for(const id of NET_IDS)okBy[id]=[]; + const okBy=XC?rcOkBy(XC,E):{};if(!XC)for(const id of NET_IDS)okBy[id]=[]; const okAdd=(e,n)=>{const L=okBy[n];if(L&&L[L.length-1]!==e&&e.ok(n))L.push(e);}; // TSL: a slot per transit port id, for what it carries to each network (R.tNet, added up in TNA). - const ekN={},TSL=new Map();for(const e of E){e.ek=e.k+':'+e.id;ekN[e.ek]=(ekN[e.ek]||0)+1;} + const ekN={},TSL=new Map();(()=>{for(const e of E){e.ek=e.k+':'+e.id;ekN[e.ek]=(ekN[e.ek]||0)+1;} // dup: shares its key with another exit (a hand-edited save), so R.egDesc shows whichever pushed last. - for(const e of E){e.J=0;e.byC=null;e.desc=null;e.dup=ekN[e.ek]>1;e.told=false;e.hp=e.pref==='high'?0:1;e.ci=CIDX[e.city];e.cn=cnCity(e.city);e.vis=0;e.ts=-1;if(e.k==='t'){let t=TSL.get(e.id);if(t===undefined)TSL.set(e.id,t=TSL.size);e.ts=t;} + for(let i=0;i1;e.told=false;e.hp=e.pref==='high'?0:1;e.ci=CIDX[e.city];e.cn=cnCity(e.city);e.vis=0;e.ts=-1;if(e.k==='t'){let t=TSL.get(e.id);if(t===undefined)TSL.set(e.id,t=TSL.size);e.ts=t;} + if(XC)continue; // Index loops or forEach (same order): for…of here made an iterator result per exit and network. const ns=e.nets||NET_IDS;if(Array.isArray(ns)){for(let q=0;qokAdd(e,n));} - for(const n of NETS){let any=false,nt=false;for(const e of okBy[n.id])if(e.rank>0){any=true;if(e.k!=='t'){nt=true;break;}}R2.reachable[n.id]=any;if(any)R2.reach+=NSH[n.id];if(nt){R2.nt[n.id]=1;R2.reachNT+=NSH[n.id]; + if(XC)rcReachPut(XC,R2);else for(const n of NETS){let any=false,nt=false;for(const e of okBy[n.id])if(e.rank>0){any=true;if(e.k!=='t'){nt=true;break;}}R2.reachable[n.id]=any;if(any)R2.reach+=NSH[n.id];if(nt){R2.nt[n.id]=1;R2.reachNT+=NSH[n.id]; // Reached without transit only through the customers of networks you peer with (#85). R.cone[id]: the peer that // carries it, the first PNI's network or else the nearest upstream that is a member of an exchange you are on. - if(!okBy[n.id].some(e=>(e.k==='p'||e.k==='ix')&&!coneOf(e,n.id)))R2.cone[n.id]=coneBy(okBy[n.id],n.id);}} + if(!okBy[n.id].some(e=>(e.k==='p'||e.k==='ix')&&!coneOf(e,n.id)))R2.cone[n.id]=coneBy(okBy[n.id],n.id);}}})(); + const RCH=XK!==null&&!XC?rcReachTake(R2):null; const cacheNets=new Set(S.caches.filter(c=>popUp(c.city)).map(c=>c.net)); const viral=(ev.viral>day?1.25:1)*(ev.php&&ev.php>day?0.7:1); /* Flows are columns, not objects (tens of thousands of them on a big network): flow F starts in city FCI[F] for network @@ -80,7 +90,7 @@ function compute(){RW.gen++; const bizBw={};for(const b of S.biz){const T=BIZ_T[b.type];if(!T.l2&&!T.fiber&&(!T.event||eventLive(b))){const v=bizBw[b.city];bizBw[b.city]=(v===undefined?0:v)+b.bw;}} // Traffic shares by network index, and their sums, for each share table (and with Russia's throttle). const shArr=new Map(),shSum=new Map(),DEMTO=new Float64Array(NN),loc=scnRule('local'); - for(const c of Object.keys(S.pops)){ + (()=>{for(const c of Object.keys(S.pops)){ const p=S.pops[c],ci=CIDX[c],cn=cnCity(c),SH=cn?NSH_CN:ruCity(c)?NSH_RU:CITY[c].kr&&S.krExit?NSH_KRX:NSH;const eye=(p.bb?p.bb.d:0)*viral*(cn&&ev.golden>day?1.4:1);const biz=bizBw[c]??0;const d0=eye+biz; // Goggle video is throttled in Russia (#70): people there get half of it, so they ask for half. It is not // traffic you failed to deliver, and it does not cross your links only to be thrown away. @@ -97,7 +107,7 @@ function compute(){RW.gen++; 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(ci,j,x*0.85,true,bz);flow(ci,j,x*0.15,false,bz);} else flow(ci,j,x,false,bz);}} R2.bizDem[c]=biz*d/(d0||1);R2.bizSrv[c]=0; - } + }})(); if(Object.keys(S.pops).length)for(let j=0;jr.keys.every(k=>edges[k]))){const prev=old.map(r=>{const path=r.keys.map(k=>edges[k]);return {path,ni:r.ns.map(c=>CIDX[c]),share:r.share};}); if(prev.reduce((x,r)=>x+cost(r),0)<=pick2.reduce((x,r)=>x+cost(r),0)*1.1)out=prev;} R2.l2route[key]=out.map(r=>({keys:r.path.map(e=>e.id),ns:cityIds(r.ni),share:r.share}));return out;}; - for(const b of S.biz){if(!BIZ_T[b.type].l2)continue; + (()=>{for(const b of S.biz){if(!BIZ_T[b.type].l2)continue; // An SD-WAN is hub-and-spoke: one circuit from the first site to each of the others. const pairs=b.sites?b.sites.slice(1).map(x=>[b.sites[0],x]):[[b.city,b.city2]];let minF=1,maxLat=0,tot=0,noPath=false; for(const [A,B] of pairs){let amt=0; @@ -158,7 +168,7 @@ function compute(){RW.gen++; minF=Math.min(minF,amt/b.bw);tot+=amt;} R2.l2lat[b.id]=noPath?Infinity:maxLat;R2.l2[b.id]=b.bw*minF;R2.egT.l2=(R2.egT.l2||0)+tot; // Same answer as diversePaths(), which would rebuild today's live links for every customer. - if((b.reqs||[]).includes('diverse')){const k=b.city+'>'+b.city2;R2.div[b.id]=divMemo[k]??(divMemo[k]=disjointI(b.city,b.city2));}} + if((b.reqs||[]).includes('diverse')){const k=b.city+'>'+b.city2;R2.div[b.id]=divMemo[k]??(divMemo[k]=disjointI(b.city,b.city2));}}})(); /* Floods ride the backbone ahead of everyone's traffic (25-ddos-flood.js). haul: up to g from city A to POP B over the shortest path, as far as links and routers have room. */ const msOf=(A,B)=>{const Bi=CIDX[B],Ai=CIDX[A];return SPD[B]&&SPD[B][Ai]n+e.lat,0):0;}; @@ -172,7 +182,7 @@ function compute(){RW.gen++; if(c0===false)return null;if(c0&&c0.path.every(l=>l.res>1e-3)&&c0.ni.every(i=>!HAS[i]||NRES[i]>1e-3))return c0; const {D,P}=dijkstra(adj,src,wW,(v,e)=>e.res<=1e-3||(HAS[v]&&NRES[v]<=1e-3)||(CORE&&OUT[v]&&v!==dst),dst); if(D[dst]===Infinity||D[dst]>REACH*base+60){teCache.set(key,false);return null;} - const Q=walkI(P,src,dst);teCache.set(key,Q);return Q;}; + const Q=walkP(P,src,dst);teCache.set(key,Q);return Q;}; /* A network in a peer's customer cone, reached through that peer (#85): after every network you peer with directly (its AS path is one hop longer) and before transit. */ const base=(e,n)=>e.pref==='low'?4:e.rank>0&&e.rank<3&&coneOf(e,n)?CONE_RANK:e.rank; @@ -190,13 +200,13 @@ function compute(){RW.gen++; // Each network's exits by the rank they route at (RouteScript can move an exit per network), in E's order. // Each list also knows whether it holds a preferred exit (hi) and its exits by city (at), in list order. // With several islands (POPs no backbone joins), each list is split by island: a flow only scans exits it can reach. - const idx=L=>{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]);}}; // Networks with the same exits at a rank share one list (LIST, by content), so what one flow learns serves them all. - const LIST=new Map();for(let i=0;i{for(let j=0;je.xi).join(','),o=LIST.get(key);if(o){m[r]=o;continue;} let V=L;if(NCOMP>1){const cs=[];for(const e of L){const k=COMP[e.ci];if(k>=0)(cs[k]||(cs[k]=[])).push(e);}for(const s of cs)if(s)idx(s);V={cs};}else idx(L);LIST.set(key,V);m[r]=V;}} + if(RCH)rcExitKeep(XK,E,okBy,RCH,LIST,BRJ,DSEL,RSET);})(); // The flow being routed, and from its POP: distances, arriving edges and the paths walked so far, by city index. // F: its index; fci, fnj: its city and network index. let F=0,fci=0,fnj=0,Dv=null,Pv=null,PP=null,vis=0;const walked=[],G=[],REST=[],NEG=[]; @@ -220,7 +230,7 @@ function compute(){RW.gen++; // Backhaul to transit: users notice the round trip to the exit. Up to 20 ms (about 1,500 km) is free, then // it gets worse, and past 50 ms broadband and internet services count as bad service (#…). Under sender pays, // Korean users notice any exit far away, transit or not, and much sooner. - if(path.length&&(e.k==='t'||snd&&CITIES[ci].kr)){let ms=P.ms;if(ms===undefined){ms=0;for(let k=0;k(kl?KR_LAT_OK:LAT_OK)){LATP[ci]+=ok*(kl?krLatLoss(rtt):latLoss(rtt));if(!P.fg){P.fg=0;FARL.push(P,ci,e.ci);}P.fg+=ok;}}R2.egT[e.k]=(R2.egT[e.k]||0)+ok;const ne=NEG[nr]||(NEG[nr]=R2.netEg[NET_IDS[nr]]={c:0,p:0,ix:0,t:0});ne[e.k]+=ok; // Where each flow left (the PNI list asks what a PNI here would take over, #175). if(e.J>0){const m=e.byC||(e.byC=new Map()),v=m.get(ci);if(v){v[0]+=ok;if(bz)v[1]+=ok;}else m.set(ci,[ok,bz?ok:0]);} @@ -231,7 +241,7 @@ function compute(){RW.gen++; // with free capacity, as long as they are no more than twice as long. const send=(e,want)=>{ let room=Math.min(want,FREM[F],e.cap-e.used);if(room<=1e-6)return; - const P=PP[e.ci]||(PP[e.ci]=walkI(Pv,fci,e.ci));if(!P.dead)room-=push(e,P,room,TE&&SPILL<1?1-SPILL:0); + const P=PP[e.ci]||(PP[e.ci]=walkP(Pv,fci,e.ci));if(!P.dead)room-=push(e,P,room,TE&&SPILL<1?1-SPILL:0); if(TE&&room>1e-6&&FREM[F]>1e-6&&e.used1e-6;k++){const P=altPath(fci,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 @@ -261,11 +271,11 @@ function compute(){RW.gen++; else for(let li=0;li1e-6;){let j=i+1;while(j{ for(F=0;F{ if(DD){let rem=0,jf=0;for(let i=0;i1e-6)rem+=FREM[i];if(JK)for(const k in edges){const e=edges[k];if(e.res<=1e-6&&e.jk)jf+=e.jk;} ddosAfter(DD,{E,R2,squeeze:Math.min(rem,jf), lost:(ci,x,y,e)=>{SRV[ci]-=x;R2.served-=x;const c=CITIES[ci].id;if(y)R2.bizSrv[c]-=y;R2.egT[e.k]-=x;if(e.rank<=2)R2.peered-=x;}});} @@ -318,8 +333,14 @@ function compute(){RW.gen++; for(const k in edges){const e=edges[k],ld=e.ld||0,ac=e.cap-e.scap,a=Math.min(ld,ac),sp=ld-a;for(const l of e.links){const c=capOf(l);R2.load[l.id]=l.standby?(e.scap>0?sp*c/e.scap:0):(ac>0?a*c/ac:0);}} for(const e of TORD){const m=R2.tNet[e.id]={},b=e.ts*NN;for(const j of TNK[e.ts])m[NET_IDS[j]]=TNA[b+j];} R2.peerShare=R2.served>0?R2.peered/R2.served:0; - for(const e of E){if(e.k==='t')R2.useT[e.id]=(R2.useT[e.id]||0)+e.used;else if(e.k==='ix')R2.useIx[e.id]=e.used;else if(e.k==='p')R2.usePni[e.id]=e.used;else R2.useCache[e.id]=e.used;} - // money + for(const e of E){if(e.k==='t')R2.useT[e.id]=(R2.useT[e.id]||0)+e.used;else if(e.k==='ix')R2.useIx[e.id]=e.used;else if(e.k==='p')R2.usePni[e.id]=e.used;else R2.useCache[e.id]=e.used;}})(); + computeMoney(R2); + // R.fl: from this call's own columns, made into objects only when something asks. + flDefine(R2,{NF,FREM,FCI,FNJ,FCA,FBZ,FGT,FGI,FGP,FGH,FTI,FII,E,CNB,NCN}); + R=R2;UI.hotMap=hotByCity(); +} +// What the network costs and earns per month, from what compute() routed. +function computeMoney(R2){ const C=R2.cost,V=R2.rev,pm=S.priceMul; C.pops=Object.entries(S.pops).reduce((a,[c,p])=>a+popMo(c)+((p.routers||1)-1)*ROUTER_MO+cageMo(p.routers)+(p.routers||1)*RG[p.rgen||0].mo+(p.gen?GEN_MO:0),0); C.optical=S.links.filter(l=>l.kind==='df').reduce((a,l)=>a+(l.ready>S.day?0:ampCount(l.km)*AMP_MO*(l.own&&isRdm(l.sys)&&!l.sub?Math.max(1,l.pairs|0):1)+(l.regen|0)*REGEN_MO)+(l.spares?Math.round(150+(l.ch||[]).length*80):0),0); @@ -338,7 +359,4 @@ function compute(){RW.gen++; C.revshare=(S.loans||[]).reduce((a,l)=>a+(BANKS[l.bank].equity||l.bal>0?l.share||0:0),0)*(V.eyeballs+V.business+V.ix); R2.costMo=Object.values(C).reduce((a,b)=>a+b,0); R2.revMo=V.eyeballs+V.business+V.ix+V.grant; - // R.fl: from this call's own columns, made into objects only when something asks. - flDefine(R2,{NF,FREM,FCI,FNJ,FCA,FBZ,FGT,FGI,FGP,FGH,FTI,FII,E,CNB,NCN}); - R=R2;UI.hotMap=hotByCity(); } diff --git a/tests/route-cache.test.js b/tests/route-cache.test.js new file mode 100644 index 0000000..0516672 --- /dev/null +++ b/tests/route-cache.test.js @@ -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!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, []); +}); diff --git a/tests/routing-reference.test.js b/tests/routing-reference.test.js index 850a21a..1c2622d 100644 --- a/tests/routing-reference.test.js +++ b/tests/routing-reference.test.js @@ -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', () => { diff --git a/tests/speed.test.js b/tests/speed.test.js index b3ac6f4..6c9b277 100644 --- a/tests/speed.test.js +++ b/tests/speed.test.js @@ -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')); }