Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code clean up around TryWriteBigEndian/TryWriteLittleEndian #110897

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Dec 23, 2024

Contributes to #94941

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

@xtqqczze
Copy link
Contributor

I wonder whether we could refactor so that the primitive types have internal methods for the implementation, e.g.:

namespace System.Buffers.Binary
{
    public static partial class BinaryPrimitives
    {
        public static void WriteDoubleBigEndian(Span<byte> destination, double value) => value.WriteBigEndian(destination);
    }
}

namespace System
{
    public readonly struct Double
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        internal void WriteBigEndian(Span<byte> destination)
        {
            if (BitConverter.IsLittleEndian)
            {
                long tmp = BinaryPrimitives.ReverseEndianness(BitConverter.DoubleToInt64Bits(m_value));
                MemoryMarshal.Write(destination, in tmp);
            }
            else
            {
                MemoryMarshal.Write(destination, in m_value);
            }
        }
    }
}

@EgorBo
Copy link
Member Author

EgorBo commented Dec 23, 2024

I wonder whether we could refactor so that the primitive types have internal methods for the implementation, e.g.:

Not sure this improves anything to be honest. The main motivation behind this change is to remove unsafe code (in this case, duplicated unsafe code).

@EgorBo EgorBo requested a review from tannergooding December 23, 2024 13:46
@MichalPetryka
Copy link
Contributor

@MihuBot

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.

3 participants