From 85e62f63927f005a6be1f18a0fc68257ac1af513 Mon Sep 17 00:00:00 2001 From: Frederic Jung Date: Sun, 1 Nov 2026 19:02:39 +0100 Subject: [PATCH] Routing adds up per-kind traffic in typed arrays and keeps its group array's storage: half the allocation per compute R.egT and R.netEg were summed by key in push(), which boxes every new value; they are now Float64Array sums written back after routing, in the same order and key order. The first group G was emptied with length=0 for every flow, which gives back its storage so the next push allocates it again; it is now G[0..GN). 27.5 MB -> 13.5 MB per compute. --- src/game/80-simulation/30-compute.js | 35 +++++++++++++++++----------- tests/speed.test.js | 6 ++--- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/game/80-simulation/30-compute.js b/src/game/80-simulation/30-compute.js index 26bf9b2..5353c91 100644 --- a/src/game/80-simulation/30-compute.js +++ b/src/game/80-simulation/30-compute.js @@ -22,7 +22,9 @@ function flDefine(R2,c){const {NF,FREM,FCI,FNJ,FCA,FBZ,FGT,FGI,FGP,FGH,FTI,FII,E /* Every exit has the same fields in the same order, whatever its kind, so the routing loop reads them from one object shape (exits of six shapes made each property read there a slow lookup). */ function mkEx(k,id,city,cap,rank,pref,lab,ok,nets,net){return {k,net,id,city,lab,cap,used:0,rank,pref,ok,nets,dir:null,clean:false,prem:false,carry:false,prep:false,sac:false,pnc:false, - ek:'',J:0,byC:null,desc:null,dup:false,told:false,hp:1,ci:0,cn:false,vis:0,ts:-1,xi:0,fac:undefined};} + ek:'',J:0,byC:null,desc:null,dup:false,told:false,hp:1,ci:0,cn:false,vis:0,ts:-1,xi:0,kx:EXK.indexOf(k),fac:undefined};} +// The exit kinds by number (mkEx's kx), for what routing adds up per kind in typed arrays. +const EXK=['t','ix','p','c']; function compute(){RW.gen++; const day=S.day, R2=emptyR(), pops=Object.keys(S.pops).filter(popUp), NC=CITIES.length; // The graph works on city indices: adj[i] lists [neighbour, edge] in S.links order. @@ -209,7 +211,11 @@ function compute(){RW.gen++; 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 F=0,fci=0,fnj=0,Dv=null,Pv=null,PP=null,vis=0;const walked=[],G=[],REST=[],NEG=[];let GN=0; + /* 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. const usable=e=>e.vis!==vis&&e.useda.hp-b.hp||Dv[a.ci]-Dv[b.ci]; // Push up to `want` along one path to exit e; returns what fitted. Index loops: for…of allocates an iterator until optimised. Capacity only shrinks while routing, so a @@ -231,7 +237,7 @@ function compute(){RW.gen++; // 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;}}R2.egT[e.k]=(R2.egT[e.k]||0)+ok;const ne=NEG[nr]||(NEG[nr]=R2.netEg[NET_IDS[nr]]={c:0,p:0,ix:0,t:0});ne[e.k]+=ok; + 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;}} @@ -256,7 +262,8 @@ function compute(){RW.gen++; for(let k=i0;k{const n=a.length;if(n>24){a.sort(byDist);return;}for(let i=1;i=0&&byDist(a[j],x)>0){a[j+1]=a[j];j--;}a[j+1]=x;}}; + // 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=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 @@ -284,10 +291,11 @@ function compute(){RW.gen++; 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. */ - G.length=0; - if(!lst.hi&&ALONE[fci]){const here=lst.at.get(fci);if(here)for(let h=0;h=e.cap||gone(e)){full++;continue;}const d=Dv[e.ci];if(d===Infinity)continue;const h=e.hp; - if(h=lst.length&&full){let k=0;for(let li=0;li1){gc[fci]=NOG;continue;} - if(G.length>1)sortD(G);gc[fci]=G.slice();}} - sendGroup(G,0,G.length); + 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;li1e-6;){ @@ -317,6 +325,7 @@ function compute(){RW.gen++; rankPass(rank,pass); } 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];} (()=>{ if(DD){let rem=0,jf=0;for(let i=0;i1e-6)rem+=FREM[i];if(JK)for(const k in edges){const e=edges[k];if(e.res<=1e-6&&e.jk)jf+=e.jk;} ddosAfter(DD,{E,R2,squeeze:Math.min(rem,jf), diff --git a/tests/speed.test.js b/tests/speed.test.js index 6c9b277..8c9795d 100644 --- a/tests/speed.test.js +++ b/tests/speed.test.js @@ -273,7 +273,7 @@ function plainCompute(ev) { const src = ev('compute.toString()'); let plain = src .replace('if(!lst.hi&&ALONE[fci])', 'if(false)') - .replace('if(!G.length){\n // One scan', 'if(false){\n // One scan') + .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;') @@ -284,13 +284,13 @@ function plainCompute(ev) { 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]*\};/, 'const sortD=a=>a.sort(byDist);'); + 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;ia.sort(byDist);') && plain.includes('g.forEach('), 'the plain group split and sort are in'); + 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')); }