|
| 1 | +import Base |
| 2 | +import ../../src/app/bots.bend as B |
| 3 | +import ../../src/app/cats.bend as C |
| 4 | +import ../../src/app/nui.bend as N |
| 5 | +import ../../src/gfx/draw.bend as D |
| 6 | +import ../../src/gfx/raster.bend as R |
| 7 | +import ../../src/gfx/text.bend as X |
| 8 | +import ../../src/server/perf.bend as P |
| 9 | + |
| 10 | +# Same-binary ABBA: original cold sleep24 requests versus retained visible cache. |
| 11 | +# Synthetic component attribution, not a Mailroom or application benchmark. |
| 12 | +# No hub, provider, window, private log, or file writes. Cached and uncached |
| 13 | +# front preparation are timed separately over identical visible geometry. |
| 14 | + |
| 15 | +def clip() -> D.Clip: |
| 16 | + D.Clip{0, 0, 1400, 900} |
| 17 | + |
| 18 | +def y(+i: Nat) -> U32: |
| 19 | + (70 + U32.from_nat(Nat.mul(i, 36n)) : U32) |
| 20 | + |
| 21 | +def spots(left: Nat, +i: Nat) -> List<&2, B.Spot>: |
| 22 | + match left: |
| 23 | + case 0n: Nil{} |
| 24 | + case 1n+k: |
| 25 | + B.Spot{Nat.add(12345n, i), "sleep", 24, 20, y(i), clip()} <> spots(k, 1n+i) |
| 26 | + |
| 27 | +def anchors(ss: List<&2, B.Spot>) -> List<&2, R.Op>: |
| 28 | + match ss: |
| 29 | + case Nil{}: Nil{} |
| 30 | + case B.Spot{_, _, px, x, y, c} <> t: |
| 31 | + B.Spot.anchor(c, x, y, px) <> anchors(t) |
| 32 | + |
| 33 | +def hits(ss: List<&2, B.Spot>) -> List<&2, N.Hit>: |
| 34 | + match ss: |
| 35 | + case Nil{}: Nil{} |
| 36 | + case B.Spot{lk, md, px, x, y, c} <> t: |
| 37 | + B.Spot.hit(lk, md, c, x, y, px) <> hits(t) |
| 38 | + |
| 39 | +def rectangles(left: Nat, +i: Nat) -> List<&2, R.Op>: |
| 40 | + match left: |
| 41 | + case 0n: Nil{} |
| 42 | + case 1n+k: |
| 43 | + D.Draw.rect(clip(), (400 + U32.from_nat(Nat.mod(i, 500n)) : U32), (100 + U32.from_nat(Nat.mod(i, 600n)) : U32), 2, 2, 100, 255) <> rectangles(k, 1n+i) |
| 44 | + |
| 45 | +def warm(ss: List<&2, B.Spot>, c: Map<&2, C.CatF>) -> Map<&2, C.CatF>: |
| 46 | + B.Spot.warm(ss, c, 0n) |
| 47 | + |
| 48 | +type Fixture is Data: |
| 49 | + Fixture{ops: List<&2, R.Op>, hits: List<&2, N.Hit>, spots: List<&2, B.Spot>, caches: List<&2, Map<&2, C.CatF>>} |
| 50 | + |
| 51 | +def cache(xs: List<&2, Map<&2, C.CatF>>) -> Map<&2, C.CatF>: |
| 52 | + match xs: |
| 53 | + case c <> _: c |
| 54 | + case Nil{}: C.Cats.none() |
| 55 | + |
| 56 | +def cover(on: Bool, ops: List<&2, R.Op>) -> List<&2, R.Op>: |
| 57 | + match on: |
| 58 | + case False{}: ops |
| 59 | + case True{}: D.Draw.rect(clip(), 0, 0, 80, 900, 200, 255) <> ops |
| 60 | + |
| 61 | +def fixture(+n: Nat, +covered: Bool) -> Fixture: |
| 62 | + +ss : List<&2, B.Spot> = spots(n, 0n) |
| 63 | + Fixture{cover(covered, List.append(&2, R.Op, anchors(ss), rectangles(400n, 0n))), hits(ss), ss, [warm(ss, C.Cats.none())]} |
| 64 | + |
| 65 | +def force_edges(es: List<&2, R.Edge>, acc: U32) -> Nat: |
| 66 | + match es: |
| 67 | + case Nil{}: U32.to_nat(acc) |
| 68 | + case R.Edge{ax, ay, bx, by} <> t: |
| 69 | + force_edges(t, (acc + F32.bits(ax) + F32.bits(ay) + F32.bits(bx) + F32.bits(by) : U32)) |
| 70 | + |
| 71 | +# Force the complete coverage bitmap as well as vector coordinates. |
| 72 | +def force_mask(m: R.Mask) -> U32: |
| 73 | + match m: |
| 74 | + case R.MWord{w}: w |
| 75 | + case R.MPair{a, b}: (force_mask(a) + force_mask(b) : U32) |
| 76 | + case R.M16{a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p}: |
| 77 | + (a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p : U32) |
| 78 | + |
| 79 | +def force_shape(s: R.Shape) -> Nat: |
| 80 | + match s: |
| 81 | + case R.SPoly{_, es}: force_edges(es, 0) |
| 82 | + case R.SMask{_, _, _, _, m}: U32.to_nat(force_mask(m)) |
| 83 | + case R.SSeg{ax, ay, bx, by, hw}: U32.to_nat((F32.bits(ax) + F32.bits(ay) + F32.bits(bx) + F32.bits(by) + F32.bits(hw) : U32)) |
| 84 | + case R.SDisc{cx, cy, r}: U32.to_nat((F32.bits(cx) + F32.bits(cy) + F32.bits(r) : U32)) |
| 85 | + case R.STri{ax, ay, bx, by, cx, cy}: U32.to_nat((F32.bits(ax) + F32.bits(ay) + F32.bits(bx) + F32.bits(by) + F32.bits(cx) + F32.bits(cy) : U32)) |
| 86 | + case _: 1n |
| 87 | + |
| 88 | +def force(ops: List<&2, R.Op>, +acc: Nat) -> Nat: |
| 89 | + match ops: |
| 90 | + case Nil{}: acc |
| 91 | + case R.Op{x0, y0, x1, y1, _, _, s} <> t: |
| 92 | + force(t, Nat.add(acc, Nat.add(force_shape(s), U32.to_nat((x0 + y0 + x1 + y1 : U32))))) |
| 93 | + |
| 94 | +def force_boxes(bs: List<&2, B.Box>, +acc: Nat) -> Nat: |
| 95 | + match bs: |
| 96 | + case Nil{}: acc |
| 97 | + case B.Box{k, x0, y0, x1, y1} <> t: |
| 98 | + force_boxes(t, Nat.add(acc, U32.to_nat((k + x0 + y0 + x1 + y1 : U32)))) |
| 99 | + |
| 100 | +def force_front(fr: B.Fronted) -> Nat: |
| 101 | + match fr: |
| 102 | + case B.Fronted{os, _, _}: force(os, 0n) |
| 103 | + |
| 104 | +# Each result, including polygon coordinates, is consumed inside the timed |
| 105 | +# region. Fixture/cache preparation stays outside it. No UI records cross IO. |
| 106 | +def evaluate(kind: Nat, f: Fixture) -> Nat: |
| 107 | + match kind f: |
| 108 | + case 0n Fixture{os, _, _, _}: |
| 109 | + List.length(&2, X.Want, X.Text.wants(os, Nil{})) |
| 110 | + case 1n Fixture{_, _, ss, cs}: |
| 111 | + force_boxes(B.Box.all(ss, cache(cs), 0n), 0n) |
| 112 | + case 2n Fixture{os, _, ss, cs}: |
| 113 | + Bool.pick(Nat, B.Spot.covered(os, B.Box.all(ss, cache(cs), 0n)), 1n, 0n) |
| 114 | + case 3n Fixture{_, _, ss, cs}: |
| 115 | + force(B.Spot.all.ops(ss, cache(cs), 0n), 0n) |
| 116 | + case 4n Fixture{os, _, ss, cs}: |
| 117 | + force(B.Spot.inline(os, ss, cache(cs), 0n), 0n) |
| 118 | + case 5n Fixture{os, +hs, _, cs}: |
| 119 | + force_front(B.Fronted.of(os, hs, B.Spot.warm(B.Spot.all(hs), cache(cs), 0n), 0n)) |
| 120 | + case _ Fixture{os, hs, _, _}: |
| 121 | + force_front(B.Fronted.of(os, hs, C.Cats.none(), 0n)) |
| 122 | + |
| 123 | +def name(kind: Nat) -> String: |
| 124 | + match kind: |
| 125 | + case 0n: "wants" |
| 126 | + case 1n: "boxes.warm" |
| 127 | + case 2n: "covered.warm" |
| 128 | + case 3n: "cats.ops.warm" |
| 129 | + case 4n: "inline.warm" |
| 130 | + case 5n: "front.warm" |
| 131 | + case _: "front.cold" |
| 132 | + |
| 133 | +def one(+kind: Nat, +fs: List<&2, Fixture>) -> IO(Nat): |
| 134 | + match fs: |
| 135 | + case Nil{}: IO.pure(Nat, 0n) |
| 136 | + case +f <> _: |
| 137 | + do IO<Nat>: |
| 138 | + +t0 : U32 <- P.Perf.us() |
| 139 | + P.Perf.add("probe.checksum", U32.from_nat(Nat.mod(evaluate(kind, f), 1000000n))) |
| 140 | + +t1 : U32 <- P.Perf.us() |
| 141 | + return U32.to_nat((t1 - t0 : U32)) |
| 142 | + |
| 143 | +def total(xs: List<&2, Nat>, acc: Nat) -> Nat: |
| 144 | + match xs: |
| 145 | + case Nil{}: acc |
| 146 | + case x <> t: total(t, Nat.add(acc, x)) |
| 147 | + |
| 148 | +def at(xs: List<&2, Nat>, i: Nat) -> Nat: |
| 149 | + Maybe.default(&2, Nat, List.get(&2, Nat, xs, i), 0n) |
| 150 | + |
| 151 | +def report(kind: Nat, +xs: List<&2, Nat>) -> IO(Unit): |
| 152 | + +sorted : List<&2, Nat> = List.sort(~Nat, ~Nat.is_lt, xs) |
| 153 | + +n : Nat = List.length(&2, Nat, xs) |
| 154 | + IO.print(name(kind) ++ " n=" ++ Nat.show(n) ++ " total_us=" ++ Nat.show(total(xs, 0n)) ++ " p50_us=" ++ Nat.show(at(sorted, Nat.div(n, 2n))) ++ " max_us=" ++ Nat.show(at(sorted, Nat.sub(n, 1n)))) |
| 155 | + |
| 156 | +def reps(left: Nat, +kind: Nat, +fs: List<&2, Fixture>, times: List<&2, Nat>) -> IO(Unit): |
| 157 | + match left: |
| 158 | + case 0n: report(kind, times) |
| 159 | + case 1n+k: IO.bind(Nat, Unit, one(kind, fs), us => reps(k, kind, fs, us <> times)) |
| 160 | + |
| 161 | +def stages(ks: List<&2, Nat>, +fs: List<&2, Fixture>) -> IO(Unit): |
| 162 | + match ks: |
| 163 | + case Nil{}: IO.pure(Unit, Unit{}) |
| 164 | + case +k <> t: |
| 165 | + do IO<Unit>: |
| 166 | + reps(12n, k, fs, Nil{}) |
| 167 | + stages(t, fs) |
| 168 | + |
| 169 | +def front_count(fr: B.Fronted) -> Nat: |
| 170 | + match fr: |
| 171 | + case B.Fronted{_, ss, _}: List.length(&2, B.Spot, ss) |
| 172 | + |
| 173 | +def ready(+n: Nat, covered: Bool, +f: Fixture) -> Bool: |
| 174 | + match covered f: |
| 175 | + case +on Fixture{+os, +hs, _, +cs}: |
| 176 | + Bool.and(Nat.is_eq(front_count(B.Fronted.of(os, hs, cache(cs), 0n)), Bool.pick(Nat, on, 0n, n)), Nat.is_gt(List.length(&2, R.Op, os), 400n)) |
| 177 | + |
| 178 | +def scenario_run(ok: Bool, +fs: List<&2, Fixture>) -> IO(Unit): |
| 179 | + match ok: |
| 180 | + case False{}: IO.print("FAIL scenario geometry/coverage") |
| 181 | + case True{}: |
| 182 | + do IO<Unit>: |
| 183 | + IO.print("PASS scenario geometry/coverage") |
| 184 | + stages([6n, 5n, 5n, 6n], fs) |
| 185 | + |
| 186 | +def scenario(+n: Nat, +covered: Bool) -> IO(Unit): |
| 187 | + +f : Fixture = fixture(n, covered) |
| 188 | + do IO<Unit>: |
| 189 | + IO.print("cats=" ++ Nat.show(n) ++ " covered=" ++ Bool.show(covered)) |
| 190 | + scenario_run(ready(n, covered, f), [f]) |
| 191 | + |
| 192 | +def main() -> IO(Unit): |
| 193 | + do IO<Unit>: |
| 194 | + scenario(1n, False{}) |
| 195 | + scenario(11n, False{}) |
| 196 | + scenario(11n, True{}) |
0 commit comments