Routing core made once, list orders kept, and Auto-TE's detour searches cut: same routing, much less work

The game plays exactly as before.

* The core of compute() (rankPass, directPass, sendGroup, send, push, carry, altPath) is ROUTE in 29-route-core.js,
  built once by routeCore(): made inside compute() they were new functions every call and V8 threw away their code.
  Exits are read by number from typed columns (USED, CAPX, CIX, HPX, VISX), each list is an object of one shape.
* The route cache keeps, for each list and source city, the order routing tries it (xOrder()); a list whose exits,
  cities and preferences are unchanged on the same graph keeps its orders when other exits change (rcExitKeep()).
* Auto-TE: pairs with no detour are marked (TEF), exits that cannot help are skipped (dud()), one failed search
  answers the next ones, dijkstraTo() searches in arrays kept between calls, teSearch() runs on the graph as flat
  arrays and picks up where the last search from the same POP stopped.
* The PNI/IX tie-break (#298) reaches the core.

Tests: route-cache, routing-reference, speed (the fast core against the plain path, Auto-TE on crowded meshes with
POPs outside the core, picked-up searches counted).
This commit is contained in:
Antoine 2026-11-07 22:21:23 +01:00
parent d03dde6fcd
commit 1edf0abb54
No known key found for this signature in database
GPG key ID: 728FF1CE15732B05
10 changed files with 409 additions and 159 deletions

View file

@ -6,6 +6,8 @@ All notable changes to this project are documented here. The project follows
## [Unreleased]
- Routing a big network's day takes about half the work, with exactly the same results: on a network of 390 POPs with Auto-TE, a day's routing went from about 0.7 to 0.3 seconds on a computer.
## [3.2.0] — 2026-11-07
- New badges for traffic: serve 1 Tbps and 10 Tbps on the way to the 100 Tbps and petabit ones, and carry a petabyte, 100 petabytes, an exabyte and a zettabyte in one game. The dashboard shows how much traffic your network has carried so far, and only the top step of each ladder is legendary: Hundred Terabits is now epic.

View file

@ -181,10 +181,20 @@ them. Only indices are kept: exits, lists and paths are made fresh every call, b
`scripts/route-fuzz.js` plays hundreds of random seeded games on main's page and this checkout's at once, comparing all
of R and S after every move and day, with the kept tables and without (`tests/route-fuzz.test.js` plays a few).
Each loop of `compute()` runs in a function of its own (the stages are closures, the busiest is `rankPass`;
the money is `computeMoney()`). V8 optimises a small function quickly and keeps it; `compute()` as one 33 KB function
never stayed optimised: it ran in baseline code, boxing every number, and was compiled again and again in the background
only to be thrown away, which cost more CPU than the routing itself. Keep it that way: a new loop goes in a function.
Each loop of `compute()` runs in a function of its own (the money is `computeMoney()`). V8 optimises a small function
quickly and keeps it; `compute()` as one 33 KB function never stayed optimised: it ran in baseline code, boxing every
number, and was compiled again and again in the background only to be thrown away, which cost more CPU than the routing
itself. Keep it that way: a new loop goes in a function.
The routing core, the loops that place every flow on an exit (`rankPass`, `directPass`, `sendGroup`, `send`, `push`,
`carry`, `altPath`), lives in `ROUTE` (`29-route-core.js`), made once at load: `compute()` hands it the day's state
(`ROUTE.nodes()`, `ROUTE.run()`). Made inside `compute()`, as they were, they were new functions every call, and V8 threw
away the code it had compiled for the old ones. The core reads exits by number from typed columns (`USED`, `CAPX`, `CIX`,
`HPX`, `VISX`), and each list as an object of one shape (`lx()`). For each list and source city, the route cache also
keeps the list in the order routing tries it (`xOrder()`: by preference, then distance, a stable sort): on a day the cache
is used, a flow's first group is where that order starts, past the exits that filled up, instead of a scan of the whole
list, and what is left needs no sort. Auto-TE marks the pairs that have no detour (`TEF`), the answer to most of its
asks. `tests/day-steps.test.js` and the routing equivalence tests hold all this to the same results.
Served share per city (`sfOf`) drives eyeball revenue and customer mood.

View file

@ -13,6 +13,21 @@ function dijkstra(adj,src,wf,skip,stop){const n=adj.length,D=new Float64Array(n)
if(done[u]||d>D[u])continue;done[u]=1;if(u===stop)break;
for(const [v,e] of adj[u]||[]){if(skip&&skip(v,e))continue;const nd=d+wf(e);if(nd<D[v]){D[v]=nd;P[v]=e;put(nd,v);}}}
return {D,P};}
/* dijkstra() for one path thrown away at once (Auto-TE's detours, thousands in a big network's routing): the same search,
heap and order, in arrays kept between calls instead of three new ones each time. A stamp per city says whether this
call wrote its distance (DJ.S), so nothing is cleared. Returns the distance to `stop`; the path is in DJ.P. */
const DJ={n:0,g:0,D:null,P:null,S:null,done:null,hd:new Float64Array(64),hu:new Int32Array(64)};
function dijkstraTo(adj,src,wf,skip,stop){const n=adj.length;
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;}
const g=++DJ.g,D=DJ.D,P=DJ.P,ST=DJ.S,DN=DJ.done;let hd=DJ.hd,hu=DJ.hu,cap=hd.length,m=0;
const put=(d,u)=>{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]=d;hu[i]=u;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;}};
D[src]=0;ST[src]=g;put(0,src);
while(m){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<m&&hd[l]<hd[k])k=l;if(r<m&&hd[r]<hd[k])k=r;if(k===i)break;const td=hd[k],tu=hu[k];hd[k]=hd[i];hu[k]=hu[i];hd[i]=td;hu[i]=tu;i=k;}}
if(DN[u]===g||d>(ST[u]===g?D[u]:Infinity))continue;DN[u]=g;if(u===stop)break;
for(const [v,e] of adj[u]||[]){if(skip&&skip(v,e))continue;const nd=d+wf(e);if(nd<(ST[v]===g?D[v]:Infinity)){D[v]=nd;ST[v]=g;P[v]=e;put(nd,v);}}}
return ST[stop]===g?D[stop]:Infinity;}
// 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.

View file

@ -39,14 +39,26 @@ function exIdx(L){L.hi=L.some(e=>e.hp===0);L.mx=L.some(e=>e.dt>0)&&L.some(e=>e.d
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<NET_IDS.length;j++)ok[j]=rcIx(okBy[NET_IDS[j]],E);
/* A list's orders (by source city) follow from its exits in list order, each exit's city and preference, and the graph's
distances. When the exits change (a PNI comes up, a network leaves an exchange), most lists do not: one with the same
exits, cities and preferences on the same graph keeps the orders it had, and uses them today already. Only the lists
that changed start again (scanned today, sorted from tomorrow). */
function rcExitKeep(key,E,okBy,reach,LIST,BRJ,DSEL,RSET,bk=''){const ok=[];for(let j=0;j<NET_IDS.length;j++)ok[j]=rcIx(okBy[NET_IDS[j]],E);
const sig=new Int32Array(E.length);for(let i=0;i<E.length;i++)sig[i]=E[i].ci*4+E[i].hp;
const P=RC.x&&RC.x.gk===RC.gk&&RC.x.bk===bk&&RC.x.sig?RC.x:null,old=new Map();
if(P)for(const d of P.lists){if(d.cs){for(let k=0;k<d.cs.length;k++)if(d.cs[k])old.set(k+'/'+d.cs[k].join(','),d.ord[k]);}else old.set(d.a.join(','),d.ord);}
const same=a=>{for(let i=0;i<a.length;i++){const x=a[i];if(x>=P.sig.length||P.sig[x]!==sig[x])return false;}return true;};
const kept=(k,a)=>{if(!P)return null;const o=old.get(k);return o&&same(a)?o:null;};
const id=new Map(),lists=[];for(const V of LIST.values()){id.set(V,lists.length);
if(V.cs){const cs=[];for(let k=0;k<V.cs.length;k++)if(V.cs[k])cs[k]=rcIx(V.cs[k],E);lists.push({cs,a:null});}else lists.push({cs:null,a:rcIx(V,E)});}
if(V.cs){const cs=[],ord=[];for(let k=0;k<V.cs.length;k++)if(V.cs[k]){cs[k]=rcIx(V.cs[k],E);const o=kept(k+'/'+cs[k].join(','),cs[k]);ord[k]=o||[];if(o)V.cs[k].ord=o;}lists.push({cs,a:null,ord});}
else{const a=rcIx(V,E),o=kept(a.join(','),a);lists.push({cs:null,a,ord:o||[]});if(o)V.ord=o;}}
const brj=BRJ.map(m=>{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]};}
RC.xk=key;RC.x={ok,reach,lists,brj,DSEL,RSET:[...RSET],gk:RC.gk,bk,sig};}
// 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;k<d.cs.length;k++)if(d.cs[k]){const s=rcEx(d.cs[k],E);exIdx(s);cs.cs[k]=s;}return cs;}
const L=rcEx(d.a,E);exIdx(L);return L;});
// Each list also gets the orders kept with it (ord: by source city, the list in the order routing tries it, see xOrder()
// in 30-compute.js), filled in as routing asks for them.
function rcExitLists(X,E,BRJ,DSEL,RSET){const V=X.lists.map(d=>{if(d.cs){const cs={cs:[]};for(let k=0;k<d.cs.length;k++)if(d.cs[k]){const s=rcEx(d.cs[k],E);exIdx(s);s.ord=d.ord[k];cs.cs[k]=s;}return cs;}
const L=rcEx(d.a,E);exIdx(L);L.ord=d.ord;return L;});
for(let j=0;j<X.brj.length;j++){const o=X.brj[j],m={};for(let k=0;k<o.length;k+=2)m[o[k]]=V[o[k+1]];BRJ[j]=m;}
for(const [r,a] of X.DSEL)DSEL.set(r,a);for(const r of X.RSET)RSET.add(r);}
function rcOkBy(X,E){const okBy={};for(let j=0;j<NET_IDS.length;j++)okBy[NET_IDS[j]]=rcEx(X.ok[j],E);return okBy;}

View file

@ -0,0 +1,224 @@
/* 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<BRX.length;j++){const l=BRX[j][rank];B[j]=l===undefined?null:l;}return B;};
const nodeTake=(i,a)=>{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;k<p.length;k++)if(!(p[k].res>1e-3)){ok=false;break;}
if(ok)for(let k=0;k<ni.length;k++){const i=ni[k];if(HAS[i]&&!(NRES[i]>1e-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];k<K;k++){const v=TEN[k],e=TEE[k];if(e.res<=1e-3||(HAS[v]&&NRES[v]<=1e-3)||(core&&OUT[v]&&v!==dst))continue;
const nd=d+e.w;if(nd<(ST[v]===g?D[v]:Infinity)){D[v]=nd;ST[v]=g;P[v]=e;
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]=nd;hu[i]=v;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;}}}}
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<m&&hd[l]<hd[k])k=l;if(r<m&&hd[r]<hd[k])k=r;if(k===i)break;const td=hd[k],tu=hu[k];hd[k]=hd[i];hu[k]=hu[i];hd[i]=td;hu[i]=tu;i=k;}}
if(DN[u]===g||d>(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]<CAPX[x]&&Dv[CIX[x]]<Infinity&&!gone(x),byDist=(a,b)=>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;k<PN;k++){const l=path[k],r=l.res;if(r<=1e-6){P.dead=1;return 0;}amt=Math.min(amt,r-l.cap*keep);}
const last=ni.length-1;
for(let k=0;k<=last;k++){const i=ni[k];if(!HAS[i])continue;const r=NRES[i];if(r<=1e-6){P.dead=1;return 0;}const w=k&&k<last?PASS[i]:1;amt=Math.min(amt,r/w);}
if(amt<=1e-6)return 0;
for(let k=0;k<=last;k++){const i=ni[k];if(HAS[i])nodeTake(i,k&&k<last?amt*PASS[i]:amt);}
const ek=e.ek,nr=fnj,ci=fci;if(!e.told||e.dup){R2.egDesc[ek]=e.desc||(e.desc={lab:e.lab,city:e.city,k:e.k});e.told=true;}
for(let k=0;k<PN;k++){const l=path[k],o=l.res;l.res=o-amt;if(o>1e-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<PN;k++)ms+=path[k].lat;P.ms=ms;}const rtt=2*ms,kl=snd&&CITIES[ci].kr;
if(rtt>(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]<CAPX[x]&&ci!==fci)for(let k=0;k<2&&room>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<c.length;k++){const x=c[k];if(!(USED[x]<CAPX[x])||gone(x))return false;}return true;},NOG=[];
/* dud(x): send() to x would do nothing, its path from here is full and, with Auto-TE, no detour exists (TEF) or the
exit is here. Both last for the rest of the routing. Most exits a flow tries once its first group is full are such
(seven in eight sends on a big network): they keep their share of a group's room, but are not called. */
const dud=x=>{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;k<i1;k++){const x=g[k];VISX[x]=vis;if(!live&&!dud(x))live=true;}
if(!live)return;
if(i1-i0>1){let tot=0;for(let k=i0;k<i1;k++){const x=g[k],r=Math.max(0,CAPX[x]-USED[x]);ROOM[k-i0]=r;tot+=r;}const want=FREM[F];if(tot>1e-6)for(let k=i0;k<i1;k++)if(!dud(g[k]))send(g[k],want*ROOM[k-i0]/tot);}
for(let k=i0;k<i1;k++){if(FREM[F]<=1e-6)break;if(!dud(g[k]))send(g[k],FREM[F]);}};
// 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.
// n: sort only a[0..n) (the group G holds GN exits).
const sortD=(a,n=a.length)=>{if(n>24){if(n===a.length)a.sort(byDist);else{const t=a.slice(0,n).sort(byDist);for(let i=0;i<n;i++)a[i]=t[i];}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;}};
// 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;F<NF;F++){fnj=FNJ[F];fci=FCI[F];
if(FREM[F]<=1e-6||DS[fnj]===0||PREFC[fci]===1||(pass!==0&&FBZ[F]!==(pass===1?1:0)))continue;
let lst=BR[fnj];if(!lst)continue;if(lst.cs){lst=lst.cs[COMP[fci]];if(!lst)continue;}
Dv=SDi[fci];Pv=SPi[fci];PP=walked[fci]||(walked[fci]=[]);vis++;
REST.length=0;
if(LOC){const here=lst.xat.get(fci);if(!here)continue;for(let h=0;h<here.length;h++)if(usable(here[h]))REST.push(here[h]);}
else{const L=lst.x;for(let li=0;li<L.length;li++)if(usable(L[li]))REST.push(L[li]);}
sortD(REST);
for(let i=0;i<REST.length&&FREM[F]>1e-6;){let j=i+1;while(j<REST.length&&HPX[REST[j]]===HPX[REST[i]]&&Math.abs(Dv[CIX[REST[j]]]-Dv[CIX[REST[i]]]+(DTX[REST[j]]-DTX[REST[i]]))<1e-6)j++;sendGroup(REST,i,j);i=j;}}};
/* One rank of one pass over every flow. Each hot loop of compute() sits in a function of its own: V8 optimises a small
function quickly and keeps it, while compute() as a whole is too big to optimise cheaply (it would run unoptimised,
boxing every number, and be compiled again and again in the background). The tables of a flow's city (distances,
paths) are set up only once it has an exit list at this rank. */
const rankPass=(rank,pass)=>{const BR=byRank(rank);
for(F=0;F<NF;F++){
if(FREM[F]<=1e-6||(rank===0&&FCA[F]===0)||(pass!==0&&FBZ[F]!==(pass===1?1:0)))continue;
fnj=FNJ[F];let lst=BR[fnj];if(!lst)continue;fci=FCI[F];if(lst.cs){lst=lst.cs[COMP[fci]];if(!lst)continue;}
Dv=SDi[fci];Pv=SPi[fci];PP=walked[fci]||(walked[fci]=[]);vis++;
/* Groups are tried nearest first, preferred exits before the rest, in the order a stable sort of the usable
exits gives. Exits only fill up once tried, so the usable ones stay the same until then. Nearly every flow
fits in its first group: find that one with a scan (nothing is visited yet), and sort only what is left
when it does not fit. Most often the group is the POP's own exits: with no preferred exit on the list
and no other city at distance 0, the usable ones here are exactly the first group, already in order.
The group is G[0..GN): emptying an array (length=0) gives back its storage, and the next push allocates it again. */
GN=0;
if(!lst.hi&&!lst.mx&&ALONE[fci]){const here=lst.xat.get(fci);if(here)for(let h=0;h<here.length;h++){const x=here[h];if(USED[x]<CAPX[x]&&!gone(x))G[GN++]=x;}}
if(!GN){
// One scan: the best (preference, distance) so far and, in list order, the exits within 1e-6 of it. The best
// 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.
/* gc[ci]: the first group this list gave a flow from city ci. Exits only fill up during routing, so while all of
them still have room no other exit can have become better, and the scan would find the same group (an
empty one, no usable exit, stays empty). */
const gc=lst.gc,c=gc[fci];
if(c!==undefined&&allRoom(c)){if(!c.length)continue;for(let k=0;k<c.length;k++)G[GN++]=c[k];}else{
if(lst.ord){
/* The list in this city's order, kept by the route cache (xOrder()): the first group is where it starts, past
the exits that are full or on a full path. Those stay so for the rest of the routing, so the start (cur) only
moves on. The group is the next exits of the same preference within 1e-6 of the first one's distance, in the
order the sort gave them: what the scan below finds. */
const A=xOrder(lst,fci),N=A.length,cur=lst.cur;let k=cur[fci]|0;while(k<N&&(USED[A[k]]>=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<N;i++){const x=A[i];if(HPX[x]!==bh||Dv[CIX[x]]+DTX[x]-bd>=1e-6)break;if(USED[x]<CAPX[x]&&!gone(x))G[GN++]=x;}
gc[fci]=G.slice(0,GN);}else{
let bh=2,bd=Infinity,full=0;const L=lst.x;
for(let li=0,LN=L.length;li<LN;li++){const e=L[li];if(USED[e]>=CAPX[e]||gone(e)){full++;continue;}const d=Dv[CIX[e]]+DTX[e];if(d===Infinity)continue;const h=HPX[e];
if(h<bh){bh=h;bd=d;GN=0;G[GN++]=e;}
else if(h===bh){if(d<bd){bd=d;let k=0;for(let gi=0;gi<GN;gi++){const x=G[gi];if(Dv[CIX[x]]+DTX[x]-d<1e-6)G[k++]=x;}GN=k;G[GN++]=e;}else if(d-bd<1e-6)G[GN++]=e;}}
if(full*4>=L.length&&full){let k=0;for(let li=0;li<L.length;li++){const e=L[li];if(USED[e]<CAPX[e])L[k++]=e;}L.length=k;}
if(bh>1){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;i<A.length;i++)if(usable(A[i]))REST.push(A[i]);}
else{const L=lst.x;for(let li=0;li<L.length;li++)if(usable(L[li]))REST.push(L[li]);sortD(REST);}
for(let i=0;i<REST.length&&FREM[F]>1e-6;){
let j=i+1;while(j<REST.length&&HPX[REST[j]]===HPX[REST[i]]&&Math.abs(Dv[CIX[REST[j]]]-Dv[CIX[REST[i]]]+(DTX[REST[j]]-DTX[REST[i]]))<1e-6)j++;
sendGroup(REST,i,j);i=j;
}
}};
/* xOrder(lst, ci): the list's exits that city ci reaches, in a stable sort by preference and distance from ci (byDist,
the comparison the scans sort their finds with). It only follows from the network's shape (distances, the exits and
how they are preferred), so the route cache keeps it with the list (lst.ord, 29-route-cache.js): made the first time
a flow from ci uses the list on a day the cache is used, then read every day until the shape changes. A list made
fresh (the shape just changed) has no lst.ord and is scanned instead: one scan costs less than a sort. */
const xOrder=(lst,ci)=>{let a=lst.ord[ci];if(a===undefined){const b=[],L=lst.x;
for(let i=0;i<L.length;i++){const x=L[i];if(Dv[CIX[x]]<Infinity)b.push(x);}b.sort(byDist);a=lst.ord[ci]=Int32Array.from(b);}return a;};
/* Each exit list once as a list object, all of one shape: x the exits by number in list order, xat by city, hi whether
one is preferred, gc the first group each city found, ord the orders the route cache keeps (null on a fresh build),
cur where each city's usable exits start in its order, cs the lists by island. */
const lx=(A,W)=>{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<NX;x++){const e=E[x];USED[x]=e.used;CAPX[x]=e.cap;CIX[x]=e.ci;HPX[x]=e.hp;DTX[x]=e.dt;}
F=0;fci=0;fnj=0;Dv=null;Pv=null;PP=null;vis=0;GN=0;walked=[];EP=0;LRs=-1;LRD=null;
if(TE){const n=adj.length;TEO=new Int32Array(n+1);let k=0;for(let u=0;u<n;u++){TEO[u]=k;const a=adj[u];if(a)k+=a.length;}TEO[n]=k;
TEN=new Int32Array(k);TEE=new Array(k);k=0;for(let u=0;u<n;u++){const a=adj[u];if(a)for(let j=0;j<a.length;j++){TEN[k]=a[j][0];TEE[k]=a[j][1];k++;}}}TEF=TE?new Uint8Array(NC*NC):null;
const W=new Map();BRX=new Array(BRJ.length);for(let j=0;j<BRJ.length;j++){const m=BRJ[j],b={};for(const r in m)b[r]=lx(m[r],W);BRX[j]=b;}
for(const pass of PASSES)for(const rank of RANKS){
if(!RSET.has(rank))continue;
if(DSEL.has(rank)){directPass(rank,pass);continue;}
rankPass(rank,pass);
}
for(let x=0;x<NX;x++)E[x].used=USED[x];
// Nothing of this day is kept: the next run() sets its own.
E=R2=BRJ=BRX=SDi=SPi=teCache=TEF=TEE=null;walked=[];}};
}
// The core in use (a test can put the plain one in its place: tests/speed.test.js).
let ROUTE=routeCore();

View file

@ -67,7 +67,9 @@ function compute(){RW.gen++;
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:'')+'|'+(PX&&PX.bh.length?PX.bh.map(e=>e.bh.city+'>'+e.bh.land).join(','):'')):null,XC=XK!==null&&RC.xk===XK?RC.x:null;
// A backhaul through a dish changes a cut-off POP's distances: lists kept in order (29-route-cache.js) only for the same one.
const BHK=PX&&PX.bh.length?PX.bh.map(e=>e.bh.city+'>'+e.bh.land).join(','):'';
const XK=RCON?rcExitKey(GK,E,(depeer?1:0)+'|'+leak+'|'+(snd?1:0)+'|'+(rp?ev.rpki.nets.join(','):'')+'|'+(TRX?S.te.script:'')+'|'+BHK):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.
@ -125,12 +127,10 @@ function compute(){RW.gen++;
for(const k in edges)edges[k].res=edges[k].cap;for(const c of pops){const i=CIDX[c];NRES[i]=nodeCap(c);HAS[i]=1;R2.nodeCap[c]=NRES[i];}
// The core (backbone technology): what passing-through traffic costs each router, and which POPs TE must avoid.
const CORE=coreOn()&&S.core.run>=1;if(CORE)for(const c of pops){const i=CIDX[c];PASS[i]=corePass(c);OUT[i]=inCore(c)?0:1;}
const nodeTake=(i,a)=>{NRES[i]-=a;if(!NSEEN[i]){NSEEN[i]=1;NORD.push(i);}NLOAD[i]=(NLOAD[i]||0)+a;};
const TNA=new Float64Array(TSL.size*NN),TNS=new Uint8Array(TSL.size*NN),TNK=[],TORD=[];
const NKN=NN+3,ACN=new Float64Array(NE*NKN),SEN=new Uint8Array(NE*NKN),ACD=new Float64Array(NE*NC),SED=new Uint8Array(NE*NC),EL=[];
// 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;};
// The routing core (29-route-core.js) adds up what floods and circuits carry too.
ROUTE.nodes({NRES,NSEEN,NORD,NLOAD,EL,NKN,SEN,ACN,NC,SED,ACD});const nodeTake=ROUTE.nodeTake,carry=ROUTE.carry;
R2.l2route={};R2.l2split={};
/* compute() reads only S, except here: scenic circuits remember yesterday's path in R.l2route and R.efg (deliberate
hysteresis, so they do not flap). A second call can still move one; the third gives the same answer as the second. */
@ -187,11 +187,6 @@ function compute(){RW.gen++;
for(const i of ni)if(HAS[i])nodeTake(i,amt);for(const l of path){l.res-=amt;l.ld+=amt;if(!clean){l.jk=(l.jk||0)+amt;JK=1;}carry(l,ek,clean?NN+2:NN,Bi,amt);}return amt;};
const DD=ddosLoad({E,okBy,R2,autoBh:!!(TRX&&TRX.blackhole),haul,ms:msOf});
const TE=!!(S.te&&S.te.on)&&coreTE(),TR=TE?teRules():null,REACH=coreTeReach();R2.teMoved=0;const teCache=new Map(),SPILL=TR&&TR.spill?TR.spill:1;
const altPath=(src,dst,base)=>{const key=src*NC+dst,c0=teCache.get(key);
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=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;
@ -215,123 +210,13 @@ function compute(){RW.gen++;
if(dm&&r===3&&e.k==='t'){const t=dm.get(e.net);if(t){const d=DIRECT_RANK[t][e.carry?0:1];let ds=DSEL.get(d);if(!ds)DSEL.set(d,ds=new Uint8Array(NET_IDS.length));ds[j]=1;RSET.add(d);(m[d]||(m[d]=[])).push(e);}}}
for(const r in m){const L=m[r],key=L.map(e=>e.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=[];let GN=0;
if(RCH)rcExitKeep(XK,E,okBy,RCH,LIST,BRJ,DSEL,RSET,BHK);})();
/* What each kind of exit served, in all (EGT) and per network (NEA, by network index and kind): numbers in typed arrays,
where a sum by key would box every new value. EGO and NEO keep the order each first got traffic, which is the order
R.egT and R.netEg list them in; both are written back after routing. */
const EGT=new Float64Array(4),EGO=[],NEA=new Float64Array(NN*4),NEO=[];
// An exit the flow can still try: not visited yet, not full when the flow started, reachable, and not on a saturated path.
// byDist: (Da-Db)+(dta-dtb), so exits of one kind at one distance compare as exactly equal and keep their order.
const usable=e=>e.vis!==vis&&e.used<e.cap&&Dv[e.ci]<Infinity&&!gone(e),byDist=(a,b)=>a.hp-b.hp||Dv[a.ci]-Dv[b.ci]+(a.dt-b.dt);
// 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=(e,P,want,keep=0)=>{if(P.dead)return 0;const path=P.path,ni=P.ni;let amt=want;
const PN=path.length;for(let k=0;k<PN;k++){const l=path[k],r=l.res;if(r<=1e-6){P.dead=1;return 0;}amt=Math.min(amt,r-l.cap*keep);}
const last=ni.length-1;
for(let k=0;k<=last;k++){const i=ni[k];if(!HAS[i])continue;const r=NRES[i];if(r<=1e-6){P.dead=1;return 0;}const w=k&&k<last?PASS[i]:1;amt=Math.min(amt,r/w);}
if(amt<=1e-6)return 0;
for(let k=0;k<=last;k++){const i=ni[k];if(HAS[i])nodeTake(i,k&&k<last?amt*PASS[i]:amt);}
const ek=e.ek,nr=fnj,ci=fci;if(!e.told||e.dup){R2.egDesc[ek]=e.desc||(e.desc={lab:e.lab,city:e.city,k:e.k});e.told=true;}
for(let k=0;k<PN;k++){const l=path[k];l.res-=amt;l.ld+=amt;carry(l,ek,nr,ci,amt);}
e.used+=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<PN;k++)ms+=path[k].lat;P.ms=ms;}const rtt=2*ms,kl=snd&&CITIES[ci].kr;
if(rtt>(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=(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]=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.used<e.cap&&e.ci!==fci)for(let k=0;k<2&&room>1e-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
// 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=e=>{if(TE)return false;const P=PP[e.ci];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<c.length;k++){const e=c[k];if(!(e.used<e.cap)||gone(e))return false;}return true;},NOG=[];
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=FREM[F];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(FREM[F]<=1e-6)break;if(!gone(g[k]))send(g[k],FREM[F]);}};
// 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.
// n: sort only a[0..n) (the group G holds GN exits).
const sortD=(a,n=a.length)=>{if(n>24){if(n===a.length)a.sort(byDist);else{const t=a.slice(0,n).sort(byDist);for(let i=0;i<n;i++)a[i]=t[i];}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;}};
// 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];
for(F=0;F<NF;F++){fnj=FNJ[F];fci=FCI[F];
if(FREM[F]<=1e-6||DS[fnj]===0||PREFC[fci]===1||(pass!==0&&FBZ[F]!==(pass===1?1:0)))continue;
let lst=BRJ[fnj][rank];if(!lst)continue;if(lst.cs){lst=lst.cs[COMP[fci]];if(!lst)continue;}
Dv=SDi[fci];Pv=SPi[fci];PP=walked[fci]||(walked[fci]=[]);vis++;
REST.length=0;
if(LOC){const here=lst.at.get(fci);if(!here)continue;for(let h=0;h<here.length;h++)if(usable(here[h]))REST.push(here[h]);}
else 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&&FREM[F]>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]+(REST[j].dt-REST[i].dt))<1e-6)j++;sendGroup(REST,i,j);i=j;}}};
/* One rank of one pass over every flow. Each hot loop of compute() sits in a function of its own: V8 optimises a small
function quickly and keeps it, while compute() as a whole is too big to optimise cheaply (it would run unoptimised,
boxing every number, and be compiled again and again in the background). The tables of a flow's city (distances,
paths) are set up only once it has an exit list at this rank. */
const rankPass=(rank,pass)=>{
for(F=0;F<NF;F++){
if(FREM[F]<=1e-6||(rank===0&&FCA[F]===0)||(pass!==0&&FBZ[F]!==(pass===1?1:0)))continue;
fnj=FNJ[F];let lst=BRJ[fnj][rank];if(!lst)continue;fci=FCI[F];if(lst.cs){lst=lst.cs[COMP[fci]];if(!lst)continue;}
Dv=SDi[fci];Pv=SPi[fci];PP=walked[fci]||(walked[fci]=[]);vis++;
/* Groups are tried nearest first, preferred exits before the rest, in the order a stable sort of the usable
exits gives. Exits only fill up once tried, so the usable ones stay the same until then. Nearly every flow
fits in its first group: find that one with a scan (nothing is visited yet), and sort only what is left
when it does not fit. Most often the group is the POP's own exits: with no preferred exit on the list
and no other city at distance 0, the usable ones here are exactly the first group, already in order.
The group is G[0..GN): emptying an array (length=0) gives back its storage, and the next push allocates it again. */
GN=0;
if(!lst.hi&&!lst.mx&&ALONE[fci]){const here=lst.at.get(fci);if(here)for(let h=0;h<here.length;h++){const e=here[h];if(e.used<e.cap&&!gone(e))G[GN++]=e;}}
if(!GN){
// One scan: the best (preference, distance) so far and, in list order, the exits within 1e-6 of it. The best
// 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.
/* gc[ci]: the first group this list gave a flow from city ci. Exits only fill up during routing, so while all of
them still have room no other exit can have become better, and the scan would find the same group (an
empty one, no usable exit, stays empty). */
const gc=lst.gc||(lst.gc=[]),c=gc[fci];
if(c!==undefined&&allRoom(c)){if(!c.length)continue;for(let k=0;k<c.length;k++)G[GN++]=c[k];}else{
let bh=2,bd=Infinity,full=0;
for(let li=0,LN=lst.length;li<LN;li++){const e=lst[li];if(e.used>=e.cap||gone(e)){full++;continue;}const d=Dv[e.ci]+e.dt;if(d===Infinity)continue;const h=e.hp;
if(h<bh){bh=h;bd=d;GN=0;G[GN++]=e;}
else if(h===bh){if(d<bd){bd=d;let k=0;for(let gi=0;gi<GN;gi++){const x=G[gi];if(Dv[x.ci]+x.dt-d<1e-6)G[k++]=x;}GN=k;G[GN++]=e;}else if(d-bd<1e-6)G[GN++]=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){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;
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&&FREM[F]>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]+(REST[j].dt-REST[i].dt))<1e-6)j++;
sendGroup(REST,i,j);i=j;
}
}};
for(const pass of PASSES)for(const rank of RANKS){
if(!RSET.has(rank))continue;
if(DSEL.has(rank)){directPass(rank,pass);continue;}
rankPass(rank,pass);
}
R.egT and R.netEg list them in; both are written back after routing. NEG: each network's R.netEg record. */
const EGT=new Float64Array(4),EGO=[],NEA=new Float64Array(NN*4),NEO=[],NEG=[];
ROUTE.run({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},PASSES,RANKS,RSET);
for(const i of NORD)R2.nodeLoad[CITIES[i].id]=NLOAD[i];
for(const k of EGO)R2.egT[EXK[k]]=EGT[k];for(const j of NEO){const ne=NEG[j];for(let k=0;k<4;k++)ne[EXK[k]]=NEA[j*4+k];}
(()=>{

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

@ -287,28 +287,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', () => {
@ -363,6 +376,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'})");