/* The routing core: the loops that place every flow on an exit (compute() in 30-compute.js does everything around them). They are made once, here, and not inside compute() as they used to be: V8 compiles a function for the objects it meets, and every compute() made new copies of these, so each routing threw the optimised code away (a new function where it had inlined the old one) and ran a good part of its loops unoptimised until it was compiled again. Made once, they stay compiled from one day to the next. compute() hands them the day's state: nodes() what floods and circuits carry before routing (nodeTake(), carry()), run() the rest, then run() routes every pass. The code is the same code; only where it lives changed (tests/routing-reference.test.js, tests/speed.test.js, scripts/route-fuzz.js). */ function routeCore(){ // The day's state, set by nodes() and run(). (TR, base and eff, the ranks, are for the reference loop of the tests.) let NRES,NSEEN,NORD,NLOAD,EL,NKN,SEN,ACN,NC,SED,ACD, E,HAS,PASS,R2,FREM,FNJ,FCI,FCA,FBZ,NF,CNB,NCN,ev,day,SRV,TNS,NN,TNK,TORD,TNA,snd,LATP,FARL,EGT,EGO,NEG,NEO,NEA,FGT,FTI,FGI,FII,FGH,FGP, TE,SPILL,teCache,adj,CORE,OUT,REACH,DSEL,PREFC,BRJ,COMP,SDi,SPi,ALONE,TR,base,eff, NX=0,USED=null,CAPX=null,CIX=null,HPX=null,DTX=null,VISX=null,TEF=null,EP=0; // 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. BRX: BRJ with each list as a list object of one shape (lx()). let F=0,fci=0,fnj=0,Dv=null,Pv=null,PP=null,vis=0,GN=0,walked=[],BRX=[];const G=[],REST=[]; const wW=e=>e.w; // Each network's list at one rank, in an array by network index: BRX[j] are objects of as many shapes as networks have // sets of ranks, and reading one per flow made the loop's code give up on them. const byRank=rank=>{const B=new Array(BRX.length);for(let j=0;j{const o=NRES[i];NRES[i]=o-a;if(o>1e-3&&NRES[i]<=1e-3)EP++;if(!NSEEN[i]){NSEEN[i]=1;NORD.push(i);}NLOAD[i]=(NLOAD[i]||0)+a;}; // t.g gives each egress a slot in t.v, so adding to it allocates nothing (a Map of numbers boxes every new sum). const carry=(l,eg,nr,ci,a)=>{let t=l.tr;if(!t){t=l.tr={g:new Map(),v:[],n:[],d:[]};EL.push(l);}const s=t.g.get(eg);if(s===undefined){t.g.set(eg,t.v.length);t.v.push(0+a);}else t.v[s]+=a; let k=l.i*NKN+nr;if(!SEN[k]){SEN[k]=1;t.n.push(nr);}ACN[k]=(ACN[k]||0)+a;k=l.i*NC+ci;if(!SED[k]){SED[k]=1;t.d.push(ci);}ACD[k]=(ACD[k]||0)+a;}; // TEF[src*NC+dst]: 1 once no detour exists for that pair (capacity only shrinks, so there never will be one again). // Most calls are those: a byte read, not a Map lookup. The kept path is still valid while every edge and node on it has room. const altPath=(src,dst,base)=>{const key=src*NC+dst;if(TEF[key]===1)return null;const c0=teCache.get(key); if(c0){let ok=true;const p=c0.path,ni=c0.ni;for(let k=0;k1e-3)){ok=false;break;} if(ok)for(let k=0;k1e-3)){ok=false;break;}} if(ok)return c0;} const d=teSearch(src,dst); if(d===Infinity||d>REACH*base+60){teCache.set(key,false);TEF[key]=1;return null;} const Q=walkP(DJ.P,src,dst);teCache.set(key,Q);return Q;}; /* Auto-TE's detour search (the one 10-dijkstra.js does to a stop), step for step: neighbours in the same order, the same heap, written into the same DJ (walkP() reads its paths, the reuse above its marks). Only on the graph as flat arrays (TEO offsets by city, TEN neighbours, TEE edges, made once per routing in run()) and with the test of an edge written out: no pairs to unpack and no call for each edge. On a big network with Auto-TE these searches were most of the routing. */ /* A search from one POP picks up where the last one from there stopped (LR*), as a search of its own would run: every city closer than the last exit is settled and the heap holds the rest, so the next exit has its answer already or the search goes on from there, the last exit's neighbours first (it stops before them). One that found nothing has walked all src reaches and answers the next ones at once. Only on the same graph: EP counts the edges and routers that run out of room (capacity only shrinks while routing, so none comes back), and another search in DJ moves its mark. An exit out of the core is let into a graph of its own (OUT[dst]): that search neither resumes nor is resumed. On a big network nearly all detours of a flow are found from one search (extreme: 3.6 M heap steps a routing → 0.17 M). */ let TEO=null,TEN=null,TEE=null,LRs=-1,LRe=-1,LRg=-1,LRD=null,LRu=-1,LRm=0; const teSearch=(src,dst)=>{const n=TEO.length-1,core=CORE,cut=!!(core&&OUT[dst]),go=LRs===src&&LRe===EP&&LRg===DJ.g&&LRD===DJ.D&&!cut; let g,m=0,u=-1; if(go){g=LRg;if(DJ.done[dst]===g)return DJ.D[dst];m=LRm;u=LRu;} else{if(DJ.n!==n||DJ.g>2e9){DJ.n=n;DJ.D=new Float64Array(n);DJ.P=new Array(n);DJ.S=new Int32Array(n);DJ.done=new Int32Array(n);DJ.g=0;}g=++DJ.g;} const D=DJ.D,P=DJ.P,ST=DJ.S,DN=DJ.done;let hd=DJ.hd,hu=DJ.hu,cap=hd.length; if(!go){D[src]=0;ST[src]=g; if(m===cap){cap*=2;const a=new Float64Array(cap),b=new Int32Array(cap);a.set(hd);b.set(hu);hd=DJ.hd=a;hu=DJ.hu=b;} let i=m++;hd[i]=0;hu[i]=src;while(i>0){const p=(i-1)>>1;if(hd[p]<=hd[i])break;const td=hd[p],tu=hu[p];hd[p]=hd[i];hu[p]=hu[i];hd[i]=td;hu[i]=tu;i=p;}} for(;;){ if(u>=0){const d=D[u];for(let k=TEO[u],K=TEO[u+1];k0){const p=(i-1)>>1;if(hd[p]<=hd[i])break;const td=hd[p],tu=hu[p];hd[p]=hd[i];hu[p]=hu[i];hd[i]=td;hu[i]=tu;i=p;}}}} if(!m){u=-1;break;} const d=hd[0];u=hu[0];m--; if(m){hd[0]=hd[m];hu[0]=hu[m];let i=0;for(;;){const l=2*i+1,r=l+1;let k=i;if(l(ST[u]===g?D[u]:Infinity)){u=-1;continue;}DN[u]=g;if(u===dst)break;} // An exit out of the core that was found sits in the heap of its own graph; one never found was never let in. if(cut&&u>=0)LRs=-1;else{LRs=src;LRe=EP;LRg=g;LRD=D;LRu=u;LRm=m;} return ST[dst]===g?D[dst]:Infinity;}; /* The routing loops work on exits by number (xi, their index in E): what they read for every exit they look at sits in columns, USED, CAPX, CIX, HPX, DTX and VISX, and the lists they scan hold numbers (lst.x, lst.xat). Scanning objects of thirty fields each read one cache line per exit; the columns keep a list's exits side by side. The objects are only read for an exit that takes traffic (push()), and e.used is written back once routing is done (run()). */ // An exit the flow can still try: not visited yet, not full when the flow started, reachable, and not on a saturated path. const usable=x=>VISX[x]!==vis&&USED[x]HPX[a]-HPX[b]||Dv[CIX[a]]-Dv[CIX[b]]+(DTX[a]-DTX[b]); // Push up to `want` along one path to exit e; returns what fitted. Index loops: for…of allocates an iterator until optimised. Capacity only shrinks while routing, so a // path with one full link or node fits nothing for the rest of this pass: mark it, and later tries cost nothing // (most tries are into paths that filled up earlier). const push=(x,P,want,keep=0)=>{if(P.dead)return 0;const e=E[x];const path=P.path,ni=P.ni;let amt=want; const PN=path.length;for(let k=0;k1e-3&&l.res<=1e-3)EP++;l.ld+=amt;carry(l,ek,nr,ci,amt);} USED[x]+=amt;FREM[F]-=amt; {let ok=amt;const cf=CNB[ci],ce=e.cn,cn=NCN[nr],bz=FBZ[F]===1; if(cf!==ce||(cf&&!cn)||(!cf&&cn&&e.k==='t')){const prem=(e.k==='t'&&e.prem)||(e.k==='p'&&e.pnc)||path.some(x=>x.links&&x.links.some(y=>y.premium));const loss=prem?0.03:(ev.gfw>day?0.35:0.2);R2.gfwLost+=amt*loss;R2.gfwCross+=amt;ok=amt*(1-loss);} SRV[ci]+=ok;R2.served+=ok;if(bz)R2.bizSrv[CITIES[ci].id]+=ok;if(e.rank<=2)R2.peered+=ok;if(e.k==='t'){const k=e.ts*NN+nr;if(!TNS[k]){TNS[k]=1;if(!TNK[e.ts]){TNK[e.ts]=[];TORD.push(e);}TNK[e.ts].push(nr);}TNA[k]+=ok;} // 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<0){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;}}const kx=e.kx;if(EGT[kx]===0&&!EGO.includes(kx))EGO.push(kx);EGT[kx]+=ok;if(!NEG[nr]){NEG[nr]=R2.netEg[NET_IDS[nr]]={c:0,p:0,ix:0,t:0};NEO.push(nr);}NEA[nr*4+kx]+=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]);} if(e.k==='t'){FGT[F]+=ok;FTI[F]=e.xi;}else if(e.k==='ix'){FGI[F]+=ok;FII[F]=e.xi;}else if(e.k==='p'){if(e.ci===ci&&e.net===NET_IDS[nr])FGH[F]+=ok;else FGP[F]+=ok;}} return amt; }; // Shortest path first. With Auto-TE, whatever does not fit spills onto up to two alternate paths // with free capacity, as long as they are no more than twice as long. const send=(x,want)=>{ let room=Math.min(want,FREM[F],CAPX[x]-USED[x]);if(room<=1e-6)return;const ci=CIX[x]; const P=PP[ci]||(PP[ci]=walkP(Pv,fci,ci));if(!P.dead)room-=push(x,P,room,TE&&SPILL<1?1-SPILL:0); if(TE&&room>1e-6&&FREM[F]>1e-6&&USED[x]1e-6;k++){const P=altPath(fci,ci,Dv[ci]);if(!P)break;const m=push(x,P,Math.min(room,CAPX[x]-USED[x]));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. // 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=x=>{if(TE)return false;const P=PP[CIX[x]];return !!P&&P.dead===1;}; // Every exit of a remembered group still has room (a loop, not every(): no closure per flow). NOG: no group. const allRoom=c=>{for(let k=0;k{const ci=CIX[x],P=PP[ci];return P!==undefined&&P.dead===1&&(!TE||ci===fci||TEF[fci*NC+ci]===1);}; const ROOM=[],sendGroup=(g,i0=0,i1=g.length)=>{let live=false;for(let k=i0;k1){let tot=0;for(let k=i0;k1e-6)for(let k=i0;k{if(n>24){if(n===a.length)a.sort(byDist);else{const t=a.slice(0,n).sort(byDist);for(let i=0;i=0&&byDist(a[j],x)>0){a[j+1]=a[j];j--;}a[j+1]=x;}}; // The direct ranks (25-direct-egress.js) have short lists, one carrier's ports, so they take the plain way: the usable exits // nearest first, tried in groups of equal distance, as the reference loop in the tests does. A direct rank only concerns the // networks that have an exit there, and nothing from a POP where Prefer has the say; a port that is not set to carry only takes // its own POP's traffic. Kept in a function of its own so the loop below stays as it was: the JIT is sensitive to its shape. const directPass=(rank,pass)=>{const DS=DSEL.get(rank),LOC=rank===DIRECT_RANK[1][1]||rank===DIRECT_RANK[2][1],BR=byRank(rank); for(F=0;F1e-6;){let j=i+1;while(j{const BR=byRank(rank); for(F=0;F=CAPX[A[k]]||gone(A[k])))k++;cur[fci]=k; if(k===N){gc[fci]=NOG;continue;} const bh=HPX[A[k]],bd=Dv[CIX[A[k]]]+DTX[A[k]]; for(let i=k;i=1e-6)break;if(USED[x]=CAPX[e]||gone(e)){full++;continue;}const d=Dv[CIX[e]]+DTX[e];if(d===Infinity)continue;const h=HPX[e]; if(h=L.length&&full){let k=0;for(let li=0;li1){gc[fci]=NOG;continue;} if(GN>1)sortD(G,GN);gc[fci]=G.slice(0,GN);}}} sendGroup(G,0,GN); if(FREM[F]<=1e-6)continue; // What is left, nearest first: with a kept order, the usable exits in it (the order sorting them gives). REST.length=0;if(lst.ord){const A=xOrder(lst,fci);for(let i=lst.cur[fci]|0;i1e-6;){ let j=i+1;while(j{let a=lst.ord[ci];if(a===undefined){const b=[],L=lst.x; for(let i=0;i{let w=W.get(A);if(w)return w; if(A.cs)w={x:null,xat:null,hi:false,mx:false,gc:null,ord:null,cur:null,cs:A.cs.map(s=>s&&lx(s,W))}; else{const xat=new Map();for(const [c,h] of A.at)xat.set(c,h.map(e=>e.xi));w={x:A.map(e=>e.xi),xat,hi:!!A.hi,mx:!!A.mx,gc:[],ord:A.ord||null,cur:[],cs:null};} W.set(A,w);return w;}; return {nodeTake,carry, nodes(o){({NRES,NSEEN,NORD,NLOAD,EL,NKN,SEN,ACN,NC,SED,ACD}=o);}, run(o,PASSES,RANKS,RSET){({E,HAS,PASS,R2,FREM,FNJ,FCI,FCA,FBZ,NF,CNB,NCN,ev,day,SRV,TNS,NN,TNK,TORD,TNA,snd,LATP,FARL,EGT,EGO,NEG,NEO,NEA,FGT,FTI,FGI,FII,FGH,FGP, TE,SPILL,teCache,adj,CORE,OUT,REACH,DSEL,PREFC,BRJ,COMP,SDi,SPi,ALONE,TR,base,eff}=o); NX=E.length;USED=new Float64Array(NX);CAPX=new Float64Array(NX);CIX=new Int32Array(NX);HPX=new Uint8Array(NX);DTX=new Float64Array(NX);VISX=new Int32Array(NX); for(let x=0;x