Faster routing and map: finished flows leave the rank loop, flows keep their exit table, lazy exit labels, off-screen links culled by box

This commit is contained in:
Frederic Jung 2026-11-01 13:51:24 +01:00
parent fda5607238
commit dd10a6c19c
No known key found for this signature in database
4 changed files with 19 additions and 13 deletions

View file

@ -8,7 +8,7 @@ All notable changes to this project are documented here. The project follows
- A network the game once failed to load is no longer stuck as damaged: the next version of the game tries it again, and its menu has a Try again button. Before, it could only be exported or deleted, even after the cause was fixed. - A network the game once failed to load is no longer stuck as damaged: the next version of the game tries it again, and its menu has a Try again button. Before, it could only be exported or deleted, even after the cause was fixed.
- Save files of very large networks import again: the limit went from 8 MB to 64 MB. - Save files of very large networks import again: the limit went from 8 MB to 64 MB.
- The Networks tab and the zoomed-in map draw faster on big networks. - Big networks run faster: routing skips traffic it has already placed, the map skips links off screen without working out their shape, and the Networks tab and the zoomed-in map draw faster. Where traffic goes does not change.
- 400G cache boxes: a POP whose routers have 400G or faster ports now gets 400G boxes, at $1,000 a month instead of $400 for 100G, so big caches need fewer boxes and cost less per Gbps. Boxes you already have stay as they are. - 400G cache boxes: a POP whose routers have 400G or faster ports now gets 400G boxes, at $1,000 a month instead of $400 for 100G, so big caches need fewer boxes and cost less per Gbps. Boxes you already have stay as they are.
- Pick a standard POP: in any POP's Infra tab, make its routers the standard, and every new POP gets that model and number of routers right away, while it is still empty (#240). - Pick a standard POP: in any POP's Infra tab, make its routers the standard, and every new POP gets that model and number of routers right away, while it is still empty (#240).
- Clarovision now peers only over paid PNIs, like the real carrier, and free only with networks at customer level 11 or above (#213). A PNI you already have with it starts paying the fee. - Clarovision now peers only over paid PNIs, like the real carrier, and free only with networks at customer level 11 or above (#213). A PNI you already have with it starts paying the fee.

View file

@ -45,15 +45,15 @@ function compute(){
const okBy={};for(const id of NET_IDS)okBy[id]=[]; const okBy={};for(const id of NET_IDS)okBy[id]=[];
const ekN={};for(const e of E){e.ek=e.k+':'+e.id;ekN[e.ek]=(ekN[e.ek]||0)+1;} const ekN={};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. // 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={lab:e.lab,city:e.city,k:e.k};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;for(const n of e.nets||NET_IDS){const L=okBy[n];if(L&&L[L.length-1]!==e&&e.ok(n))L.push(e);}} 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;for(const n of e.nets||NET_IDS){const L=okBy[n];if(L&&L[L.length-1]!==e&&e.ok(n))L.push(e);}}
for(const n of NETS){R2.reachable[n.id]=okBy[n.id].some(e=>e.rank>0);if(R2.reachable[n.id])R2.reach+=NSH[n.id];if(okBy[n.id].some(e=>e.rank>0&&e.k!=='t')){R2.nt[n.id]=1;R2.reachNT+=NSH[n.id]; 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 // 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. // 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 cacheNets=new Set(S.caches.filter(c=>popUp(c.city)).map(c=>c.net)); 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); const viral=(ev.viral>day?1.25:1)*(ev.php&&ev.php>day?0.7:1);
// Every flow has the same shape and carries what routing asks about it again and again (nj: its network's index). // Every flow has the same shape and carries what routing asks about it again and again (nj: its network's index).
const NN=NETS.length,flows=[],flow=(c,ci,cn,j,rem,cache,junk,bz=false)=>{const n=NETS[j],f={c,n:n.id,rem,cache,junk,bz,ci,cn,ncn:!!n.cn,nj:j,nr:junk?NN:j,gt:0,gi:0,gp:0,gh:0,ti:null,ii:null};flows.push(f);return f;}; const NN=NETS.length,flows=[],flow=(c,ci,cn,j,rem,cache,junk,bz=false)=>{const n=NETS[j],f={c,n:n.id,rem,cache,junk,bz,ci,cn,ncn:!!n.cn,nj:j,nr:junk?NN:j,gt:0,gi:0,gp:0,gh:0,ti:null,ii:null,br:null};flows.push(f);return f;};
// Customer demand per city, added up in S.biz order (what filtering S.biz for each city would give). // Customer demand per city, added up in S.biz order (what filtering S.biz for each city would give).
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;}} 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). // Traffic shares by network index, and their sums, for each share table (and with Russia's throttle).
@ -181,7 +181,7 @@ function compute(){
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);} 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; 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);} 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=f.nr,ci=f.ci;if(!e.told||e.dup){R2.egDesc[ek]=e.desc;e.told=true;} const ek=e.ek,nr=f.nr,ci=f.ci;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(const l of path){l.res-=amt;l.ld+=amt;carry(l,ek,nr,ci,amt);} for(const l of path){l.res-=amt;l.ld+=amt;carry(l,ek,nr,ci,amt);}
e.used+=amt;f.rem-=amt; e.used+=amt;f.rem-=amt;
if(!f.junk){let ok=amt;const cf=f.cn,ce=e.cn,cn=f.ncn; if(!f.junk){let ok=amt;const cf=f.cn,ce=e.cn,cn=f.ncn;
@ -220,23 +220,27 @@ function compute(){
// 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 // 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. // 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]; const directPass=(rank,pass)=>{const DS=DSEL.get(rank),LOC=rank===DIRECT_RANK[1][1]||rank===DIRECT_RANK[2][1];
for(let fi=0,FN=flows.length;fi<FN;fi++){f=flows[fi]; for(let fi=0,FN=AF.length;fi<FN;fi++){f=AF[fi];
if(f.rem<=1e-6||DS[f.nj]===0||PREFC[f.ci]===1||(pass&&!pass(f)))continue; if(f.rem<=1e-6||DS[f.nj]===0||PREFC[f.ci]===1||(pass&&!pass(f)))continue;
Dv=SDi[f.ci];Pv=SPi[f.ci];PP=walked[f.ci]||(walked[f.ci]=[]);vis++; Dv=SDi[f.ci];Pv=SPi[f.ci];PP=walked[f.ci]||(walked[f.ci]=[]);vis++;
let lst=byRank[f.n][rank];if(!lst)continue;if(lst.cs){lst=lst.cs[COMP[f.ci]];if(!lst)continue;} let lst=(f.br||(f.br=byRank[f.n]))[rank];if(!lst)continue;if(lst.cs){lst=lst.cs[COMP[f.ci]];if(!lst)continue;}
REST.length=0; REST.length=0;
if(LOC){const here=lst.at.get(f.ci);if(!here)continue;for(let h=0;h<here.length;h++)if(usable(here[h]))REST.push(here[h]);} if(LOC){const here=lst.at.get(f.ci);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]); else for(let li=0;li<lst.length;li++)if(usable(lst[li]))REST.push(lst[li]);
sortD(REST); sortD(REST);
for(let i=0;i<REST.length&&f.rem>1e-6;){let j=i+1;while(j<REST.length&&REST[j].hp===REST[i].hp&&Math.abs(Dv[REST[j].ci]-Dv[REST[i].ci])<1e-6)j++;sendGroup(REST,i,j);i=j;}}}; for(let i=0;i<REST.length&&f.rem>1e-6;){let j=i+1;while(j<REST.length&&REST[j].hp===REST[i].hp&&Math.abs(Dv[REST[j].ci]-Dv[REST[i].ci])<1e-6)j++;sendGroup(REST,i,j);i=j;}}};
// AF: the flows with traffic left. A flow never gets traffic back, so one that is done is dropped after each rank
// instead of being skipped again by every later one (same flows, same order).
let AF=flows;
for(const pass of PASSES||[null])for(const rank of RANKS){ for(const pass of PASSES||[null])for(const rank of RANKS){
if(!RSET.has(rank))continue; if(!RSET.has(rank))continue;
if(AF.length>64){let k=0;const A=AF===flows?AF.slice():AF;for(let i=0;i<A.length;i++)if(A[i].rem>1e-6)A[k++]=A[i];A.length=k;AF=A;}
if(DSEL.has(rank)){directPass(rank,pass);continue;} if(DSEL.has(rank)){directPass(rank,pass);continue;}
// Index loops in here: plain for…of allocates an iterator per loop until V8 has optimised compute(). // Index loops in here: plain for…of allocates an iterator per loop until V8 has optimised compute().
for(let fi=0,FN=flows.length;fi<FN;fi++){f=flows[fi]; for(let fi=0,FN=AF.length;fi<FN;fi++){f=AF[fi];
if(f.rem<=1e-6||(rank===0&&!f.cache)||(pass&&!pass(f)))continue; if(f.rem<=1e-6||(rank===0&&!f.cache)||(pass&&!pass(f)))continue;
Dv=SDi[f.ci];Pv=SPi[f.ci];PP=walked[f.ci]||(walked[f.ci]=[]);vis++; Dv=SDi[f.ci];Pv=SPi[f.ci];PP=walked[f.ci]||(walked[f.ci]=[]);vis++;
let lst=byRank[f.n][rank];if(!lst)continue;if(lst.cs){lst=lst.cs[COMP[f.ci]];if(!lst)continue;} let lst=(f.br||(f.br=byRank[f.n]))[rank];if(!lst)continue;if(lst.cs){lst=lst.cs[COMP[f.ci]];if(!lst)continue;}
/* Groups are tried nearest first, preferred exits before the rest, in the order a stable sort of the usable /* 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 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 fits in its first group: find that one with a scan (nothing is visited yet), and sort only what is left

View file

@ -2,7 +2,7 @@ function rng(seed){return()=>{seed|=0;seed=seed+0x6D2B79F5|0;let t=Math.imul(see
// A point outside a ring's bounding box is outside the ring: the box is kept on the ring, so the hundreds of tests // A point outside a ring's bounding box is outside the ring: the box is kept on the ring, so the hundreds of tests
// at load (sea waves, hills, which islets the far view keeps) skip nearly every coastline without walking it. // at load (sea waves, hills, which islets the far view keeps) skip nearly every coastline without walking it.
const polyBox=pts=>{let b=pts.box;if(!b){let x0=Infinity,y0=Infinity,x1=-Infinity,y1=-Infinity;for(const [x,y] of pts){if(x<x0)x0=x;if(x>x1)x1=x;if(y<y0)y0=y;if(y>y1)y1=y;}b=pts.box=[x0,y0,x1,y1];}return b;}; const polyBox=pts=>{let b=pts.box;if(!b){let x0=Infinity,y0=Infinity,x1=-Infinity,y1=-Infinity;for(const [x,y] of pts){if(x<x0)x0=x;if(x>x1)x1=x;if(y<y0)y0=y;if(y>y1)y1=y;}b=pts.box=[x0,y0,x1,y1];}return b;};
function inPoly(x,y,pts){const b=polyBox(pts);if(x<b[0]||x>b[2]||y<b[1]||y>b[3])return false;let ins=false;for(let i=0,j=pts.length-1;i<pts.length;j=i++){const [xi,yi]=pts[i],[xj,yj]=pts[j];if(((yi>y)!==(yj>y))&&(x<(xj-xi)*(y-yi)/(yj-yi)+xi))ins=!ins;}return ins;} function inPoly(x,y,pts){const b=polyBox(pts);if(x<b[0]||x>b[2]||y<b[1]||y>b[3])return false;let ins=false;for(let i=0,j=pts.length-1;i<pts.length;j=i++){const P=pts[i],Q=pts[j],xi=P[0],yi=P[1],xj=Q[0],yj=Q[1];if(((yi>y)!==(yj>y))&&(x<(xj-xi)*(y-yi)/(yj-yi)+xi))ins=!ins;}return ins;}
function smoothPath(pts){const p=new Path2D();const n=pts.length;const mid=(a,b)=>[(a[0]+b[0])/2,(a[1]+b[1])/2];let m=mid(pts[n-1],pts[0]);p.moveTo(m[0],m[1]);for(let i=0;i<n;i++){const nx=pts[(i+1)%n];m=mid(pts[i],nx);p.quadraticCurveTo(pts[i][0],pts[i][1],m[0],m[1]);}p.closePath();return p;} function smoothPath(pts){const p=new Path2D();const n=pts.length;const mid=(a,b)=>[(a[0]+b[0])/2,(a[1]+b[1])/2];let m=mid(pts[n-1],pts[0]);p.moveTo(m[0],m[1]);for(let i=0;i<n;i++){const nx=pts[(i+1)%n];m=mid(pts[i],nx);p.quadraticCurveTo(pts[i][0],pts[i][1],m[0],m[1]);}p.closePath();return p;}
const LANDP_ALL=COAST.map(smoothPath); const LANDP_ALL=COAST.map(smoothPath);
// Zoomed out, hundreds of tiny Arctic and Pacific islets, each with a thick outline, turn into noise. // Zoomed out, hundreds of tiny Arctic and Pacific islets, each with a thick outline, turn into noise.

View file

@ -33,10 +33,12 @@ function drawFrame(now){
const w=(vx?2.2:(2.5+Math.max(...grp.map(tierW))*1.8+(many?Math.min(1.5,(grp.length-1)*0.5):0)))*sc*(far?0.5:1),np=gm.pts.length; const w=(vx?2.2:(2.5+Math.max(...grp.map(tierW))*1.8+(many?Math.min(1.5,(grp.length-1)*0.5):0)))*sc*(far?0.5:1),np=gm.pts.length;
if(SX.length<np){SX=new Float64Array(np*2);SY=new Float64Array(np*2);} if(SX.length<np){SX=new Float64Array(np*2);SY=new Float64Array(np*2);}
for(const k of ks){ for(const k of ks){
// Off screen (with room for the widest line and the markers at its middle): nothing of it would show. The line's
// box is kept on its cached geometry, so a link off screen is skipped without projecting its points.
const bb=gm.bb||(gm.bb=(()=>{let a=Infinity,b=-Infinity,c=Infinity,d=-Infinity;for(const p of gm.pts){if(p[0]<a)a=p[0];if(p[0]>b)b=p[0];if(p[1]<c)c=p[1];if(p[1]>d)d=p[1];}return [a,b,c,d];})());
if((bb[1]+k*W-cam.x)*cam.z+CW/2<-40||(bb[0]+k*W-cam.x)*cam.z+CW/2>CW+40||(bb[3]-cam.y)*cam.z+CH/2<-40||(bb[2]-cam.y)*cam.z+CH/2>CH+40)continue;
// Screen points as toS() gives them, kept in SX/SY. // Screen points as toS() gives them, kept in SX/SY.
let mn=Infinity,mx=-Infinity,mny=Infinity,mxy=-Infinity;for(let i=0;i<np;i++){const p=gm.pts[i],x=(p[0]+k*W-cam.x)*cam.z+CW/2,y=(p[1]-cam.y)*cam.z+CH/2;SX[i]=x;SY[i]=y;if(x<mn)mn=x;if(x>mx)mx=x;if(y<mny)mny=y;if(y>mxy)mxy=y;} for(let i=0;i<np;i++){const p=gm.pts[i];SX[i]=(p[0]+k*W-cam.x)*cam.z+CW/2;SY[i]=(p[1]-cam.y)*cam.z+CH/2;}
// Off screen (with room for the widest line and the markers at its middle): nothing of it would show.
if(mx<-40||mn>CW+40||mxy<-40||mny>CH+40)continue;
const path=()=>{ctx.beginPath();ctx.moveTo(SX[0],SY[0]);for(let i=1;i<np;i++)ctx.lineTo(SX[i],SY[i]);}; const path=()=>{ctx.beginPath();ctx.moveTo(SX[0],SY[0]);for(let i=1;i<np;i++)ctx.lineTo(SX[i],SY[i]);};
ctx.lineCap='round';ctx.lineJoin='round'; ctx.lineCap='round';ctx.lineJoin='round';
if(sel){ctx.strokeStyle='rgba(255,79,154,.35)';ctx.lineWidth=w+14;path();ctx.stroke();} if(sel){ctx.strokeStyle='rgba(255,79,154,.35)';ctx.lineWidth=w+14;path();ctx.stroke();}