Skip to content

Refill buffer before slicing in BinaryReadStream.ReadMemory when readSafe is false - #22

Open
Illustar0 wants to merge 1 commit into
Noggog:devfrom
Illustar0:fix/read-memory-load-position
Open

Illustar0 wants to merge 1 commit into
Noggog:devfrom
Illustar0:fix/read-memory-load-position

Conversation

@Illustar0

Copy link
Copy Markdown

Previously, the fast path in BinaryReadStream.ReadMemory required amount < InternalStreamRemaining. Whenever a read hit the initial unloaded state, matched InternalStreamRemaining, or crossed a buffer boundary, it fell back to allocating a new byte[amount] instead of refilling the internal buffer first.

Calling LoadPosition(amount) when !readSafe && amount <= _data.Length refills the buffer on boundary crossings and keeps readSafe: false reads on the zero-allocation slice path.

- Call LoadPosition(amount) when readSafe is false and amount fits within the internal buffer
- Avoid falling back to byte array allocation when the current buffer has fewer remaining bytes than requested
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.

1 participant