Files
clean-architecture-frontend…/tests/unit/realtime
DongHyeonkaandClaude Opus 5.5 aac7a32862 perf(realtime): split SSE lines on bytes and decode each line once (R3-5)
The parser now finds CR/LF with Uint8Array.indexOf before decoding, so a
line's UTF-8 byte count comes from byte offsets instead of re-encoding the
text. Each complete line is decoded once by a streaming fatal TextDecoder;
a line that continues into the next read is stream-decoded, and the bytes
of a character split across reads count once it completes, as before.

Behavior kept (the push() input was already Uint8Array; no seam change in
fetch-sse-connection.ts, public API unchanged):
- CR / LF / CRLF, CR and LF split across reads, multi-line data
- one leading BOM stripped (now done by the parser, decoder uses
  ignoreBOM: true, so a BOM at a later line start stays text)
- line / event / incomplete-buffer byte limits, same failure point
- drain() and the per-call item cap; kept bytes are copied
- a pending CR or the item cap waits while only part of a character is
  left, as the text-based parser did
- error codes (MALFORMED_EVENT / EVENT_TOO_LARGE / QUEUE_OVERFLOW / CLOSED)

Differences only on invalid UTF-8: the old parser decoded a whole chunk
first, so malformed bytes won over another error later in that chunk and
malformed bytes past the item cap failed in push(). Now the first
problem in stream order wins and those bytes fail in drain(). Both
still fail closed.

Fixed: Node's streaming TextDecoder (ignoreBOM: false) drops a U+FEFF
right after the stream's first character when that character is split
across reads, e.g. [EF] [BB BF EF BB BF] decoded to "" instead of
"". The old parser inherited that; the new one keeps the text.

Tests: existing parser and connection tests unchanged and passing. Added
a split-BOM case and a differential test against the round-2 parser
(tests/unit/realtime/sse-parser-reference.ts) over 2x1,500 seeded random
ASCII / multibyte streams with random read splits, random small limits,
truncated streams and skipped drains. 300,000 seeds also matched in a
manual run.

Benchmark (manual, not committed; Node v24.14.0, one push of one chunk,
best of 150 runs, host load average ~11-14 so absolute ms are noisy):

| input (one chunk)              | original e59994a | round 2 | new     | new vs original | new vs round 2 |
|--------------------------------|------------------|---------|---------|-----------------|----------------|
| ASCII, 249 KB, 209 events      | 103.72 ms        | 0.48 ms | 0.30 ms | 348x            | 1.6x           |
| Korean/emoji, 249 KB, 209 ev.  | 23.30 ms         | 0.85 ms | 0.44 ms | 53x             | 1.9x           |
| ASCII, 234 KB, 4 x 60 KB lines | 176.45 ms        | 0.61 ms | 0.34 ms | 513x            | 1.8x           |
| Korean/emoji, 234 KB, 4 x 60KB | 25.28 ms         | 1.10 ms | 0.65 ms | 39x             | 1.7x           |

Across five runs, multibyte new vs original stayed between 19x and 70x
(target: 10x or more).

Realtime bundle: 39903 / 40000 gzip bytes (baseline 39685, +218).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 15:31:29 +09:00
..
2026-08-01 19:39:59 +09:00