using AyCode.Core.Serializers;
using AyCode.Core.Serializers.Binaries;
using AyCode.Core.Tests.TestModels;
namespace AyCode.Core.Benchmarks;
///
/// Direct JIT-disassembly harness for the AcBinary Large Serialize hot path — the cell where
/// the PGO-driven inline bistability shows up (~120 µs/op fast mode ⇄ ~142 µs/op slow mode, same
/// source, run-to-run).
///
/// Not a BenchmarkDotNet benchmark. BDN's DisassemblyDiagnoser produced no output
/// ("No benchmarks were disassembled"); this harness leans on the runtime's own JIT disassembler
/// instead. builds the workload and exercises the Large Ser FastMode path — when the
/// process is launched with DOTNET_JitDisasm=<pattern> the JIT dumps the x64 assembly of
/// every matching method to stdout as it compiles them.
///
/// Run it (via the --jitasm switch). Set:
///
/// - DOTNET_TieredCompilation=0 — each method compiled once, straight to full-opt Tier-1:
/// deterministic codegen, no tiering/PGO lottery (so the disasm is reproducible).
/// - DOTNET_JitDisasm=<pattern> — e.g. *GeneratedWriter* for the SGen writer
/// hot loop. The un-inlined calls in that loop are the candidates for the PGO-flipped inline
/// site; pinning the right callee with [MethodImpl(AggressiveInlining)] locks in the fast mode.
///
///
/// The workload mirrors exactly — Large (5×5×5×10)
/// graph, AsciiShort charset, FastMode + Compact wire.
///
public sealed class JitDisassemblyBenchmark
{
///
/// Builds the Large workload and JITs + exercises the Large Ser FastMode hot path. With
/// DOTNET_JitDisasm set, the JIT emits the matching methods' disassembly to stdout on
/// first compile; the loop guarantees every reachable serializer method is JIT-compiled (and,
/// if tiering is left on, promoted to Tier-1).
///
public void Run()
{
// Mirror AcBinaryVsMemPackBenchmark exactly: AsciiShort charset (where the Large-Ser bimodality
// was observed), Large (5×5×5×10) TestOrder_All_False graph, FastMode + Compact wire.
BenchmarkTestDataProvider.LongStringSuffix = CharsetSuffixes.AsciiShort;
var allTestData = BenchmarkTestDataProvider_All_False.CreateTestDataSets();
var largeSet = (TestDataSet)allTestData.First(t => t.Name.StartsWith("Large"));
var order = largeSet.Order;
var options = AcBinarySerializerOptions.FastMode;
options.WireMode = WireMode.Compact;
Console.WriteLine("=== JIT-DISASM HARNESS: Large Ser FastMode (TestOrder_All_False, AsciiShort) — start ===");
byte[] last = null!;
for (var i = 0; i < 50; i++)
last = AcBinarySerializer.Serialize(order, options);
Console.WriteLine($"=== JIT-DISASM HARNESS: done — 50 Large Ser ops, last payload {last.Length} bytes ===");
}
}