Merge remote-tracking branch 'origin/main' into perf/3-worker

# Conflicts:
#	src/game/80-simulation/30-compute.js
This commit is contained in:
Frederic Jung 2026-11-01 16:14:07 +01:00
commit 18db953818
No known key found for this signature in database
10 changed files with 144 additions and 92 deletions

View file

@ -6,16 +6,16 @@ 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.
const PH=new Set(E.filter(x=>x.k==='t'&&x.pref==='high').map(x=>CIDX[x.city]));
const dr=(e,f)=>{if(e.k!=='t'||PH.has(f.ci)||eff(e,f.n)!==3)return 0;const t=directTier(e.net,f.n);return t?DIRECT_RANK[t][e.carry?0:1]:0;};
for(const pass of PASSES||[null])for(const rank of RANKS){
for(f of flows){
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++;
const candidates=E.filter(e=>e.used<e.cap&&e.ok(f.n)&&
((TR?eff(e,f.n):base(e,f.n))===rank||(dr(e,f)||-1)===rank)&&
(rank!==DIRECT_RANK[1][1]&&rank!==DIRECT_RANK[2][1]||CIDX[e.city]===f.ci)&&Dv[CIDX[e.city]]<Infinity&&!gone(e))
const dr=(e,ci,n)=>{if(e.k!=='t'||PH.has(ci)||eff(e,n)!==3)return 0;const t=directTier(e.net,n);return t?DIRECT_RANK[t][e.carry?0:1]:0;};
for(const pass of PASSES)for(const rank of RANKS){
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)&&
((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))
.sort((a,b)=>(a.pref==='high'?0:1)-(b.pref==='high'?0:1)||Dv[CIDX[a.city]]-Dv[CIDX[b.city]]);
for(let i=0;i<candidates.length&&f.rem>1e-6;){
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]])<1e-6)j++;
sendGroup(candidates.slice(i,j));i=j;

View file

@ -27,7 +27,7 @@ test('the worker side routes what it is sent: same R as routing on the page', (t
const { ev, errors } = big(t);
// rwServe() in the page itself stands in for the worker: it posts back what the worker would.
ev("window.__got=null;window.postMessage=(m)=>{window.__got=m;};");
ev("{window.__R0=R;const prev={load:R.load,l2route:R.l2route,efg:R.efg};const keep=S;window.__S0=JSON.stringify(S);rwServe(structuredClone({seq:1,S,...prev}));S=keep;}");
ev("{window.__R0=R;const prev={load:R.load,l2route:R.l2route,efg:rwEfgOut(R.efg)};const keep=S;window.__S0=JSON.stringify(S);rwServe(structuredClone({seq:1,S,...prev}));S=keep;}");
ev("R=rwUnpack(structuredClone(__got.R),__got.ef)");
const fromWorker = ev(SIG);
ev('R=__R0;S=JSON.parse(__S0);compute()');

View file

@ -23,7 +23,7 @@ function network(ev) {
// 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||[null])for(const rank of RANKS){');
const start = source.indexOf(' for(const pass of PASSES)for(const rank of RANKS){');
const end = source.indexOf(' for(const i of NORD)', start);
assert.ok(start >= 0 && end > start, 'allocation loop boundaries must exist');
ev(`window.referenceCompute=eval('('+${JSON.stringify(source.slice(0,start)+referenceAllocation+source.slice(end))}+')')`);

View file

@ -272,17 +272,17 @@ test('the map is not drawn while the dashboard covers the whole screen', () => {
function plainCompute(ev) {
const src = ev('compute.toString()');
let plain = src
.replace('if(!lst.hi&&ALONE[f.ci])', 'if(false)')
.replace('if(!lst.hi&&ALONE[fci])', 'if(false)')
.replace('if(!G.length){\n // One scan', 'if(false){\n // One scan')
.replace('for(const n of e.nets||NET_IDS)', 'for(const n of NET_IDS)')
.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 the group split and sorting written the plain way, with array copies, as before they were made allocation-free
const sg = plain.indexOf('const ROOM=[],sendGroup='), sd = plain.indexOf('const sortD=');
assert.ok(sg > 0 && sd > sg, 'found the group sender and the sort');
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=f.rem;if(tot>1e-6)g.forEach((e,k)=>send(e,want*room[k]/tot));}
for(const e of g){if(f.rem<=1e-6)break;send(e,f.rem);}};\n ` + plain.slice(sd).replace(/^const sortD=a=>\{[^\n]*\};/, 'const sortD=a=>a.sort(byDist);');
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 [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');
assert.ok(!plain.includes('e.nets||NET_IDS'), 'the exit index was switched off');