Merge performance work into v2: faster routing and sales trials

This commit is contained in:
Frederic Jung 2026-10-28 19:50:33 +01:00
commit 2a5a26a831
No known key found for this signature in database
4 changed files with 124 additions and 5 deletions

View file

@ -11,7 +11,14 @@ function autoOffer(o){if(!o||!own(BIZ_T,o.type)||!(o.exp>S.day)||!(o.bw>0)||o.ra
// Trial the added demand before signing; failed trials leave the live game untouched.
try{S={...S,biz:S.biz};
// shortcut: future events reserve capacity together; use per-date trials if separate bookings must share capacity.
if(future){S.biz=S.biz.map(x=>BIZ_T[x.type].event&&x.start>S.day?{...x,start:S.day}:x);compute();}
if(future){S.biz=S.biz.map(x=>BIZ_T[x.type].event&&x.start>S.day?{...x,start:S.day}:x);
/* Signed if the offer works and nobody who works without it breaks. compute() reads the last result only for
scenic circuits, so without them the pass with the offer can go first: when the offer fails, or nobody is
unhappy with it, the pass without it cannot change the answer and is skipped. */
const fut=S.biz,all=fut.concat(b);
if(!all.some(x=>x.route==='scenic')){if(T.event)b.start=S.day;if(T.cap)b.live=true;S.biz=all;compute();if(bizWhy(b).length)return null;
const bad=fut.filter(x=>bizWhy(x).length);if(!bad.length)return b;S.biz=fut;compute();return bad.every(x=>bizWhy(x).length)?b:null;}
compute();}
const healthy=S.biz.filter(x=>!bizWhy(x).length);if(T.event)b.start=S.day;if(T.cap)b.live=true;S.biz=S.biz.concat(b);compute();
return !bizWhy(b).length&&healthy.every(x=>!bizWhy(x).length)?b:null;
}finally{S=state;R=result;UI.hotMap=hot;b.start=o.start;if(T.cap)delete b.live;}}

View file

@ -14,6 +14,8 @@ function compute(){
// ALONE[i]: no other city lies within 1e-6 of POP i (so only its own exits can be at distance 0 from it).
const SPD={},SPP={},SDi=[],SPi=[],ALONE=new Uint8Array(NC),wW=e=>e.w,wLat=e=>e.lat+0.05;
for(const s of pops){const i=CIDX[s],r=dijkstra(adj,i,wW);SDi[i]=SPD[s]=r.D;SPi[i]=SPP[s]=r.P;ALONE[i]=1;for(let x=0;x<NC;x++)if(x!==i&&r.D[x]<1e-6)ALONE[i]=0;}
// COMP[i]: the island city i is on (-1: no POP reaches it). Links run both ways, so reaching is mutual.
const COMP=new Int32Array(NC).fill(-1);let NCOMP=0;for(const s of pops){const i=CIDX[s];if(COMP[i]>=0)continue;const D=SDi[i],k=NCOMP++;for(let x=0;x<NC;x++)if(D[x]<Infinity)COMP[x]=k;}
const E=[], ev=S.ev;
const depeer=ev.depeer>day||scnRule('spat'), leak=ev.leak&&ev.leak.until>day?ev.leak.net:null;
for(const t of S.transit){if(!popUp(t.city)||t.down>day||!liveN(t.city,t.site,t.n))continue;
@ -165,8 +167,15 @@ function compute(){
const PASSES=TR&&TR.protect?[x=>x.bz,x=>!x.bz]:null;
// Each network's exits by the rank they route at (RouteScript can move an exit per network), in E's order.
// Each list also knows whether it holds a preferred exit (hi) and its exits by city (at), in list order.
// With several islands (POPs no backbone joins), each list is split by island: a flow only scans exits it can reach.
const idx=L=>{L.hi=L.some(e=>e.hp===0);L.at=new Map();for(const e of L){const h=L.at.get(e.ci);if(h)h.push(e);else L.at.set(e.ci,[e]);}};
// Networks with the same exits at a rank share one list (LIST, by content), so what one flow learns serves them all.
const LIST=new Map();for(let i=0;i<E.length;i++)E[i].xi=i;
const byRank={},RSET=new Set();for(const id of NET_IDS){const m=byRank[id]={};for(const e of okBy[id]){const r=TR?eff(e,id):base(e,id);RSET.add(r);(m[r]||(m[r]=[])).push(e);}
for(const r in m){const L=m[r];L.hi=L.some(e=>e.hp===0);L.at=new Map();for(const e of L){const h=L.at.get(e.ci);if(h)h.push(e);else L.at.set(e.ci,[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;}}
// Attack flows pinned to their entry port route at its plain rank, which RouteScript may have left with no list.
for(const f of flows)if(f.pin)for(const e of ingressE[f.pin]||[])RSET.add(base(e));
// The flow being routed, and from its POP: distances, arriving edges and the paths walked so far, by city index.
let f=null,Dv=null,Pv=null,PP=null,vis=0;const walked=[],G=[],REST=[],NEG=[];
// An exit the flow can still try: not visited yet, not full when the flow started, and reachable from the POP.
@ -216,7 +225,7 @@ function compute(){
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++;
if(f.pin){for(const e of ingressE[f.pin]||[])if(base(e)===rank&&e.used<e.cap&&Dv[e.ci]<Infinity)send(e,f.rem);continue;}
const lst=byRank[f.n][rank];if(!lst)continue;
let lst=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
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
@ -228,13 +237,18 @@ function compute(){
// 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[f.ci];
if(c!==undefined&&c.every(e=>e.used<e.cap)){if(!c.length)continue;for(let k=0;k<c.length;k++)G.push(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){full++;continue;}const d=Dv[e.ci];if(d===Infinity)continue;const h=e.hp;
if(h<bh){bh=h;bd=d;G.length=0;G.push(e);}
else if(h===bh){if(d<bd){bd=d;let k=0;for(let gi=0;gi<G.length;gi++){const x=G[gi];if(Dv[x.ci]-d<1e-6)G[k++]=x;}G.length=k;G.push(e);}else if(d-bd<1e-6)G.push(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)continue;
if(G.length>1)sortD(G);}
if(bh>1){gc[f.ci]=[];continue;}
if(G.length>1)sortD(G);gc[f.ci]=G.slice();}}
sendGroup(G,0,G.length);
if(f.rem<=1e-6)continue;
REST.length=0;for(let li=0;li<lst.length;li++)if(usable(lst[li]))REST.push(lst[li]);sortD(REST);

View file

@ -54,3 +54,28 @@ test('indexed routing matches the simple allocation loop across network changes'
} finally { dom.window.close(); }
});
// Islands: POPs no backbone joins. Exit lists are split per island (a flow only scans exits it can reach),
// and must route exactly like the full scan, also when a cut splits an island or joins two.
test('indexed routing matches the simple allocation loop on a network of islands', () => {
const { ev, dom, errors } = load();
try {
network(ev);
ev("S.links=S.links.filter(l=>!['df4','df9','w3','w4','w8','w9'].includes(l.id));compute()");
assert.ok(ev("Object.keys(S.pops).some(c=>!pathExists('fra',c))"), 'the network is in pieces');
const changes = [
'',
"S.pni.forEach((p,i)=>p.pref=i%3===0?'high':i%3===1?'low':'normal')",
"S.te={on:true,script:'prefer pni for goggle\\nspill at 80%'}",
"S.ev.ddos={city:'fra',until:S.day+3,gbps:300,vec:'syn',entry:{k:'edge'}}",
"S.links[0].cut=S.day+2",
"S.te=null;S.links[0].cut=0;S.links.push({id:'join',a:'par',b:'pek',kind:'wave',tier:2,km:8000,cut:0,mo:100})",
];
for (const change of changes) {
ev(change);
ev('window.priorR=JSON.stringify(R);referenceCompute();window.expectedR=JSON.stringify(R);R=JSON.parse(priorR);compute()');
assert.deepEqual(JSON.parse(ev('JSON.stringify(R)')), JSON.parse(ev('expectedR')), change || 'baseline');
}
assert.deepEqual(errors, []);
} finally { dom.window.close(); }
});

73
tests/sales-trial.test.js Normal file
View file

@ -0,0 +1,73 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { load } = require('./harness');
// The sales trial (#185, the intern's sales order) runs the pass with the offer first and skips the pass without
// it when it cannot change the answer. It must sign exactly the offers the original two-pass trial signs.
const ORIGINAL = `(function(o){if(!o||!own(BIZ_T,o.type)||!(o.exp>S.day)||!(o.bw>0)||o.rate<0||!bCities(o).length||!bCities(o).every(popUp)||!reqOk(o))return null;
const T=BIZ_T[o.type],b=offerCustomer(o);if(T.event&&(!(o.start>S.day)||!(o.dur>0)))return null;
if(o.type==='capfib'&&S.biz.some(x=>x.type==='capfib'&&capPend(x)&&!x.link&&((x.city===o.city&&x.city2===o.city2)||(x.city===o.city2&&x.city2===o.city))))return null;
if(T.fiber){const l=o.type==='capfib'?capFibLink(b):S.links.find(x=>x.id===o.link);if(!l)return null;b.link=l.id;
if(!fiberOk(b)||(o.type==='wwave'&&(!(o.slots>0)||freeCh(l)<o.slots))||(o.type==='dfpair'&&!pairFree(l))||
(o.type==='pairs'&&(!l.own||S.biz.some(x=>x.link===l.id&&['pairs','capfib','dfpair'].includes(x.type)))))return null;}
const state=S,result=R,hot=UI.hotMap,future=S.biz.some(x=>BIZ_T[x.type].event&&x.start>S.day);
try{S={...S,biz:S.biz};
if(future){S.biz=S.biz.map(x=>BIZ_T[x.type].event&&x.start>S.day?{...x,start:S.day}:x);compute();}
const healthy=S.biz.filter(x=>!bizWhy(x).length);if(T.event)b.start=S.day;if(T.cap)b.live=true;S.biz=S.biz.concat(b);compute();
return !bizWhy(b).length&&healthy.every(x=>!bizWhy(x).length)?b:null;
}finally{S=state;R=result;UI.hotMap=hot;b.start=o.start;if(T.cap)delete b.live;}})`;
test('the faster sales trial signs exactly the offers the two-pass trial signs', () => {
const { ev, dom, errors } = load();
try {
const save = fs.readFileSync(path.join(__dirname, 'fixtures', 'big-network.json'), 'utf8');
ev(`S=migrate(JSON.parse(${JSON.stringify(save)}));UI.started=true;UI.pitched=true;UI.speed=0;closeModal();compute();window.slowOffer=${ORIGINAL};`);
assert.ok(ev('S.biz.some(x=>BIZ_T[x.type].event&&x.start>S.day)'), 'the fixture has events booked ahead, so the shortcut runs');
const seen = { yes: 0, no: 0, skipped: 0, third: 0 };
ev('window.__n=0;const _c=compute;window.compute=function(){__n++;return _c.apply(this,arguments)}');
for (let day = 0; day < 6; day++) {
ev(`__reseed(${1000 + day});S.offers=[];for(let i=0;i<10;i++)spawnOffer();`);
// Small offers that fit, and on alternate days a squeeze, so offers fail on capacity and on others' service too.
ev(`for(const o of S.offers)o.bw=Math.max(1,Math.round(o.bw*${day % 2 ? 1 : 0.05}))`);
if (day % 2) ev('for(const c in S.pops)S.pops[c].bb&&(S.pops[c].bb.d*=1.3)');
// And circuits over the busiest links, which take bandwidth from customers who had it.
ev(`for(const l of S.links.filter(l=>!linkDown(l)&&S.pops[l.a]&&S.pops[l.b]&&R.load[l.id]>1).sort((x,y)=>R.load[y.id]/capOf(y)-R.load[x.id]/capOf(x)).slice(${day},${day}+2))
S.offers.push({id:'sq'+l.id,city:l.a,city2:l.b,name:'Squeeze',type:'l2vpn',bw:${day % 2 ? 300 : 2},rate:100,reqs:[],exp:S.day+30})`);
for (const id of JSON.parse(ev('JSON.stringify(S.offers.map(o=>o.id))'))) {
const o = `S.offers.find(x=>x.id===${JSON.stringify(id)})`;
const before = ev('JSON.stringify(R.load)+R.served');
ev('__n=0');const fast = ev(`JSON.stringify(autoOffer(${o}))`);const n = ev('__n');
assert.equal(ev('JSON.stringify(R.load)+R.served'), before, 'a trial leaves the live result alone');
const slow = ev(`JSON.stringify(slowOffer(${o}))`);
assert.equal(fast, slow, `offer ${id} (${ev(`${o}.type`)}) on day ${day}`);
if (fast !== 'null') seen.yes++; else seen.no++;
if (n === 1) seen.skipped++;
if (n === 2) seen.third++;
}
ev('S.nextFail=S.day+99;S.nextEvent=S.day+99;dayTick()');
}
assert.ok(seen.yes >= 3 && seen.no >= 3, `both answers come up (${JSON.stringify(seen)})`);
assert.ok(seen.skipped >= 5, `the second pass is often skipped (${JSON.stringify(seen)})`);
assert.ok(seen.third >= 3, `and run when the offer hurts someone (${JSON.stringify(seen)})`);
assert.deepEqual(errors, []);
} finally { dom.window.close(); }
});
test('with a scenic circuit the sales trial keeps both passes in their original order', () => {
const { ev, dom, errors } = load();
try {
const save = fs.readFileSync(path.join(__dirname, 'fixtures', 'big-network.json'), 'utf8');
ev(`S=migrate(JSON.parse(${JSON.stringify(save)}));UI.started=true;UI.pitched=true;UI.speed=0;closeModal();S.biz.find(b=>BIZ_T[b.type].l2).route='scenic';compute();window.slowOffer=${ORIGINAL};`);
ev('window.__n=0;const _c=compute;window.compute=function(){__n++;return _c.apply(this,arguments)}');
ev('__reseed(77);S.offers=[];for(let i=0;i<6;i++)spawnOffer();');
for (const id of JSON.parse(ev('JSON.stringify(S.offers.map(o=>o.id))'))) {
const o = `S.offers.find(x=>x.id===${JSON.stringify(id)})`;
ev('__n=0');const fast = ev(`JSON.stringify(autoOffer(${o}))`);const n = ev('__n');
assert.equal(fast, ev(`JSON.stringify(slowOffer(${o}))`), id);
assert.ok(n === 0 || n === 2, 'both passes, as before');
}
assert.deepEqual(errors, []);
} finally { dom.window.close(); }
});