Skip to content

Refactor TyphonEvent emission: header struct + source generator + codec consolidation #294

Closed

Description

@nockawa

Goal

Eliminate ~7,800 LOC of mechanical boilerplate across ~90 typed-event ref structs in src/Typhon.Engine/Profiler/Events/ and src/Typhon.Engine/Profiler/TyphonEvent.cs (currently 5,214 LOC).

The duplication exists in four buckets: (a) struct field declarations, (b) Begin* factory bodies, (c) per-kind codec encode/size methods, (d) call sites. (d) has the right shape (using var scope = BeginX(...)) and is out of scope. (a)–(c) are mechanical and regular — well-suited to a header struct + Roslyn source generator.

Scope (three layered phases)

Phase 1 — TraceSpanHeader struct

Introduce a single TraceSpanHeader struct holding the seven prologue fields (ThreadSlot, StartTimestamp, SpanId, ParentSpanId, PreviousSpanId, TraceIdHi, TraceIdLo) and embed it as the first field in every event ref struct. Update Begin* factories and Dispose accordingly. Mechanical, no behavior change, no wire-format change. ~1,200 LOC saved.

Phase 2 — Roslyn source generator

Add a generator driven by a declarative kind list (or [TraceEvent] attribute on partial structs). Generator emits:

  • The ref struct fields (header + payload).
  • The BeginXxx / EmitXxx methods in TyphonEvent.
  • The ITraceEventEncoder.Kind / ComputeSize / EncodeTo / Dispose plumbing.

Replaces ~6,000 LOC of hand-written boilerplate with ~500 LOC of declarations + a generator template.

Phase 3 — Codec consolidation

Consolidate the per-kind *EventCodec.Encode / ComputeSize methods into the generator, with a hand-written escape hatch for kinds with weird wire shapes (e.g. TransactionPersist's walLsn-in-componentTypeId-slot, optional-mask kinds with non-trivial encoding).

Constraints

  • MUST preserve zero-cost-when-off semantics. The if (!TelemetryConfig.ProfilerActive) return default; gate must still let the JIT dead-code-eliminate the entire factory body when the profiler is off.
  • MUST preserve using var scope = BeginX(...) ergonomics at call sites. No call-site changes — the ref-struct + RAII shape is the correct abstraction for spans and is not the source of duplication.
  • MUST preserve wire format byte-for-byte. Consumers (exporters, viewer, profiler library) depend on the exact layout. Phase 1 is wire-neutral by construction; Phase 3 needs careful golden-file verification.
  • All existing tests must continue to pass. Add round-trip tests if not already present for kinds touched in Phase 3.

Out of scope

  • Call-site changes (using var scope = ... stays).
  • Adding new event kinds.
  • Changing the TraceEventKind enum or wire-format byte layout.

Related Documents

Activity

  1. self-assigned this
    on May 1, 2026
  2. added
    performance
    refactoring
    technical-debt
    on May 1, 2026
  3. moved this to Ready in Typhon devon May 1, 2026
  4. moved this from Ready to In Progress in Typhon devon May 1, 2026
  5. moved this from In Progress to Done in Typhon devon May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

performancerefactoringtechnical-debt

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions