Skip to content

Slightly improve performance and maintainability in SequentialGuidValueGenerator #35981

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions src/EFCore/ValueGeneration/SequentialGuidValueGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers.Binary;
using System.Runtime.InteropServices;

namespace Microsoft.EntityFrameworkCore.ValueGeneration;
Expand Down Expand Up @@ -35,17 +36,29 @@ public class SequentialGuidValueGenerator : ValueGenerator<Guid>
/// <returns>The value to be assigned to a property.</returns>
public override Guid Next(EntityEntry entry)
{
Span<byte> guidBytes = stackalloc byte[16];
var succeeded = Guid.NewGuid().TryWriteBytes(guidBytes);
Check.DebugAssert(succeeded, "Could not write Guid to Span");
var incrementedCounter = Interlocked.Increment(ref _counter);
Span<byte> counterBytes = stackalloc byte[sizeof(long)];
MemoryMarshal.Write(counterBytes, in incrementedCounter);
var guid = Guid.NewGuid();

if (!BitConverter.IsLittleEndian)
{
counterBytes.Reverse();
}
var counter = BitConverter.IsLittleEndian
? Interlocked.Increment(ref _counter)
: BinaryPrimitives.ReverseEndianness(Interlocked.Increment(ref _counter));

var counterBytes = MemoryMarshal.AsBytes(
new ReadOnlySpan<long>(in counter));

// Guid uses a sequential layout where the first 8 bytes (_a, _b, _c)
// are subject to byte-swapping on big-endian systems when reading from
// or writing to a byte array (e.g., via MemoryMarshal or Guid constructors).
// The remaining 8 bytes (_d through _k) are interpreted as-is,
// regardless of endianness.
//
// Since we only modify the last 8 bytes of the Guid (bytes 8–15),
// byte order does not affect the result.
//
// This allows us to safely use MemoryMarshal.AsBytes to directly access
// and modify the Guid's underlying bytes without any extra conversions,
// which also slightly improves performance on big-endian architectures.
var guidBytes = MemoryMarshal.AsBytes(
new Span<Guid>(ref guid));

guidBytes[08] = counterBytes[1];
guidBytes[09] = counterBytes[0];
Expand All @@ -56,7 +69,7 @@ public override Guid Next(EntityEntry entry)
guidBytes[14] = counterBytes[3];
guidBytes[15] = counterBytes[2];

return new Guid(guidBytes);
return guid;
}

/// <summary>
Expand Down