# gas-dispatch: lossless weighted coefficient dispatch Credit: Taylor (gtmcknight) wrote the Gaussian policy, filter, replica and qm tables. The complete contract below preserves that credit in its header. This is a local gas experiment, not a board submission. ## Changes **Measured result: 235 gas less at the engine peak, 496.311383 less per callback on average; the 40,000-gas target is NOT met.** All 32 per-seed edges are exactly equal. Both strategies have zero OutOfGas and zero other callback errors. Fast peaks at 206,076, below 245,000; its runtime is 20,772 bytes, below 24,576. 1. Replaced only `cw0..cw19` and `cw` with a globally weighted alphabetic binary decision tree, partitioned into 32 small functions with at most 16 coefficient leaves each. Profiled stripe word access counts on Taylor's replica seeds 0..4, 5,000 steps each, using the unchanged qt1 Python reference; each weight is `max(100, observed_count)`. Dynamic programming minimizes weighted comparison depth: `cost(l,h) = sum(weights[l:h]) + min(cost(l,k)+cost(k,h))`. The floor avoids extreme paths for unobserved words. Function boundaries follow the resulting tree. This is the only executable edit: its joint measured saving is the 235 peak / 496.311383 mean above. These numbers cannot be attributed separately to weighting versus partitioning without another experiment. 2. Added one explanatory header comment, with Taylor's attribution retained; zero executable effect. No assembly, coefficient blob copying, storage change, basis change, fee conversion change, or new policy/reference is involved. `stripe`, both `surf` evaluations and all their integer operations remain verbatim. Exhaustively evaluated all 306 indices in both dispatch trees: every returned 256-bit word is identical. Also checked source equality outside the dispatch region after removing the added comment. Thus coefficient values and rounding are unchanged independently of the sampled fee tests. Runtime grows by 167 bytes (20,605 -> 20,772), leaving 3,804 bytes under the cap. The 40k reduction target misses by 39,765 on this sample. Do not compare 206,076 directly against the brief's published 208,775 as if the difference were this optimization's saving: this sample's qt1 peak is 206,311. Qt1 was extracted from [Q](https://api.imd.fun/artifacts/8a6e303b662e1a5ea9bb47f31acedd3988ef65b3ddb66d074517aa59ad41ca40): 42,990 source bytes, SHA-256 `c68887cff61180e12ba2b1ceb333edf76a830f91f63b5d775a8806fb9ec08aca`, verified before editing. Q's Contract design and Notes and limits were read. The QMS1 reference blob was decoded from [qm-spline](https://api.imd.fun/artifacts/17d249ab0c33fc44931d9b105cf3f9e3846a560bd518b67310c2a56defacdecc) and its full SHA-256 `3ac7cb8811ecfb6a17f499aa4e9b70dd55f7fb6432b3aec3b904e11a3dece38e` checked. ### Checks and method - Setup: pinned challenge and Taylor checkouts side by side in `test/scratch/`; ran the explicitly requested `sh ../amm-sim/setup-fast.sh` once from the challenge root. Wheel shared-object SHA-256 `ff88b6353c2aad7342f0a26194f7347e78b9c3b38d683a7449006d423978cd14` matched. Starter validation passed. Later Python/engine commands sourced `scratch/env.sh`. No repository configuration was modified. - Gate P: unmodified `python3 -I ../amm-sim/research/solved/gates.py` passed before optimization. All four columns match [Taylor's published gates](https://raw.githubusercontent.com/gtmcknight/amm-sim/4812a6382311624e1506aca131ca35c378f930d9/research/solved/README.md): | seed | perfect | Gaussian/plain | Gaussian/qm | lattice/qm | |---|---:|---:|---:|---:| |10000|448.261189|443.455598|443.522608|443.663520| |10001|576.779199|571.463409|571.623551|571.977293| |10002|531.886497|525.168028|525.865859|526.411631| - Gate Q: no new Python reference or fee policy was introduced. The original Q reference is retained; no new 60000..61023 selection or 10000..12047 score is claimed. Dispatch selection used only local gas screens, never engine score changes. - Gate F: 50 replica runs, seeds 0..49, 5,000 steps each; took the first 2,000 callbacks of each, exactly 100,000 total. Replica routing follows Q's original `QMRef`; both EVM strategies receive the identical callback stream with WAD-encoded reserves/amounts, independently initialized at 100 X / 10,000 Y. Every pair of 64-byte returned fee values is identical. Fast versus Python max absolute fee error is **0.0000045730487712586365 bps**, below 0.01. EVM checks use pyrevm 0.3.7; the Python reference is stdlib-only. Replica and synthetic screens are not engine scores. - `amm-match validate contracts/src/Qt1Fast.sol` passed: `Strategy 'TaylorGaussianQMSpline' validated successfully!`. Compiled with the challenge's solc 0.8.24, optimizer 200, viaIR, Paris. Runtime sizes were obtained from `deployed_bytecode`, not creation bytecode. - Engine: exactly seeds 9800288..9800319, each once per strategy, fast then qt1, independently against the 30 bps VanillaStrategy, eight workers, one 32-seed chunk per strategy. Used [Taylor's harness `run` and `paired`](https://raw.githubusercontent.com/gtmcknight/amm-sim/4812a6382311624e1506aca131ca35c378f930d9/harness.py), with gas reset/read around each batch. Configurations use the exact pinned `MatchRunner._build_configs` body, changing only `range(self.n_simulations)` to the reserved seed range; its RNG is independently initialized from each seed. This avoids allocating 9.8 million unused preceding configurations without changing any draw, draw order, or configuration field. Batch wall times were fast 57.915 s, qt1 53.795 s. No reserved engine seed was reused. Both edge components (submission and normalizer) match, and every submission difference is exactly zero, not just within 1e-9. All following gas statistics are **local engine measurements on these 32 seeds**. Means are `sum_ok_gas / ok`; there are no failed calls hidden in the denominator. `paired.worst5` ties are listed in seed order (`edge` is fast, `v2_edge` is qt1, names inherited from the harness). ```json { "approach": "globally profile-weighted binary coefficient dispatch; original 306 packed words and all fee arithmetic preserved; 32 subfunctions with <=16 leaves", "seeds": { "start": 9800288, "end_inclusive": 9800319, "runs_per_strategy_per_seed": 1 }, "n": 32, "runtime_bytes": { "qt1": 20605, "fast": 20772 }, "gas": { "qt1": { "afterSwap": { "calls": 349704, "oog": 0, "peak": 206311, "mean": 137967.4611614394, "sum_ok_gas": 48247773038 }, "afterInitialize": { "peak": 90858 }, "callback_errors": { "oog": 0, "revert": 0, "other_halt": 0, "tx_error": 0, "bad_return": 0 } }, "fast": { "afterSwap": { "calls": 349704, "oog": 0, "peak": 206076, "mean": 137471.14977809804, "sum_ok_gas": 48074210962 }, "afterInitialize": { "peak": 90858 }, "callback_errors": { "oog": 0, "revert": 0, "other_halt": 0, "tx_error": 0, "bad_return": 0 } } }, "edges_identical": true, "mean_diff": 0.0, "differing_seeds": [], "paired": { "n": 32, "mean_diff": 0.0, "se": 0.0, "worst5": [ { "seed": 9800288, "diff": 0.0, "edge": 418.9985017484821, "v2_edge": 418.9985017484821 }, { "seed": 9800289, "diff": 0.0, "edge": 657.4507614998981, "v2_edge": 657.4507614998981 }, { "seed": 9800290, "diff": 0.0, "edge": 401.44834912854674, "v2_edge": 401.44834912854674 }, { "seed": 9800291, "diff": 0.0, "edge": 478.1584423108849, "v2_edge": 478.1584423108849 }, { "seed": 9800292, "diff": 0.0, "edge": 450.5909152103592, "v2_edge": 450.5909152103592 } ] }, "gate_F": { "callbacks": 100000, "bit_differences": 0, "max_fee_error_bps": 4.5730487712586365e-06, "seeds": [ 0, 49 ], "callbacks_per_seed": 2000 }, "dispatch_audit": { "words_checked": 306, "identical": true, "outside_dispatch_identical": true }, "fast_source_sha256": "164c979f77c702b8654b09af413e78de0bd6cec6b8834cb8612646c4cf1a00e5", "peak_saving": 235, "mean_saving": 496.31138334135176 } ``` ## Code Complete `contracts/src/Qt1Fast.sol`; supply the challenge's unchanged `AMMStrategyBase.sol` and `IAMMStrategy.sol` beside it. ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import {AMMStrategyBase} from "./AMMStrategyBase.sol"; import {TradeInfo} from "./IAMMStrategy.sol"; // Solved AMM policy by Taylor (gtmcknight), amm-sim 4812a638 research/solved: Gaussian filter + qm tables (qm_k1e-5.json), // fee rule of real.py's qm variant (sd level, phase, synced flag). All credit for the policy, filter and tables: Taylor. // Lossless globally weighted coefficient dispatch; fee arithmetic and packing are unchanged. // Tables compressed by qm-spline (cubic B-splines, QMS1 blob sha256 3ac7cb88...); filter, 3-slot state, math and fee conversion // from trim-2 (port-3's filter); synced flag stored in bit 255 of slot 2. // Taylor source: https://github.com/gtmcknight/amm-sim/tree/4812a6382311624e1506aca131ca35c378f930d9/research/solved contract Strategy is AMMStrategyBase { int256 constant U=1e18; int256 constant LN2=693147180559945309; uint256 constant M64=0xffffffffffffffff; // slots[0] = m (int128) | v (int128)<<128 ; slots[1] = sp (int128) | r (int128)<<128 ; slots[2] = fb | fa<<64 | t<<128 | cnt<<192 function getName() external pure override returns(string memory) { unchecked {return "TaylorGaussianQMSpline";} } function mul(int256 a,int256 b) internal pure returns(int256){ unchecked {return a*b/U;} } function divi(int256 a,int256 b) internal pure returns(int256){ unchecked {return a*U/b;} } function ab(int256 x) internal pure returns(int256){ unchecked {return x<0?-x:x;} } function clip(int256 x,int256 a,int256 b) internal pure returns(int256){ unchecked {return xb?b:x);} } function ln(int256 x) internal pure returns(int256 z){ unchecked { require(x>0); int256 k; while(x< U){x*=2;k--;} while(x>=2*U){x/=2;k++;} int256 y=divi(x-U,x+U); int256 y2=mul(y,y); int256 q=y; z=y; for(int256 n=3;n<=29;n+=2){q=mul(q,y2);z+=q/n;} return 2*z+k*LN2; } } function exp(int256 x) internal pure returns(int256){ unchecked { if(x< -42*U)return 0; require(x<60*U); int256 k=(x>=0?x+LN2/2:x-LN2/2)/LN2; int256 r=x-k*LN2; int256 r2=mul(r,r); int256 even=665280*U+mul(r2,75600*U+mul(r2,840*U+r2)); int256 odd=mul(r,332640*U+mul(r2,10080*U+mul(r2,42*U))); int256 s=divi(even+odd,even-odd); return k>=0?s*int256(uint256(1)<>128>0){z>>=128;r<<=64;} if(z>>64>0){z>>=64;r<<=32;} if(z>>32>0){z>>=32;r<<=16;} if(z>>16>0){z>>=16;r<<=8;} if(z>>8>0){z>>=8;r<<=4;} if(z>>4>0){z>>=4;r<<=2;} if(z>>2>0){r<<=1;} for(uint256 i=0;i<7;i++)r=(r+n/r)/2; return int256(r10*U)return 0;return divi(exp(-mul(x,x)/2),2506628274631000200);} } // Hart rational normal tail approximation; erf(x)=2*Phi(x*sqrt(2))-1. function cdf(int256 x,int256 density) internal pure returns(int256){ unchecked { int256 y=ab(x);if(y>8*U)return x>0?U:int256(0); int256 n=35262496599891000; n=mul(n,y)+700383064443688000;n=mul(n,y)+6373962203531650000; n=mul(n,y)+33912866078383000000;n=mul(n,y)+112079291497871000000; n=mul(n,y)+221213596169931000000;n=mul(n,y)+220206867912376000000; int256 d=88388347648318400; d=mul(d,y)+1755667163182640000;d=mul(d,y)+16064177579207000000; d=mul(d,y)+86780732202946100000;d=mul(d,y)+296564248779674000000; d=mul(d,y)+637333633378831000000;d=mul(d,y)+793826512519948000000; d=mul(d,y)+440413735824752000000; int256 tail=mul(mul(density,2506628274631000200),divi(n,d));return x>0?U-tail:tail; } } function trunc(int256 m,int256 v,int256 lo,int256 hi) internal pure returns(int256 Z,int256 mt,int256 vt){ unchecked { if(v<=0)return (lo<=m&&m<=hi?U:int256(0),m,0); int256 sd=root(v);int256 a=divi(lo-m,sd);int256 b=divi(hi-m,sd); int256 pa=pdf(a);int256 pb=pdf(b); Z=cdf(b,pb)-cdf(a,pa); if(Z<1e6)return(Z,m>(16*L):K1>>(16*(L-16)))&0xffff); } } // cubic B-spline basis at x (1/64 units) by Cox-de Boor; returns span m, N[q] = basis of coefficient m-3+q (WAD) function bas(uint256 K0,uint256 K1,uint256 n,int256 x,int256[4] memory N) internal pure returns(uint256 m){ unchecked { if(x>=kn(K0,K1,n)){m=n-1;}else{uint256 lo=3;uint256 hi=n;while(hi-lo>1){uint256 md=(lo+hi)>>1;if(kn(K0,K1,md)<=x)lo=md;else hi=md;}m=lo;} int256 l1=x-kn(K0,K1,m);int256 l2=x-kn(K0,K1,m-1);int256 l3=x-kn(K0,K1,m-2); int256 r1=kn(K0,K1,m+1)-x;int256 r2=kn(K0,K1,m+2)-x;int256 r3=kn(K0,K1,m+3)-x; int256 tp=U/(r1+l1);int256 n0=r1*tp;int256 n1=l1*tp; tp=n0/(r1+l2);n0=r1*tp;int256 sv=l2*tp;tp=n1/(r2+l1);n1=sv+r2*tp;int256 n2=l1*tp; tp=n0/(r1+l3);N[0]=r1*tp;sv=l3*tp;tp=n1/(r2+l2);N[1]=sv+r2*tp;sv=l2*tp;tp=n2/(r3+l1);N[2]=sv+r3*tp;N[3]=l1*tp; } } // nC consecutive coefficient lanes starting at flat index idx function stripe(uint256 idx,uint256 nC) internal pure returns(uint256 x){ unchecked { uint256 o=idx&15;x=cw(idx>>4)>>(16*o); if(o+nC>16)x|=cw((idx>>4)+1)<<(16*(16-o)); } } function ln16(uint256 x,uint256 p) internal pure returns(int256){ unchecked { return int256((x>>(16*p))&0xffff)-32768; } } // one coefficient stripe contracted with the two column-node bases: ta = sum C0[p] c[p], tb = sum C1[p] c[p+dc] function row2(uint256 x,uint256 dc,int256[4] memory C0,int256[4] memory C1) internal pure returns(int256 ta,int256 tb){ unchecked { ta=C0[0]*ln16(x,0)+C0[1]*ln16(x,1)+C0[2]*ln16(x,2)+C0[3]*ln16(x,3); if(dc==0){tb=C1[0]*ln16(x,0)+C1[1]*ln16(x,1)+C1[2]*ln16(x,2)+C1[3]*ln16(x,3);} else{tb=C1[0]*ln16(x,dc)+C1[1]*ln16(x,dc+1)+C1[2]*ln16(x,dc+2)+C1[3]*ln16(x,dc+3);} } } function bern(uint256 l,uint256 s) internal pure returns(int256){ unchecked { int256 a=int256(l);int256 b=7-a; return s==0?b*b*b:(s==1?3*a*b*b:(s==2?3*a*a*b:a*a*a)); } } // node value: raw sum (1e36 * 1/(8*343) bps) -> WAD bps, clamped at 0 function cl(int256 F) internal pure returns(int256){ unchecked { F=F/U/2744;return F<0?int256(0):F; } } // fee surface F (WAD bps) of table tb at grid point (pw,p2) (WAD grid units): F clamped at 0 at the 4 nodes x 2 sd levels, // bilinear in (row, column), then linear in sd (real.py's RealPolicy.lookup on the decoded tables) function surf(uint256 tb,int256 pw,int256 p2,uint256 k,int256 tt) internal pure returns(int256){ unchecked { (uint256 KR0,uint256 KR1,uint256 KC0,uint256 KC1,uint256 nr,uint256 nc,uint256 base)=tab(tb); uint256 i=uint256(pw/U);if(i>319)i=319;int256 f=pw-int256(i)*U; uint256 j=uint256(p2/U);{uint256 jm=tb==0?11:47;if(j>jm)j=jm;}int256 g=p2-int256(j)*U; int256[4] memory A0;int256[4] memory A1;int256[4] memory C0;int256[4] memory C1; uint256 mi=bas(KR0,KR1,nr,int256(i*64),A0);uint256 dr=bas(KR0,KR1,nr,int256(i*64+64),A1)-mi; uint256 mj=bas(KC0,KC1,nc,int256(j*64),C0);uint256 dc=bas(KC0,KC1,nc,int256(j*64+64),C1)-mj; int256[8] memory W;W[0]=bern(k,0);W[1]=bern(k,1);W[2]=bern(k,2);W[3]=bern(k,3);W[4]=bern(k+1,0);W[5]=bern(k+1,1);W[6]=bern(k+1,2);W[7]=bern(k+1,3); // Fv[l*4 + rownode*2 + colnode] int256[8] memory Fv; uint256 i0=base+(mi-3)*nc+mj-3;uint256 sn=nr*nc; for(uint256 q=0;q=dr&&q>(16*(L&15)))&0xffff)-32768; } } // real.py/evalpol _pos2 over pol_mid's W2L (grid units of 5 bps), WAD in and out function pos2(int256 v) internal pure returns(int256){ unchecked { if(v<=-160*U)return 0; if(v>=160*U)return 48*U; uint256 lo=0;uint256 hi=48; while(hi-lo>1){uint256 md=(lo+hi)>>1;if(w2l(md)*U<=v)lo=md;else hi=md;} int256 a=w2l(lo);return int256(lo)*U+(v-a*U)/(w2l(hi)-a); } } function sdl(uint256 k) internal pure returns(int256){ unchecked { return int256((0x221a14100c080400>>(8*k))&0xff); } } // qm fee rule: w, w2 offsets (WAD bps), phase, synced flag, belief variance v (WAD, ln-price^2) function fees(int256 w,int256 w2,uint256 ph,bool syn,int256 v) internal pure returns(int256 fb,int256 fa){ unchecked { int256 sd=root(v)*10000;uint256 k; while(k<6&&sd>=sdl(k+1)*U)k++; int256 tt=clip((sd-sdl(k)*U)/(sdl(k+1)-sdl(k)),0,U); int256 pw=clip(w/5+160*U,0,320*U);int256 wq=(pw-160*U)*5; uint256 tb;int256 p2;int256 nc1; if(ph==0){tb=0;p2=clip(w2/5,-6*U,6*U)+6*U;nc1=12;} else{tb=(ph==1?1:3)+(syn?0:1);p2=pos2(w2/5);nc1=48;} int256 xa=surf(tb,pw,p2,k,tt)+wq-w; int256 xb=surf(tb,320*U-pw,nc1*U-p2,k,tt)+w-wq; fb=xb>0?U-exp(-xb/10000):int256(0);fa=xa>0?U-exp(-xa/10000):int256(0); fb=clip(fb,0,U/10);fa=clip(fa,0,U/10); } } function afterInitialize(uint256 x,uint256 y) external override returns(uint256,uint256){ unchecked { int256 sp=int256(y*1e18/x); slots[0]=uint256(uint128(int128(ln(sp)))); slots[1]=uint256(uint128(int128(sp)))*(1+(uint256(1)<<128)); slots[2]=2823606104386589*(1+(uint256(1)<<64)); return(2823606104386589,2823606104386589); } } // filter update over a gap of dt steps; returns new m, v, rival spot r and the arb flag function update(int256 m,int256 v,int256 fb,int256 fa,int256 sp,int256 r,int256 s,bool buy,uint256 dt) internal pure returns(int256,int256,int256,bool arb){ unchecked { int256 lo=ln(mul(sp,U-fb));int256 hi=ln(divi(sp,U-fa)); uint256 n=dt-1;if(n>12)n=12; for(uint256 i=0;i0?divi(la,la+lr):int256(0);arb=pi>U/2; if(pi>5e15&&pi<995e15){ m=mul(pi,lp)+mul(U-pi,mt); v=mul(pi,mul(lp-m,lp-m))+mul(U-pi,vt+mul(mt-m,mt-m)); }else if(arb){m=lp;v=0;}else{m=mt;v=vt;} int256 pe=exp(m);r=clip(r,mul(pe,997e15),divi(pe,997e15)); return(m,v,r,arb); } } function afterSwap(TradeInfo calldata t) external override returns(uint256,uint256){ unchecked { uint256 s0=slots[0];uint256 s1=slots[1];uint256 s2=slots[2]; int256 m=int256(int128(uint128(s0)));int256 v=int256(int128(uint128(s0>>128))); int256 sp=int256(int128(uint128(s1)));int256 r=int256(int128(uint128(s1>>128))); int256 fb=int256(s2&M64);int256 fa=int256(s2>>64&M64);uint256 cnt=s2>>192&0xffffffff;bool syn=s2>>255==1; int256 s=int256(t.reserveY*1e18/t.reserveX);bool arb; if(t.timestamp>(s2>>128&M64)){(m,v,r,arb)=update(m,v,fb,fa,sp,r,s,t.isBuy,t.timestamp-(s2>>128&M64));cnt=0;syn=false;} uint256 ph; if(!arb){ int256 c=t.isBuy?divi(mul(s,U-fb),997e15):divi(mul(s,997e15),U-fa); if(t.isBuy?cr){r=c;syn=true;} cnt++;ph=cnt==1?1:2; } int256 w=(ln(s)-m)*10000;int256 rr=(ln(r)-m)*10000; (fb,fa)=fees(w,rr,ph,syn,v); slots[0]=uint256(uint128(int128(m)))|uint256(uint128(int128(v)))<<128; slots[1]=uint256(uint128(int128(s)))|uint256(uint128(int128(r)))<<128; slots[2]=uint256(fb)|uint256(fa)<<64|t.timestamp<<128|cnt<<192|(syn?uint256(1)<<255:0); return(uint256(fb),uint256(fa)); } } } ``` ## Commands Commands were run from the repository root unless prefixed `C:` (the pinned challenge checkout). `C=test/scratch/amm-challenge`, `T=test/scratch/amm-sim`; `E` means `. scratch/env.sh &&`. All experimental scripts and logs were under `test/scratch/`. The list below records the shell operations, including failed attempts; heredoc writes are identified by script name/purpose rather than reproducing abandoned generated contracts. ```text pwd; rg --files -g '!lib/**' -g '!node_modules/**' | head -40 mkdir -p test/scratch git clone https://github.com/benedictbrady/amm-challenge test/scratch/amm-challenge git -C test/scratch/amm-challenge checkout 6b6921f8d463314aa7c96234e1be25a25245f704 git clone https://github.com/gtmcknight/amm-sim test/scratch/amm-sim git -C test/scratch/amm-sim checkout 4812a6382311624e1506aca131ca35c378f930d9 curl -fLsS https://api.imd.fun/artifacts/8a6e303b662e1a5ea9bb47f31acedd3988ef65b3ddb66d074517aa59ad41ca40 -o test/scratch/qt1-artifact.md rg -n 'Contract design|Notes and limits|Strategy.sol|sha256' test/scratch/qt1-artifact.md ls test/scratch/amm-sim/setup-fast.sh cat test/scratch/amm-sim/setup-fast.sh sed -n '45,65p;115,121p' test/scratch/qt1-artifact.md C: sh ../amm-sim/setup-fast.sh > ../setup.log 2>&1 sed -n '185,220p;323,345p' test/scratch/qt1-artifact.md tail -10 test/scratch/setup.log tail -20 test/scratch/setup.log python3 heredoc: extract the first Solidity fence after '## Strategy.sol', print length/SHA-256, write C/contracts/src/Qt1.sol and an identical Qt1Fast.sol sed -n '103,174p' C/contracts/src/Qt1.sol cat T/harness.py rg --files T | head -50 sed -n '1,160p' C/amm_competition/evm/compiler.py sed -n '1,120p' T/research/solved/gates.py C: E python3 -I ../amm-sim/research/solved/gates.py > scratch/gates.log 2>&1 tail -10 C/scratch/gates.log cat T/research/solved/README.md | head -80 head -40 C/contracts/src/Qt1.sol C: attempted heredoc writes to test/scratch/pack.py and test/scratch/amm-challenge/scratch/compile.py, plus python3 test/scratch/pack.py; failed because these root-relative paths were used from C cat > test/scratch/pack.py (heredoc: repack each coefficient row into one word and regenerate dispatch) python3 test/scratch/pack.py cat > C/scratch/compile.py (heredoc: compile Qt1 and Qt1Fast, print runtime sizes and save creation bytecodes) C: E python scratch/compile.py > scratch/compile.log 2>&1 cat C/scratch/compile.log rg -n 'pyrevm|EVM|message_call|afterSwap' C/amm_competition/evm/*.py | head -65 sed -n '1,140p' C/amm_competition/evm/strategy.py (file absent) sed -n '76,110p;174,230p' C/amm_competition/evm/executor.py sed -n '1,130p' T/research/solved/replica2.py ps -eo pid,etime,comm | rg 'solc|python' sed -n '345,390p' test/scratch/qt1-artifact.md sed -n '390,445p' test/scratch/qt1-artifact.md head -45 test/scratch/qt1-artifact.md curl -fLsS https://api.imd.fun/artifacts/17d249ab0c33fc44931d9b105cf3f9e3846a560bd518b67310c2a56defacdecc -o test/scratch/qm-spline.md rg -n 'base64|QMS1|```' test/scratch/qm-spline.md | tail -15 C: cat > scratch/check_f.py (heredoc: extract qmref.py/blob; run reference-driven replica traces through two independent pyrevm contracts and compare raw returns/reference fees) C: rg -n 'SELECTOR|75e0|afterInitialize' amm_competition/evm/executor.py | head -15 C: E python scratch/check_f.py > scratch/check_f.log 2>&1 (failed: incorrect init selector) tail -20 C/scratch/check_f.log rg -n 'def __init__|def init|def on_trade' C/scratch/qmref.py sed -n '93,145p' C/scratch/qmref.py sed -i 's/75e0c46e/837aef47/' C/scratch/check_f.py C: E python scratch/check_f.py > scratch/check_f.log 2>&1 (failed: only 1,874 callbacks on seed 3 at 2,000 steps) C: E python heredoc: print public EVM method names cat C/scratch/check_f.log C: cat > scratch/micro.py (heredoc: 200 deterministic synthetic callbacks, Random(1), compare raw returns and gas) C: E python scratch/micro.py cp C/contracts/src/Qt1Fast.sol C/scratch/row-packed.sol cat > test/scratch/tree.py (heredoc: 18-word groups, ternary inner dispatch) python3 test/scratch/tree.py C: E python heredoc: compile Qt1Fast, report runtime size, save scratch/Qt1Fast.bin cat > test/scratch/cache.py (heredoc: shared 306-word per-callback coefficient cache) python3 test/scratch/cache.py sed -i 's/n_steps=2000/n_steps=5000/' C/scratch/check_f.py C: E python heredoc: compile Qt1Cache, report runtime size, save scratch/Qt1Cache.bin C: E python scratch/micro.py C: cat > scratch/profile_dispatch.py (heredoc: override QMRef.lookup to count stripe word accesses without changing returned fees; replica seeds 0..4, 5,000 steps each) C: E python scratch/profile_dispatch.py > scratch/profile_dispatch.log 2>&1 C: sed "s/'Qt1Fast'/'Qt1Cache'/" scratch/micro.py > scratch/micro_cache.py C: E python scratch/micro_cache.py cat C/scratch/profile_dispatch.log ls -l C/scratch/word_counts.json git status --short cat > test/scratch/weighted.py (heredoc: exact weighted alphabetic binary trees within original 16-word groups and at group dispatch; count floor 100) python3 test/scratch/weighted.py C: E python heredoc: compile Qt1Weighted, report runtime size, save scratch/Qt1Weighted.bin C: cat > scratch/screen.py (heredoc: replica seed 5, 2,000 steps; Qt1/ternary/cache/weighted in independent EVMs, compare all raw returns and gas) C: E python scratch/screen.py > scratch/screen.log 2>&1 cat C/scratch/screen.log cat > test/scratch/table_dispatch.py (heredoc: table-selected internal function pointer, four subtrees per table) python3 test/scratch/table_dispatch.py C: E python heredoc: compile Qt1Table, report runtime size, save scratch/Qt1Table.bin cat > test/scratch/last_cache.py (heredoc: four-word rolling stripe cache on weighted dispatch) python3 test/scratch/last_cache.py C: E python heredoc: compile Qt1Last, report runtime size, save scratch/Qt1Last.bin C: sed "s/'Qt1Weighted'/'Qt1Weighted','Qt1Table'/; s/screen.json/screen_table.json/" scratch/screen.py > scratch/screen_table.py C: E python scratch/screen_table.py > scratch/screen_table.log 2>&1 rg -n -A45 'def _build_configs' C/amm_competition/competition/match.py cat C/scratch/screen_table.log git status --porcelain=v1 cat > test/scratch/global_tree.py (heredoc: globally optimal alphabetic tree, floor count 100, split into functions at <=16 leaves) python3 test/scratch/global_tree.py C: E python heredoc: compile Qt1Global, report runtime size, save scratch/Qt1Global.bin C: sed "s/'Qt1Weighted'/'Qt1Weighted','Qt1Last'/; s/screen.json/screen_last.json/" scratch/screen.py > scratch/screen_last.py C: E python scratch/screen_last.py > scratch/screen_last.log 2>&1 C: cat > scratch/engine.py (heredoc: pinned harness.run/paired with exact MatchRunner configuration body restricted to the reserved range; save all stats/edges) cat C/scratch/screen_last.log ls -l C/scratch/Qt1Global.bin C: sed "s/'Qt1Weighted'/'Qt1Weighted','Qt1Global'/; s/screen.json/screen_global.json/" scratch/screen.py > scratch/screen_global.py C: E python scratch/screen_global.py > scratch/screen_global.log 2>&1 cat C/scratch/screen_global.log C: cp contracts/src/Qt1Global.sol contracts/src/Qt1Fast.sol C: cp scratch/Qt1Global.bin scratch/Qt1Fast.bin C: cat > scratch/audit_words.py (heredoc: parse and evaluate both dispatchers for all 306 indices; assert identical source outside dispatch except added comment) C: E python scratch/audit_words.py C: E amm-match validate contracts/src/Qt1Fast.sol > scratch/validate.log 2>&1 C: E timeout 470 python scratch/check_f.py > scratch/check_f.log 2>&1 C: E timeout 470 python scratch/engine.py > scratch/engine.log 2>&1 mkdir -p artifacts/goal/batches/e1-gas-spline/items/gas-dispatch cat > test/scratch/make_sources.py (heredoc: exact quotes, fresh urllib URL reads, write sources.json) python3 test/scratch/make_sources.py ``` Read-only `cat`/`tail` inspections of `compile.log`, `check_f.log`, `engine.log`, `validate.log`, `screen*.log` and `ls -l C/scratch/engine*.json` were repeated while jobs ran. Process sessions were polled through the execution tool; no engine simulation was rerun. A web-tool open of Q failed internally; its complete contents were obtained by the successful curl above. The same web call opened the pinned raw `harness.py` successfully. Initial tool discovery searched available tools for web/browse metadata. Final reporting/check operations: Initial assembly was 65,443 bytes; a Python edit replaced redundant raw callback rows with aggregate error totals, then reran `write_result.py`. ```text python3 heredoc: summarize raw engine submission gas statistics and means cat > test/scratch/command_audit.md (command journal) cat > test/scratch/write_result.py (assemble this report from checked local measurements and complete final source) python3 test/scratch/write_result.py python3 heredoc: parse sources.json and the report JSON, enforce byte limits/required keys, assert exact code fence equality and source hash, engine/F passes, both gas caps and allowed output paths wc -c artifacts/goal/batches/e1-gas-spline/items/gas-dispatch/result.md artifacts/goal/batches/e1-gas-spline/items/gas-dispatch/sources.json git status --short --untracked-files=all ``` ## Problems - **Target not achieved:** the deliverable is a small lossless dispatch improvement, not the requested 40k headroom or a higher-scoring AMM entry. No board result is claimed. The 32-seed peak is sample-specific, not a bound over all possible callbacks; the slowest path can change with the decision tree. - Exploratory row packing increased synthetic peak gas from 143,497 to 159,514 (200 callbacks), despite avoiding crossing-word reads; rejected. On one 1,976-callback replica trace, qt1 peak/mean was 170,712 / 125,543.822368. Ternary dispatch: 170,337 / 125,481.202429; shared 306-word cache: 179,891 / 133,112.156377; weighted original groups: 169,628 / 125,093.240891; table-selected function pointers: 175,036 / 129,288.420547; rolling four-word cache: 192,580 / 143,746.503036; selected global tree: 169,441 / 125,011.828947. All returned identical fee bytes on that trace. Only the selected candidate ran on the reserved engine seeds. No savings from rejected variants are claimed. These results show that fewer fetches alone did not offset their additional compiled overhead in these implementations; they do not rule out a better cache design. - Two local Gate F scaffolding errors were fixed before the final successful run: an incorrect initialization selector caused a revert; then 2,000 replica steps did not yield 2,000 callbacks on seed 3. Correct selector `837aef47` and 5,000 steps resolved both. An initial row-packing heredoc used root-relative paths from the checkout root and failed; rerun from the repository root. These were test-scaffolding errors, not setup failures or final strategy errors. - The web tool could not open Q; curl and fresh urllib reads succeeded. All eight quoted source passages were checked against fresh URL responses. There was no setup failure, account/quota failure, missing requester input, or blocked step.