Skip to content

Conversation

paulomorgado
Copy link
Contributor

@paulomorgado paulomorgado commented May 21, 2025

This PR adds methods that use ReadOnlySpan<byte>/Span<byte> instead of byte[] and replaces byte[] enumeration and LINQ Skip and Take operations with span operations.

This allows callers to have complete ownership of the memory and use pooled memory.

Not being a complete solution, it already achieves some level of reducing allocations and memory copying and constitutes ground work for applying this across the library.

Some normalization of reading and writing bytes using BinaryPrimitives. This can be improved by creating helper methods that read/write and advance to make the code more readable and error prone, but I left that for later.

Enhanced performance and memory efficiency throughout the SIPSorcery project by replacing traditional byte array handling with Span<byte> and ReadOnlySpan<byte> in multiple components, including RTCP, SCTP, and AudioEncoder classes.
Refactored packet parsing and serialization logic using BinaryPrimitives for endian conversions, and updated NetConvert accordingly.
Introduced utility methods in EncodingExtensions, TypeExtensions, BinaryOperations, and MemoryOperations to streamline string, byte array, and endian-aware operations.
Marked legacy methods as obsolete to guide migration to span-based APIs.
Updated SIPSorcery.csproj to include System.Memory and Microsoft.Bcl.HashCode packages, and enabled AllowUnsafeBlocks for performance-critical code paths.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants