"""Read archived profiling only; write a reproducible fallback cost inventory. No worker build, simulation, native code edit, or extrapolated speedup claim. Run from any directory with the repository's Python interpreter. """ from collections import defaultdict import hashlib import json from pathlib import Path import re import statistics ROOT = Path(__file__).resolve().parents[2] SOURCE = ROOT / "test/context-fallback-20260917" OUT = ROOT / "test/fallback-profitability-20260917" DOCS = ROOT / "tests/manual" J = 896 MANIFEST = {} def read(path): raw = path.read_bytes() MANIFEST[path.relative_to(ROOT).as_posix()] = hashlib.sha256(raw).hexdigest() return json.loads(raw) def close(a, b): assert abs(a - b) <= 1e-12 * max(1, abs(a), abs(b)), (a, b) def table(headers, rows): def cell(x): return str(x).replace("|", "\\|").replace("\n", " ") return "\n| " + " | ".join(headers) + " |\n| " + " | ".join(["---"] * len(headers)) + " |\n" + "\n".join("| " + " | ".join(map(cell, r)) + " |" for r in rows) + "\n" def labels(xs, prefix=""): return ",".join(prefix + str(x) for x in xs) def span_stats(xs): return dict(rounds=xs, mean=statistics.mean(xs), median=statistics.median(xs), min=min(xs), max=max(xs)) def main(): a = read(SOURCE / "analysis.json") plan = read(SOURCE / "plan.json") typed = read(ROOT / "test/r288-typed-replay-20260917/performance-summary.json") layered = read(ROOT / "test/local-probe-profile-direct-20260917/comparison.json") P = typed["metrics"]["C"]["stages"]["median"] H = typed["paired"]["C"]["metadataIncrementUs"]["median"] def budgets(c, m): # Strict inequality is required for a positive saving. Negative budgets # are retained: they mean no nonnegative implementation cost can fit. return dict(originalUs=c, structuralReusePerJacobian=m, captureMaxAtZeroProbeUs=m*c, captureMaxAtProbeUs={str(p): m*(c-p) for p in (0.1, 0.25, 0.5, 2, 5, 10, P)}, probeMaxAtTypedCaptureUs=c-H/m, typedZeroCapturePossible=c>P, typedWithSharedCapturePossible=c>P+H/m, typedScenarioNetMs=J*(m*(c-P)-H)/1000) native = {} for pos, lines in enumerate(plan["code"]): native[pos] = sorted(set(re.findall(r"\b(native_\w+)\s*\(", "\n".join(lines)))) for op in a["operations"]: pos = op["position"] assert bool(native[pos]) == (plan["versions"][pos] != plan["versions"][pos+1]) pairs = {(x["group"], x["region"]): x for x in a["groupRegions"] if x["failures"]} gos = {(x["group"], x["position"]): x for x in a["groupOperations"]} assert len(pairs) == 156 and len(gos) == len(a["groupOperations"]) == 5157 assert sum(x["failures"] for x in pairs.values()) == 139776 assert sum(x["count"] for x in gos.values()) == 4620672 assert all(x["failures"] == J for x in pairs.values()) assert all(x["inputDiffOps"] == x["outputDiffOps"] == x["exitOutputDiff"] == 0 and x["exitContextDiff"] == J for x in pairs.values()) region_runs, op_runs = [], [] for i in range(3): r = read(SOURCE / f"regions-{i}/context.json") o = read(SOURCE / f"ops-{i}/context.json") assert r["sampled"] == o["sampled"] == 112 region_runs.append({(v["group"], v["region"]): v["regionTicks"] * J/r["sampled"]/r["frequency"] for v in r["regions"]}) op_runs.append({(g, pos): ticks * J/o["sampled"]/o["frequency"] for g, pos, n, ticks in o["operations"]}) for key, x in pairs.items(): close(x["seconds"], statistics.mean(r[key] for r in region_runs)) for key, x in gos.items(): close(x["seconds"], statistics.mean(o[key] for o in op_runs)) def composition(ops): ns = sum(x["seconds"] for x in ops if native[x["position"]]) alg = sum(x["seconds"] for x in ops if not native[x["position"]]) return dict(nativeContainingOperationSeconds=ns, pureAlgebraAliasOperationSeconds=alg) operations = [] for x in sorted(a["operations"], key=lambda x: -x["seconds"]): pos = x["position"] members = [o for (g, p), o in gos.items() if p == pos] close(x["seconds"], sum(o["seconds"] for o in members)) assert x["count"] == len(x["groups"])*J calls = native[pos] kernel_family = any(c in calls for c in ("native_pipe_flow_cached_context", "native_medium_orifice_context")) operation = dict(**x, nativeCalls=calls, code=plan["code"][pos], nativeOperationCount=int(bool(calls)), operationCount=1, contextFreeAlgebraAlias=not calls, decision="A;停止R288后续优化" if pos == 16 else "A;E只测纯数值尾部" if kernel_family else "A" if calls else "A;C合并代数段", baselineSharing="显式输入/输出同值;跨组记录共享仅为结构上限,消费字段/分支未普遍验证", budget=budgets(x["seconds"]*1e6/x["count"], len(x["groups"])), roundSeconds=span_stats([sum(run[g, pos] for g in x["groups"]) for run in op_runs]), groupMeanUsRange=[min(o["seconds"]*1e6/o["count"] for o in members), max(o["seconds"]*1e6/o["count"] for o in members)], functionTimingScope="仅有 regionKernels 的区间函数归因,无本 position 专属函数计时", **composition(members)) operations.append(operation) regions, group_regions = [], [] for r in sorted((r for r in a["regions"] if r["failures"]), key=lambda r: -r["seconds"]): rid = r["id"] members = [x for x in gos.values() if x["region"] == rid] comp = composition(members) close(r["operationSeconds"], sum(comp.values())) native_positions = [p for p in range(r["start"], r["end"]) if native[p]] assert native_positions == r["nativeOperationPositions"] count = r["end"]-r["start"] kernels = sorted((dict(name=plan["kernels"][k["kernel"]][1], **k) for k in a["regionKernels"] if k["region"] == rid), key=lambda k: -k["exclusiveSeconds"]) m = len(r["failedGroups"]) op_us = r["operationSeconds"]*1e6/r["failures"] decision = "A" if count == 1 else "C/E预算筛选;native默认A" if rid in (3, 480, 485, 490): decision += ";D仅融合区间的待证假设" region = dict(**r, operationCount=count, nativeOperationCount=len(native_positions), nativeCalls=sorted(set(c for p in native_positions for c in native[p])), kernels=kernels, decision=decision, **comp, baselineSharing="同一 baseline 区间结构可供多个组引用;语义兼容未证" if m>1 else "仅一个失败组,无跨失败组摊销", budget=budgets(r["meanUs"], m), operationModeBudget=budgets(op_us, m), bothModesTypedWithSharedCapturePossible=min(r["meanUs"], op_us)>P+H/m, roundMeanUs=span_stats([sum(run[g, rid] for g in r["failedGroups"])*1e6/r["failures"] for run in region_runs])) regions.append(region) for g in r["failedGroups"]: x = pairs[g, rid] group_regions.append(dict(**x, operationCount=count, nativeOperationCount=len(native_positions), nativeCalls=region["nativeCalls"], functionTimingScope=f"R{rid}汇总,无group级函数拆分", decision=decision, **composition([v for v in members if v["group"] == g]), budget=budgets(x["seconds"]*1e6/J, m), # The net over J*m applies to a homogeneous-cost # scenario, not the measured total for this group. sharedBudgetNote="m为整个interval共享上限;本group成本的预算是假设同成本的情景,不是全interval实测净收益", roundMeanUs=span_stats([run[g, rid]*1e6/J for run in region_runs]))) assert len(regions) == 115 and len(operations) == 343 close(sum(r["seconds"] for r in regions), a["totals"]["regionSeconds"]) close(sum(o["seconds"] for o in operations), a["totals"]["operationSeconds"]) close(sum(r["pureAlgebraAliasOperationSeconds"] for r in regions), sum(o["pureAlgebraAliasOperationSeconds"] for o in operations)) cohorts = [] for p in (2, 5, P, 10): for capture in (0, H): eligible = [r for r in regions if r["meanUs"] > p+capture/r["budget"]["structuralReusePerJacobian"]] cohorts.append(dict(probeCostUs=p, captureUs=capture, regionIds=[r["id"] for r in eligible], count=len(eligible), originalSeconds=sum(r["seconds"] for r in eligible), originalShare=sum(r["seconds"] for r in eligible)/a["totals"]["regionSeconds"], hypotheticalNetSeconds=sum(r["seconds"]-r["failures"]*p/1e6-J*capture/1e6 for r in eligible), assumption="每个完整interval只付一次固定P,baseline捕获H跨所有失败组共享,100%成功;不等于逐operation replay,也不是预测")) total_comp = composition(list(gos.values())) pure_spans = [] for r in regions: pos = r["start"] while pos < r["end"]: if native[pos]: pos += 1 continue start = pos while pos < r["end"] and not native[pos]: pos += 1 members = [gos[g, p] for g in r["failedGroups"] for p in range(start, pos)] seconds = sum(x["seconds"] for x in members) pure_spans.append(dict(region=r["id"], groups=r["failedGroups"], start=start, end=pos, operationCount=pos-start, count=r["failures"], seconds=seconds, meanUs=seconds*1e6/r["failures"])) close(sum(s["seconds"] for s in pure_spans), total_comp["pureAlgebraAliasOperationSeconds"]) pure_spans.sort(key=lambda s: -s["seconds"]) kernel_cohort = [o for o in operations if any(c in o["nativeCalls"] for c in ("native_pipe_flow_cached_context", "native_medium_orifice_context"))] summary = dict(**a["totals"], **total_comp, intervals=len(regions), groupIntervals=len(pairs), operations=len(operations), groupOperations=len(gos), maxOperationMeanUs=max(o["budget"]["originalUs"] for o in operations), maxGroupOperationMeanUs=max(o["seconds"]*1e6/o["count"] for o in gos.values()), pureSpanCount=len(pure_spans), pureSpansAbove2UsSeconds=sum(s["seconds"] for s in pure_spans if s["meanUs"]>2), kernelContainingPositions=[o["position"] for o in kernel_cohort], kernelContainingOriginalSeconds=sum(o["seconds"] for o in kernel_cohort), layeredFallbackSeconds=layered["main"]["totals"]["correctedSeconds"]["schedule_context_fallback"]*J/layered["main"]["samples"], wholeContextSuccesses=a["totals"]["compares"]-a["totals"]["failures"]) group_ops = [] for x in a["groupOperations"]: op = next(o for o in operations if o["position"] == x["position"]) group_ops.append(dict(**x, nativeCalls=op["nativeCalls"], decision=op["decision"], meanUs=x["seconds"]*1e6/x["count"], structuralReusePerJacobian=op["budget"]["structuralReusePerJacobian"])) out = dict(scope="archived 0–10 s; diagnostics only", summary=summary, referenceCosts=dict(typedProbeUs=P, typedCaptureIncrementUs=H, metrics=typed["metrics"], paired=typed["paired"]), assumptions=dict(sharing="structural upper bound, not established semantic reuse", successRate=1, originalCost="sampled instrumented mean; not an uninstrumented lower bound", budgets="strictly less for profit; no incremental failed-guard/fallback-dispatch cost assumed"), regions=regions, groupRegions=group_regions, operations=operations, groupOperations=group_ops, pureSpans=pure_spans, groups=a["groups"], kernels=a["kernels"], scenarios=cohorts, successfulContextRegions=[r for r in a["regions"] if r["contextual"] and not r["failures"]], inputSha256=MANIFEST, checks=["139776 interval failures", "4620672 fallback operation executions", "no duplicate (group,position)", "156 failing group/interval pairs; 115 intervals; 343 positions; 5157 group/position pairs", "raw three-run timing reconstruction matches archived analysis", "native-call and context-version classification agree", "region, group and operation partitions conserve totals", "all pure spans conserve algebra time"]) OUT.mkdir(parents=True, exist_ok=True) (OUT / "analysis.json").write_text(json.dumps(out, ensure_ascii=False, indent=2)+"\n", encoding="utf-8") preamble = "# 全部 fallback 区间及预算\n\n由 `analyze_fallback_profitability.py` 从历史数据生成。ID、group、position 为0基,范围为[start,end)。ms为完整896个Jacobian折算累计;µs为每次fallback。按累计原计算时间排序。\n\nP=probe总开销,H=每个Jacobian新增baseline捕获,m=结构上最多共享的失败组数。盈利要求 P+H/m