Prefer keeps the say at its own POP only, and the direct ranks stop slowing the game day

Review of #205:

- Prefer no longer switches the carrier-direct rule off for the whole network.
  A POP with a Prefer-marked transit port keeps what Prefer says; every other
  POP keeps the rule. compute(), traceroute and the reference loop in the tests
  all use the same per-POP rule, and a new test covers it (New York and Chicago,
  Prefer on one POP's Tell-Ya port).
- dayTick() cost. The harness is neutral (the same page on both sides differs by
  1%), but the cost was real: the four extra rank passes inside compute()'s main
  loop made V8 deoptimise compute() 954 times in 30 game days against 46 on main.
  The direct ranks now run in a small function of their own, in the plain way the
  reference loop does (their lists are one carrier's ports), so the main loop is
  the one main has, and the setup moved out of compute(). 61 deopts instead of 954.
- Italian: "Porta qui" read as "port here" next to "Porta più vicina"; it is
  "Instrada qui" now, and "Ogni POP instrada il traffico..." matches.
- Help text: says that real BGP carries the traffic from every POP (the shorter
  AS path beats IGP distance), that Carry here does that here, and that Nearest
  port is the default. All six languages.
This commit is contained in:
Christopher Hawker 2026-11-01 20:07:23 +11:00
parent b8845413d3
commit eb78d7104e
14 changed files with 77 additions and 23 deletions

View file

@ -24,8 +24,9 @@ function traceroute(src,to){const P=trPaths(src),me=slug(S.name)+'.net',lines=[]
const N=NET[to.net],ex=trExits(to.net).filter(e=>P.dist[e.city]!==undefined);
lines.unshift(`traceroute to www.${slug(N.name)}.example (${N.name}, AS${N.asn}) from ${CITY[src].name}, 30 hops max`);
if(!ex.length){lines.push(' 2 * * *\n 3 * * * ← '+tr('traceroute.n_network_unreachable'));return {text:lines.join('\n'),why:tr('traceroute.nothing_on_your_network_reaches_net',{net:N.name,city:CITY[src].name}),asPath:null};}
// As in compute(): a carrier's own port (or its customers' carrier) goes before other transit, at this POP or anywhere when it carries.
const hiAny=S.transit.some(t=>t.pref==='high'&&popUp(t.city)&&!(t.down>S.day)&&liveN(t.city,t.site,t.n)),dtier=e=>hiAny||e.k!=='t'||e.pref==='low'?0:directTier(e.tnet,N.id);
// As in compute(): a carrier's own port (or its customers' carrier) goes before other transit, at this POP or anywhere when it
// carries, except from a POP with a Prefer-marked transit port, where Prefer keeps the say.
const hiHere=S.transit.some(t=>t.city===src&&t.pref==='high'&&popUp(t.city)&&!(t.down>S.day)&&liveN(t.city,t.site,t.n)),dtier=e=>hiHere||e.k!=='t'||e.pref==='low'?0:directTier(e.tnet,N.id);
const rk=e=>{const r=e.pref==='low'?4:e.rank,t=dtier(e);return t&&(e.carry||e.city===src)?Math.min(r,DIRECT_RANK[t][e.carry?0:1]):r;};
ex.sort((a,b)=>rk(a)-rk(b)||(a.pref==='high'?0:1)-(b.pref==='high'?0:1)||P.dist[a.city]-P.dist[b.city]);
const e=ex.find(x=>x.used<x.cap*0.999)||ex[0];walk(P.path(e.city));

View file

@ -5,3 +5,10 @@
DIRECT_RANK[tier] = [carries, this POP only]. compute() and traceroute() share this. */
const DIRECT_RANK=[null,[2.92,2.93],[2.94,2.95]];
const directTier=(carrier,dest)=>carrier===dest?1:(NET[dest].up&&NET[dest].up.includes(carrier))?2:0;
// What compute() needs of the rule, built once per call and kept out of compute(), whose size the JIT is sensitive to.
// prefc[ci]: 1 where a Prefer-marked transit port has the say, so the rule is off for flows from that POP.
// dirOf(id): the carriers you buy from that are direct for network id (itself: tier 1; one of its upstreams: tier 2), or null.
function directSetup(E,NC){const cars=new Set(),prefc=new Uint8Array(NC);
for(const e of E)if(e.k==='t'){cars.add(e.net);if(e.pref==='high')prefc[e.ci]=1;}
const dirOf=id=>{let m=null;if(cars.has(id))(m=new Map()).set(id,1);const up=NET[id].up;if(up)for(const u of up)if(cars.has(u)&&!(m&&m.has(u)))(m||(m=new Map())).set(u,2);return m;};
return {prefc,dirOf};}

View file

@ -151,10 +151,10 @@ function compute(){
// LOCAL_PREF on an upstream moves it ahead of or behind the other transits, never ahead of peering (#96).
const eff=(e,n)=>{let r=base(e,n);if(TR){if(e.k==='t'&&TR.up[e.net])r=TR.up[e.net]==='prefer'?2.9:3.5;if(e.prep)r=4.4;if(e.k!=='c'&&TR.drain.has(e.city))r=4.6;const o=TR.net[n];if(o){if(o.prefer===e.k)r=0.5;else if(o.avoid===e.k)r=4.5;}}return r;};
// A carrier's own port for traffic bound for it or for its customers (25-direct-egress.js): tried before plain transit, but
// never over RouteScript, a Prefer-marked port or a port set to backup, which all keep the say they had. Such a port is
// also an ordinary transit exit (rank 3), so POPs without one of its own can still use it.
const hiAny=E.some(e=>e.k==='t'&&e.pref==='high');
const dr=(e,n)=>{if(hiAny||e.k!=='t')return 0;const t=directTier(e.net,n);return t&&eff(e,n)===3?DIRECT_RANK[t][e.carry?0:1]:0;};
// never over RouteScript or a port set to backup, which keep the say they had, and not from a POP with a Prefer-marked
// transit port (PREFC), where Prefer keeps it. Such a port is also an ordinary transit exit (rank 3), so POPs without one of
// its own can still use it. DSEL: for each direct rank, the networks that have an exit there.
const DIR=directSetup(E,NC),PREFC=DIR.prefc,dirOf=DIR.dirOf,DSEL=new Map();
const RANKS=TR?[0,0.5,1,2,CONE_RANK,2.9,2.92,2.93,2.94,2.95,3,3.5,4,4.4,4.5,4.6]:[0,1,2,CONE_RANK,2.92,2.93,2.94,2.95,3,4];
// With `protect customers`, business flows take every rank first; home users get what is left.
const PASSES=TR&&TR.protect?[x=>x.bz,x=>!x.bz]:null;
@ -164,8 +164,8 @@ function compute(){
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);
const d=dr(e,id);if(d){RSET.add(d);(m[d]||(m[d]=[])).push(e);}}
const byRank={},RSET=new Set();for(let j=0;j<NET_IDS.length;j++){const id=NET_IDS[j],m=byRank[id]={},dm=dirOf(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);
if(dm&&r===3&&e.k==='t'){const t=dm.get(e.net);if(t){const d=DIRECT_RANK[t][e.carry?0:1];let ds=DSEL.get(d);if(!ds)DSEL.set(d,ds=new Uint8Array(NET_IDS.length));ds[j]=1;RSET.add(d);(m[d]||(m[d]=[])).push(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;}}
// The flow being routed, and from its POP: distances, arriving edges and the paths walked so far, by city index.
@ -215,16 +215,28 @@ function compute(){
// Stable, in place: short lists by insertion (no garbage), long ones by the built-in sort. Both are stable with the
// same comparison, so the order is the same either way.
const sortD=a=>{const n=a.length;if(n>24){a.sort(byDist);return;}for(let i=1;i<n;i++){const x=a[i];let j=i-1;while(j>=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
// 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];
for(let fi=0,FN=flows.length;fi<FN;fi++){f=flows[fi];
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++;
let lst=byRank[f.n][rank];if(!lst)continue;if(lst.cs){lst=lst.cs[COMP[f.ci]];if(!lst)continue;}
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]);}
else for(let li=0;li<lst.length;li++)if(usable(lst[li]))REST.push(lst[li]);
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(const pass of PASSES||[null])for(const rank of RANKS){
if(!RSET.has(rank))continue;
// A carrier's own port that is not set to carry only takes the traffic of the POP it sits in.
const LOC=rank===DIRECT_RANK[1][1]||rank===DIRECT_RANK[2][1];
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().
for(let fi=0,FN=flows.length;fi<FN;fi++){f=flows[fi];
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++;
let lst=byRank[f.n][rank];if(!lst)continue;if(lst.cs){lst=lst.cs[COMP[f.ci]];if(!lst)continue;}
if(LOC){G.length=0;const here=lst.at.get(f.ci);if(!here)continue;for(let h=0;h<here.length;h++){const e=here[h];if(e.used<e.cap&&!gone(e))G.push(e);}if(G.length)sendGroup(G,0,G.length);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

View file

@ -2157,7 +2157,7 @@
"help.want_to_know_how_it_really":"Want to know how it really works?",
"help.what_a_wave_costs_depends_on":"What a wave costs depends on where it runs. Europe is cheap (lots of competing fiber), North America is average, Asia and South America are pricier, and anything into Africa is several times the price. Every route has its own going rate, and new leases get about 8% cheaper each year, so waiting can pay. The link planner tells you how a route compares.",
"help.you_built_a_cartoon_internet_now":"You built a cartoon internet. Now see how the real one works:",
"help.you_pay_a_bigger_network_per":"You pay a bigger network per Gbps and it delivers your traffic anywhere. Easy, full reach, never free.",
"help.you_pay_a_bigger_network_per":"You pay a bigger network per Gbps and it delivers your traffic anywhere. Easy, full reach, never free. A carrier’s own port takes the traffic bound for that carrier. In real BGP that holds from every POP, because the shorter AS path beats IGP distance; here it is a port’s Carry here switch, and Nearest port is the default.",
"help.you_re_a_tier_1":"You're a <span style=\"color:var(--me)\">Tier 1</span>!",
"help.your_fiber_links_are_leased_waves":"Your fiber links are leased waves at a flat monthly rate. Longer and bigger costs more (10G → 6.4T), and subsea is 1.8× the price.",
"helpers.y_y_mon_d":"Y{y} · {mon} {d}",