Skip to content

Feature/stream reader improvements - #763

Draft
Hirogen wants to merge 13 commits into
Developmentfrom
feature/stream-reader-improvements
Draft

Hirogen wants to merge 13 commits into
Developmentfrom
feature/stream-reader-improvements

Conversation

@Hirogen

@Hirogen Hirogen commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Fixes #758. Fixes #762.

Memory and correctness fixes for the Direct stream reader and LogfileReader, collected on feature/stream-reader-improvements.

Changes

XML-mode logs load again (dac321e)

In XML mode GetLogStreamReader returns an XmlBlockSplitter, which only implements ILogStreamReader. ReadToBufferList and ReReadBuffer cast it to ILogStreamReaderMemory, got null and threw on reader.Position. An XML log loaded no lines, and the splitter was never disposed.

Now:

  • The reader is kept as ILogStreamReader.
  • Memory readers are read through TryReadLine, string readers through ReadLine.
  • Char blocks are attached only for memory readers.

Tail mode no longer pins a full char block per poll (672dc6b, 39d6c16)

The reader rented a 32 768-char block in its constructor. The block was handed to the LogBuffer as soon as one char was used. Tail mode creates a reader on every poll, so a log that grows by one line per poll pinned 64 KB per line, up to about 32 MB for a 500-line buffer.

The block is now rented lazily. After a short read, the rest of the file is already buffered, so the block is sized to those bytes plus one, capped at the old size. Stream.Length is not queried, which matters on SFTP streams. A LogfileReader test checks that ten one-line polls leave blocks sized to the text; before the fix they held 360 448 chars.

Memory per line bounded by the maximum line length (86446fc, #761 / 794f20a), #758

A line was buffered and decoded whole, then cut to MaxLineLength. Memory grew with the longest line: about 1 GB for a 300 MB run without a newline. The oversized char block stayed with the LogBuffer. Above about 1 GB, the doubling byte buffer overflowed and the read threw.

Now:

  • Once a line holds 4 × MaxLineLength + 4 bytes, only that prefix is decoded.
  • The rest of the line is scanned for the terminator and counted in Position, so the position stays exact.
  • Text that may exceed the cap is decoded into a pooled scratch buffer, and only the capped text goes into the char block. Blocks handed to the LogBuffer stay at BLOCK_SIZE, or what the cap needs.
  • The ASCII and UTF-16 LE fast paths convert only up to the cap.

Measured on a 50 MB run with MaxLineLength = 20 000:

  • Before: about 255 MB allocated, and a 128 MB char block kept by the LogBuffer.
  • After: under 0.9 MB allocated, with no block over 32 768 chars.

Each reader keeps its own maximum line length (38a4db3), #762

PositionAwareStreamReaderBase.MaximumLineLength was static. Every reader's constructor overwrote the cap for all readers, so a reader truncated lines at the cap of whichever reader was created last. It is now an instance property. This file also loses its UTF-8 BOM, because src/.editorconfig requires charset = utf-8 and the CI format check flags changed files.

LogLine stores its text once (a4cf0b0)

Both constructors set Text to the same memory as FullLine, so every resident line carried a second ReadOnlyMemory<char>. Text is now a computed property returning FullLine: 24 bytes per line instead of 40. The public surface and record equality are unchanged.

Cleanup (21e7b2d)

Drops a misspelled reader alias in ReReadBuffer.

Tests

  • New DirectReaderLongLineMemoryTests covers:
    • allocation and block-size bounds for a 50 MB run in UTF-8 and UTF-16;
    • text and Position against the raw bytes;
    • cut points inside UTF-8, UTF-16 surrogate pairs and GB18030 4-byte sequences;
    • \r at refill boundaries;
    • random bytes in UTF-8, UTF-16 LE/BE, UTF-32, GB2312 and GB18030.
  • RawByteLineOracle takes an optional maximum line length.
  • New tests for XML mode, the tail block size, LogLine, and two readers with different caps.
  • Full suite: passes except SelectionPainterTests.PaintedBlock_HasNoInternalEdge_AndErasesOldEdgesAfterSelectionChanges, which is on the known-flaky list. InactiveWindow_KeepsItsOwnSearch was excluded, as CLAUDE.md says.

Benchmarks

DirectReaderBenchmarks for #761 against the branch before it showed no regression. Measured on a noisy machine. All results were within ±2.5 %, allocations were identical, and an 18 % Unicode outlier didn't reproduce in three alternating reruns. Details are in #761.

Not in this PR

BRUNER Patrick and others added 13 commits November 20, 2026 17:16
In XML mode GetLogStreamReader returns an XmlBlockSplitter, which only
implements ILogStreamReader. ReadToBufferList and ReReadBuffer cast it
to ILogStreamReaderMemory, got null and threw on reader.Position, so an
XML log loaded no lines and the splitter was never disposed.

Keep the reader as ILogStreamReader, read memory readers through
TryReadLine and string readers through ReadLine (a null line after
preprocessing counts as dropped, as before the memory path), and attach
char blocks only for memory readers.
The reader rented a 32,768-char block in its constructor and handed it
to the LogBuffer as soon as one char was used. Tail mode creates a new
reader on every poll, so a log that grows by one line per poll pinned
64 KB per line, up to about 32 MB for a 500-line buffer.

Rent the block lazily. After a short read the rest of the file is
already buffered, so the block is sized to those bytes plus one (no
encoding decodes n bytes to more than n + 1 chars), capped at the old
block size. Full reads still get full blocks, and Stream.Length is not
queried, which would be a round trip on SFTP streams.
A line was buffered and decoded whole before being cut to
MaximumLineLength. Memory grew with the longest line: about 1 GB for a
300 MB run without a newline, the oversized char block stayed with the
LogBuffer, and above about 1 GB the doubling byte buffer overflowed and
the read threw.

Once a line holds more bytes than any supported encoding needs for
MaximumLineLength chars (4 per char, plus 4 so a cut character falls
beyond the cap), decode only that prefix and drop the searched bytes up
to the terminator. Position still counts every dropped byte, so it stays
exact, and the byte buffer stays at about twice the prefix.
Both constructors set Text to the same memory as FullLine, so every
resident line carried a second ReadOnlyMemory<char>: 40 bytes instead
of 24 for every line of every loaded LogBuffer.

Text is now a computed property returning FullLine. The public surface
is unchanged (callers use the getter either way), and record equality
is unchanged because the two were always equal.
Ten polls that each read one appended line must leave the buffers with
char blocks sized to the text. Before the fix they held 360,448 chars,
one 32,768-char block for the initial load and one per poll.
…r-improvements

# Conflicts:
#	src/LogExpert.Core/Classes/Log/Streamreaders/PositionAwareStreamReaderDirect.cs
A line whose text may exceed MaximumLineLength, including the 4-bytes-per-char prefix of a truncated line, was decoded straight into the char block, so the block handed to the LogBuffer was sized for about 4x the cap (131,072 chars for a 20,000 cap in UTF-8). Such lines now decode into a pooled scratch buffer and only the capped text is copied into the block. The ASCII and UTF-16 LE fast paths convert only up to the cap.

Adds DirectReaderLongLineMemoryTests (50 MB run allocation and block-size bounds, cut points in UTF-8, UTF-16 and GB18030, CR at refill boundaries, random bytes) and a maximum line length for RawByteLineOracle.

Refs #758
Keep char blocks to the capped text on lines over the maximum
MaximumLineLength was static, so every reader's constructor overwrote the cap for all readers in the process: a reader truncated lines at the cap of whichever reader was created last. The Direct reader also sized its truncation prefix from the cap at its first read and cut the text at the cap current at decode time, so an over-long line could come out at neither length.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stream readers share one static maximum line length a binary blob without newlines grows the reader's buffers to the blob size

1 participant