Skip to content

FNV, Hex and byte-wise obfuscation stream paths read one byte at a time: ~11x slower and 32 bytes allocated per input byte on plain streams #91

Closed

Description

@matt-edmondson

What's wrong

Several synchronous stream paths process input through Stream.ReadByte() (and WriteByte() where they write output) in a per-byte loop:

  • FNV hashing, TryHash(Stream, …):
    • Essentials.HashProviders.FNV1_32/FNV1_32HashProvider.cs (~line 89)
    • FNV1a_32 (~line 89)
    • FNV1_64 (~line 93)
    • FNV1a_64 (~line 93)
  • Hex encoding, sync TryEncode/TryDecode over streams: Essentials.EncodingProviders.Hex/HexEncodingProvider.cs (~lines 59-62 and 114-128).
  • Obfuscation, the stream paths of the Xor, Caesar and BitRotate obfuscators.

The base Stream.ReadByte() allocates a fresh byte[1] and calls Read on every call. Only some stream types override it (FileStream and MemoryStream do; many wrappers and custom streams do not).

Measured impact

Hashing 64 MB with FNV-1a-64 (net10.0, Release):

Source stream Current ReadByte loop 80 KB chunked reads
FileStream 270 ms 113 ms
Stream that does not override ReadByte 1187 ms, 2048 MB allocated 108 ms

FNV is usually chosen because it is fast. On a wrapped stream (network, compression, crypto or a custom adapter) it is roughly 11x slower than it needs to be and creates gigabytes of GC pressure. The async paths and the other providers already read in chunks.

Suggested fix

  • FNV: read into a pooled 81920-byte buffer and fold each chunk with the same logic the provider's private Incremental.Append already uses. Alternatively, drive CreateIncremental() the way IHashProvider.TryHashAsync does.
  • Hex and the obfuscators: transform a chunk at a time and write the transformed chunk, the way their async paths already do.

Acceptance criteria

  • No per-byte ReadByte/WriteByte loops remain in these providers' stream paths.
  • The outputs are byte-identical to today's. Add tests that compare the stream output with the span output for each affected provider, including inputs larger than one buffer and an empty stream.

Activity

  1. added
    enhancement
    on Oct 6, 2026
  2. matt-edmondson commented on Oct 6, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

  3. added a commit that references this issue on Oct 26, 2026
    517b281
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions