Faster days on big networks: look fiber customers up by link id

compute(), the daily mood check and new alien-wave/fiber-pair offers asked
S.links.find() or S.biz.filter() once per customer or per link. On a save
with 2,950 links and 18,000 customers (16,000 alien waves) that was ~150 ms
per compute, ~250 ms more per day, and ~650 ms when a wave offer spawned.

linkIds() and fiberUse() build the lookups in one pass; fiberOk, bizSf,
soldSlots, freeCh and pairFree take them optionally, and give the same
answers as before (first link with the id wins, sums in S.biz order).

Player save (202 POPs, 2,958 links, 17,967 customers), median of 15:
compute 323 -> 124 ms, dayTick 545 -> 133 ms, after 356 -> 149 ms.
Big-network fixture: unchanged. Routing results identical to main.
This commit is contained in:
Hugues 2026-10-27 12:26:10 +01:00
parent 5158491b79
commit 456062284f
6 changed files with 37 additions and 8 deletions

View file

@ -6,6 +6,7 @@ All notable changes to this project are documented here. The project follows
## [Unreleased]
- Big networks with thousands of fiber customers no longer stall: each game day and each action is about two to three times faster, and new alien-wave offers no longer freeze the game for a moment.
- The game starts faster in Italian, Portuguese and French: their translations are about 40% smaller to download.
## [1.34.0] — 2026-10-27

View file

@ -259,7 +259,7 @@ function compute(){
C.extras=(S.cnLic&&S.cnLic.state==='ok'?CN_JV_MO:0)+scrubMo()+Object.keys(S.pops).filter(ruCity).length*RU_BOX_MO+Object.values(S.pops).filter(p=>p.site2).length*SITE2_MO+(S.te?TE_MO:0)+(S.inbox&&S.inbox.bot?AI_MO:0);
V.eyeballs=0;V.business=0;V.ix=0;
C.wholesale=0;C.levy=0;const lev=scnRule('levy');for(const c in S.pops){const sf=sfOf(R2,c),v=R2.cityEye[c]*sf*EYE_RATE*(CITY[c].arpu||1);V.eyeballs+=v;if(CITY[c].au){if(lev)C.levy+=v*AU_LEVY;else C.wholesale+=v*AU_WHOLESALE;}}
for(const b of S.biz){const sf=BIZ_T[b.type].fiber?(fiberOk(b)?1:0):BIZ_T[b.type].l2?clamp((R2.l2[b.id]||0)/b.bw,0,1):sfOf(R2,b.city);V.business+=b.bw*b.rate*Math.min(1,sf/0.95);}
const ids=linkIds();for(const b of S.biz){const sf=BIZ_T[b.type].fiber?(fiberOk(b,ids)?1:0):BIZ_T[b.type].l2?clamp((R2.l2[b.id]||0)/b.bw,0,1):sfOf(R2,b.city);V.business+=b.bw*b.rate*Math.min(1,sf/0.95);}
for(const c in S.myIx)V.ix+=S.myIx[c].members.length*MYIX_FEE;
C.revshare=(S.loans||[]).reduce((a,l)=>a+(BANKS[l.bank].equity||l.bal>0?l.share||0:0),0)*(V.eyeballs+V.business+V.ix);
R2.costMo=Object.values(C).reduce((a,b)=>a+b,0);

View file

@ -51,11 +51,17 @@ function diversityPossible(a,b,state=S){
const pair=[a,b].sort().join(':');
return diversityMap.answers[pair]??(diversityMap.answers[pair]=disjointRoutes(a,b,diversityMap.links));
}
function fiberOk(b){const l=S.links.find(x=>x.id===b.link);if(!l)return false;if(b.type==='dfpair'||b.type==='pairs')return l.ready<=S.day&&!(l.cut>S.day);return l.ready<=S.day&&!(l.cut>S.day)&&!(l.eqDown>S.day);}
function soldSlots(l){return S.biz.filter(b=>b.type==='wwave'&&b.link===l.id).reduce((a,b)=>a+(b.slots||0),0);}
// Big networks have thousands of links and customers: callers that ask about all of them (compute(), the daily mood
// check, new offers) look links up and add up what is sold in one pass (linkIds(), fiberUse()), not once per item.
const linkIds=()=>{const m=new Map();for(const l of S.links)if(!m.has(l.id))m.set(l.id,l);return m;};
function fiberUse(){const sold=new Map(),dfp=new Map(),pairs=new Set();
for(const b of S.biz){if(b.type==='wwave')sold.set(b.link,(sold.get(b.link)||0)+(b.slots||0));else if(b.type==='dfpair')dfp.set(b.link,(dfp.get(b.link)||0)+1);else if(b.type==='pairs')pairs.add(b.link);}
return {sold,dfp,pairs};}
function fiberOk(b,ids){const l=ids?ids.get(b.link):S.links.find(x=>x.id===b.link);if(!l)return false;if(b.type==='dfpair'||b.type==='pairs')return l.ready<=S.day&&!(l.cut>S.day);return l.ready<=S.day&&!(l.cut>S.day)&&!(l.eqDown>S.day);}
function soldSlots(l,u){return u?u.sold.get(l.id)||0:S.biz.filter(b=>b.type==='wwave'&&b.link===l.id).reduce((a,b)=>a+(b.slots||0),0);}
// Spare capacity other carriers can rent on a dark fiber (#51): free channels for alien waves, and on your own build
// a spare pair in the trench. Random offers and broker listings follow the same rules and the same prices.
const freeCh=l=>chMax(l)-(l.ch||[]).length-soldSlots(l),pairFree=l=>!!l.own&&S.biz.filter(b=>b.type==='dfpair'&&b.link===l.id).length<2&&!S.biz.some(b=>b.type==='pairs'&&b.link===l.id);
const freeCh=(l,u)=>chMax(l)-(l.ch||[]).length-soldSlots(l,u),pairFree=(l,u)=>!!l.own&&(u?u.dfp.get(l.id)||0:S.biz.filter(b=>b.type==='dfpair'&&b.link===l.id).length)<2&&!(u?u.pairs.has(l.id):S.biz.some(b=>b.type==='pairs'&&b.link===l.id));
const wwWhat=(l,n)=>trn('requirements.n_400g_alien_wave_on_your',n,{a:CITY[l.a].name,b:CITY[l.b].name});
function fiberOffer(t,l,nm,cut=1){
if(t==='wwave'){const slots=1+Math.floor(Math.random()*Math.min(4,freeCh(l)));

View file

@ -1,4 +1,4 @@
function bizSf(b){const T=BIZ_T[b.type];return T.fiber?(fiberOk(b)?1:0):T.l2?clamp((R.l2[b.id]||0)/b.bw,0,1):bizSfOf(R,b.city);}
function bizSf(b,ids){const T=BIZ_T[b.type];return T.fiber?(fiberOk(b,ids)?1:0):T.l2?clamp((R.l2[b.id]||0)/b.bw,0,1):bizSfOf(R,b.city);}
// Protected business traffic has its own served share; without the rule it shares the city's.
function bizSfOf(r,c){const d=r.bizDem&&r.bizDem[c];return teRules()&&teRules().protect&&d>1e-6?clamp(r.bizSrv[c]/d,0,1):sfOf(r,c);}
function bCities(o){return o.sites?o.sites.slice():[o.city,o.city2].filter(Boolean);}
@ -39,6 +39,6 @@ function spawnSpecial(t){const T=BIZ_T[t],mine=Object.keys(S.pops),lv=S.lvl||1;
while(sites.length<n&&pool.length)sites.push(pool.splice(Math.floor(Math.random()*pool.length),1)[0]);if(sites.length<3)return;
S.offers.push({id:uid(),city:hub,city2:null,sites,name:nm(),type:t,bw:pick(T.opts),rate:700*sites.length,reqs:[],exp:S.day+40});return;}
const ready=S.links.filter(l=>l.kind==='df'&&l.ready<=S.day);
if(t==='wwave'){const ls=ready.filter(l=>freeCh(l)>=1&&!S.offers.some(o=>o.link===l.id));if(!ls.length)return;S.offers.push(fiberOffer(t,pick(ls),nm));return;}
if(t==='dfpair'){const ls=ready.filter(l=>pairFree(l)&&!S.offers.some(o=>o.link===l.id));if(!ls.length)return;S.offers.push(fiberOffer(t,pick(ls),nm));}
if(t==='wwave'){const u=fiberUse(),ls=ready.filter(l=>freeCh(l,u)>=1&&!S.offers.some(o=>o.link===l.id));if(!ls.length)return;S.offers.push(fiberOffer(t,pick(ls),nm));return;}
if(t==='dfpair'){const u=fiberUse(),ls=ready.filter(l=>pairFree(l,u)&&!S.offers.some(o=>o.link===l.id));if(!ls.length)return;S.offers.push(fiberOffer(t,pick(ls),nm));}
}

View file

@ -38,7 +38,7 @@ function dayTick(){
b.slaFor=a.start;S.cash-=SLA_PEN;b.mood=Math.max(0,b.mood-18);S.stats.sla=(S.stats.sla||0)+1;
toast(tr('day_tick.sla_breach_name_the_attack_on',{name:esc(b.name),city:CITY[a.city].name,pen:fmt$(SLA_PEN)}),'bad');sfx('err');}}
// businesses mood
for(const b of S.biz.slice()){if(eventTick(b,d))continue;b.pm=b.mood;const sf=bizSf(b),reachOk=BIZ_T[b.type].l2||BIZ_T[b.type].fiber||R.reach>0.99||scnRule('local'),scrubOk=b.type!=='scrub'||(hasScrub()&&!scrubFar(b.city)),divOk=reqOk(b);
const ids=linkIds();for(const b of S.biz.slice()){if(eventTick(b,d))continue;b.pm=b.mood;const sf=bizSf(b,ids),reachOk=BIZ_T[b.type].l2||BIZ_T[b.type].fiber||R.reach>0.99||scnRule('local'),scrubOk=b.type!=='scrub'||(hasScrub()&&!scrubFar(b.city)),divOk=reqOk(b);
if(sf>=0.95&&reachOk&&scrubOk&&divOk)b.mood=Math.min(100,b.mood+2);
else if(S.day-(b.since||0)<15){}
else{b.mood-=(Math.max(0,0.95-sf)*45+(!reachOk?3:0)+(!scrubOk?4:0)+(!divOk?3:0));

View file

@ -296,3 +296,25 @@ test('Auto-TE routes around a full link exactly like the plain path', () => {
}
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'})");
// Dark fibers: one fine, one cut, one waiting for gear, one not built yet, and a second link reusing an id (the first one wins).
ev(`(()=>{const sys=Object.keys(DF_SYS)[0],L=(id,o)=>S.links.push({id,a:'fra',b:'ams',kind:'df',sys,km:400,ready:0,ch:[],own:true,...o});
L('f1',{});L('f2',{cut:S.day+5});L('f3',{eqDown:S.day+5});L('f4',{ready:S.day+30});L('f1',{cut:S.day+5});
let n=0;const B=(type,link,o)=>S.biz.push({id:'b'+(n++),type,link,city:'fra',city2:'ams',bw:1,rate:1,mood:80,reqs:[],...o});
for(const l of ['f1','f2','f3','f4','gone'])for(const t of ['wwave','dfpair','pairs'])B(t,l,{slots:t==='wwave'?2:undefined});
B('wwave','f1',{slots:3});B('wwave','f1',{});B('dfpair','f3',{});})()`);
const r = JSON.parse(ev(`(()=>{const ids=linkIds(),u=fiberUse(),out=[];
for(const b of S.biz)if(BIZ_T[b.type].fiber)out.push([b.id,fiberOk(b),fiberOk(b,ids),bizSf(b),bizSf(b,ids)]);
for(const l of S.links)if(l.kind==='df')out.push([l.id,soldSlots(l),soldSlots(l,u),freeCh(l),freeCh(l,u),pairFree(l),pairFree(l,u)]);
return JSON.stringify(out);})()`));
for (const row of r) {
const [id, ...v] = row;
for (let i = 0; i < v.length; i += 2) assert.equal(v[i + 1], v[i], `${id}: column ${i / 2}`);
}
assert.ok(r.some((x) => x[0] === 'f1' && x[1] === 5), 'f1 has 5 alien-wave slots sold');
assert.ok(r.some((x) => x[1] === true) && r.some((x) => x[1] === false), 'some fibers are up and some are not');
assert.deepEqual(errors, []);
});