Skip to content

The SSE reader's per-byte unit counting: an ASCII fast path #676

Closed

Description

@Blind-Striker

Part of #661.

Lane: Runtime. Follows RT-E5 slice 2 (#484), the byte-level SSE reader.

What. Slice 2 removed almost all of the reader's allocation:

  • per frame: 5,063 → 137 B for a large frame, and 177 → 8.6 B for a small one, on net10;
  • the prompt subscription: 2,210 → 1,442 B per frame.

The cost is time, in the reader-only benchmarks (ServerSentEventReaderBenchmarks, ShortRun):

Row net10 net472
ReadFramesAsync large-x64 40 → 87 µs (2.2×) 130 → 409 µs (3.1×)
ReadFramesAsync small-x1024 86 → 110 µs (1.3×) 246 → 474 µs (1.9×)

End to end it vanishes: the prompt subscription is 4,243 → 4,435 µs, inside the noise.

Where it comes from. ServerSentEventReader.AppendLine charges the frame ceiling (16 Mi UTF-16 units) one byte at a time, so it can charge each character when it completes. On top of that, each read is still decoded on arrival into a scratch buffer, as the old reader did, and the net472 decoder is slower.

The fix: an ASCII fast path in the counting. Skip ASCII runs in one step and charge them with one add. Fall back to the per-byte state machine only from the first byte ≥ 0x80. No behaviour change: the same unit, the same charging point, the same refusal.

  • net8+: IndexOfAnyExceptInRange((byte)0, (byte)0x7F) is vectorized.
  • net472 and netstandard2.0 have no vectorized IndexOfAnyExceptInRange. Whatever Polyfill offers, or a span loop, may be all that's achievable there. That's fine, as long as it's measured and reported.

Target:

  • the reader-only time back to today's, or better, on net10 and net11;
  • net472 improved as far as it goes, with the number stated;
  • allocation unchanged.

The evidence it needs:

  • the before/after on ServerSentEventReaderBenchmarks, plus the prompt, event and session-log rows;
  • the slice's characterization rows staying green, with the ceiling rows (é, €, 😀, the near-BOM rows, charging at completion) as its oracle;
  • a mutant for each branch of the fast path.

Activity

  1. added this to the 1.0 milestone on Oct 27, 2026
  2. added
    performance
    on Oct 27, 2026
  3. Blind-Striker commented on Oct 27, 2026

    @Blind-Striker
    CollaboratorAuthor

    Added to scope (the lead, 2026-10-27): pool the read buffer too.

    ServerSentEventReader still allocates new byte[ReadBufferBytes] (8 KiB) per enumeration. That's most of the
    remaining fixed cost: 8,608 B on net10 and 8,765 B on net472 for an empty or one-frame body. It matters for short-lived,
    one-shot streams such as session-log calls.

    • The change: rent it from ArrayPool<byte>.Shared, like the scratch buffer, and return it in the same finally.
    • The target: a fixed cost per enumeration near zero, with no behaviour change.

    Why pooling it is safe:

    • The read buffer never escapes. Each read's bytes are copied into the line buffer (AppendLine), and a frame's
      Data points into the data buffer, never into the read buffer.
    • The reader always awaits each ReadAsync to completion before it returns, throws or disposes. By the Stream
      contract, a completed read no longer touches its buffer.
      • ReadAsync_Should_Abandon_A_Read_That_Blocks_After_Cancellation uses a stream that honours the token, so the read
        completes, cancelled, before the finally runs.
    • The precedent: ResponseBufferingPolicy.CopyToPooledAsync already reads a response body into a pooled array.

    The one residual risk, stated for the PR: a stream that broke the contract, writing after its read completed, would
    now corrupt an array another pool user holds, instead of a private one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performance

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions