Refactor NetDataReader for safer, faster reads#593
Open
FUTURE-SL wants to merge 4 commits intoRevenantX:masterfrom
Open
Refactor NetDataReader for safer, faster reads#593FUTURE-SL wants to merge 4 commits intoRevenantX:masterfrom
FUTURE-SL wants to merge 4 commits intoRevenantX:masterfrom
Conversation
Rework NetDataReader to improve safety, performance and API clarity. Adds bounds-checked helpers (EnsureAvailable, ThrowNotEnoughData), validates SetSource/SetPosition inputs and changes SetSource to use an endOffset. Replaces BitConverter/Buffer.BlockCopy usages with Span/MemoryMarshal-based GetUnmanaged/PeekUnmanaged/TryGetUnmanaged helpers and uses ReadOnlySpan/ReadOnlyMemory for remaining bytes. Streamlines Get/Peek/TryGet methods (value-returning overloads, generic unmanaged array handling), improves string and IPEndPoint parsing, and introduces typed TryGet and Peek methods for common primitives.
Add System.Runtime.CompilerServices.Unsafe (v6.0.0) to the project and update NetDataReader.cs to use Unsafe.ReadUnaligned for unmanaged reads instead of MemoryMarshal.Read/TryRead. Also adjust TryGetUnmanaged to validate size and advance position correctly, and fix the generic Deserialize loop to create/deserialize the item before assigning it to the result array. Changes touch LiteNetLib.csproj and Utils/NetDataReader.cs.
Remove the System.Runtime.CompilerServices.Unsafe package reference from the csproj and replace uses of Unsafe.ReadUnaligned<T> in NetDataReader with a new private unsafe ReadUnmanaged<T>() method (uses fixed pointer dereference and AggressiveInlining). This preserves the previous unaligned reads while eliminating the external Unsafe dependency and keeping behavior unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rework NetDataReader to improve safety, performance and API clarity. Adds bounds-checked helpers (EnsureAvailable, ThrowNotEnoughData), validates SetSource/SetPosition inputs and changes SetSource to use an endOffset. Replaces BitConverter/Buffer.BlockCopy usages with Span/MemoryMarshal-based GetUnmanaged/PeekUnmanaged/TryGetUnmanaged helpers and uses ReadOnlySpan/ReadOnlyMemory for remaining bytes. Streamlines Get/Peek/TryGet methods (value-returning overloads, generic unmanaged array handling), improves string and IPEndPoint parsing, and introduces typed TryGet and Peek methods for common primitives.