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

Merged
merged 3 commits into from
Dec 26, 2024

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

Unsafe.WriteUnaligned(
ref MemoryMarshal.GetReference(buffer),
BitOperations.RotateLeft(s1 * 5, 7) * 9);
MemoryMarshal.Write(buffer, BitOperations.RotateLeft(s1 * 5, 7) * 9);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this one get optimized correctly today?

The perf of Random is fairly sensitive and I believe at the time it wasn't well handled.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The codegen seesm to be the same - I've checked. We might want to make various helpers like this as "zero budget" for inliner if we hit any issues because of that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good; just wanted to confirm that it was getting handled right since its a slightly different change than all the others in the PR

@EgorBo EgorBo merged commit 194ad16 into dotnet:main Dec 26, 2024
139 checks passed
@EgorBo EgorBo deleted the cleanup-TryWriteBigEndian branch December 26, 2024 21:47
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.

5 participants