Avoid initializing every session event type on first deserialization

Author: davkeanCreated Sep 26, 2026Updated Sep 28, 2026

SessionEvent.FromJson deserializes through the polymorphic base JsonTypeInfo. On the first event, System.Text.Json configures metadata for every derived session event type before returning the one concrete event that was received.

A cold-process console reproduction that deserializes one user.message event shows these medians across 15 processes:

Target Current Discriminator dispatch prototype
.NET Framework 4.7.2 5,466 ms 315 ms
.NET 8 512 ms / 5.09 MB 139 ms / 0.42 MB

The prototype reads the existing type discriminator and deserializes with only the matching source-generated concrete JsonTypeInfo. Unknown and missing discriminators continue to produce the base SessionEvent, preserving forward compatibility.

The expected behavior is for first-event deserialization to configure only the metadata required by the received event type, without changing known, unknown, union, or forward-compatible event semantics.