diff --git a/LibGit2Sharp/BlameHunkCollection.cs b/LibGit2Sharp/BlameHunkCollection.cs index 2766ee7a6..b0509d3a6 100644 --- a/LibGit2Sharp/BlameHunkCollection.cs +++ b/LibGit2Sharp/BlameHunkCollection.cs @@ -37,7 +37,7 @@ internal unsafe BlameHunkCollection(Repository repo, RepositoryHandle repoHandle { fixed (byte* p = rawopts.newest_commit.Id) { - Marshal.Copy(repo.Committish(options.StartingAt).Oid.Id, 0, new IntPtr(p), git_oid.Size); + *(GitOid*)p = repo.Committish(options.StartingAt).Oid; } } @@ -45,7 +45,7 @@ internal unsafe BlameHunkCollection(Repository repo, RepositoryHandle repoHandle { fixed (byte* p = rawopts.oldest_commit.Id) { - Marshal.Copy(repo.Committish(options.StoppingAt).Oid.Id, 0, new IntPtr(p), git_oid.Size); + *(GitOid*)p = repo.Committish(options.StoppingAt).Oid; } } diff --git a/LibGit2Sharp/Core/GitOid.cs b/LibGit2Sharp/Core/GitOid.cs index f466621b1..7db2d2c17 100644 --- a/LibGit2Sharp/Core/GitOid.cs +++ b/LibGit2Sharp/Core/GitOid.cs @@ -1,6 +1,4 @@ -using System.Runtime.InteropServices; - -namespace LibGit2Sharp.Core +namespace LibGit2Sharp.Core { internal struct git_oid { @@ -11,7 +9,11 @@ internal struct git_oid /// /// Represents a unique id in git which is the sha1 hash of this id's content. /// - internal struct GitOid + /// + /// The bytes are stored inline, so the struct is blittable: it is passed to and + /// from libgit2 without being copied or allocating a managed array. + /// + internal unsafe struct GitOid { /// /// Number of bytes in the Id. @@ -21,8 +23,7 @@ internal struct GitOid /// /// The raw binary 20 byte Id. /// - [MarshalAs(UnmanagedType.ByValArray, SizeConst = Size)] - public byte[] Id; + public fixed byte Id[Size]; public static implicit operator ObjectId(GitOid oid) { diff --git a/LibGit2Sharp/Index.cs b/LibGit2Sharp/Index.cs index 321673606..bb6c2a11d 100644 --- a/LibGit2Sharp/Index.cs +++ b/LibGit2Sharp/Index.cs @@ -249,7 +249,7 @@ private unsafe void AddEntryToTheIndex(string path, ObjectId id, Mode mode) mode = (uint)mode, path = (char*)pathPtr, }; - Marshal.Copy(id.RawId, 0, new IntPtr(indexEntry.id.Id), GitOid.Size); + *(GitOid*)indexEntry.id.Id = id.Oid; Proxy.git_index_add(handle, &indexEntry); EncodingMarshaler.Cleanup(pathPtr); diff --git a/LibGit2Sharp/ObjectId.cs b/LibGit2Sharp/ObjectId.cs index d87bbcb34..3806fedfc 100644 --- a/LibGit2Sharp/ObjectId.cs +++ b/LibGit2Sharp/ObjectId.cs @@ -1,6 +1,7 @@ using System; using System.Globalization; using System.Linq; +using System.Threading; using LibGit2Sharp.Core; namespace LibGit2Sharp @@ -14,6 +15,10 @@ public sealed class ObjectId : IEquatable private const int rawSize = GitOid.Size; private readonly string sha; + // The bytes live inline in oid. This array is only created when RawId is read, + // so ids that never expose their raw bytes don't carry a second copy of them. + private byte[] rawId; + /// /// Size of the string-based representation of a SHA-1. /// @@ -37,24 +42,27 @@ public sealed class ObjectId : IEquatable /// The oid. internal ObjectId(GitOid oid) { - if (oid.Id == null || oid.Id.Length != rawSize) - { - throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "A non null array of {0} bytes is expected.", rawSize), nameof(oid)); - } - this.oid = oid; - sha = ToString(oid.Id, oid.Id.Length * 2); + sha = ToString(oid, HexSize); } /// /// Initializes a new instance of the class. /// /// The byte array. - public ObjectId(byte[] rawId) - : this(new GitOid { Id = rawId }) + public unsafe ObjectId(byte[] rawId) { Ensure.ArgumentNotNull(rawId, "rawId"); Ensure.ArgumentConformsTo(rawId, b => b.Length == rawSize, "rawId"); + + fixed (byte* source = rawId) + { + oid = *(GitOid*)source; + } + + // Keep the caller's array so RawId keeps returning it, as it always has. + this.rawId = rawId; + sha = ToString(oid, HexSize); } internal static unsafe ObjectId BuildFromPtr(IntPtr ptr) @@ -69,18 +77,8 @@ internal static unsafe ObjectId BuildFromPtr(git_oid* id) internal unsafe ObjectId(byte* rawId) { - byte[] id = new byte[GitOid.Size]; - - fixed (byte* p = id) - { - for (int i = 0; i < rawSize; i++) - { - p[i] = rawId[i]; - } - } - - this.oid = new GitOid { Id = id }; - this.sha = ToString(oid.Id, oid.Id.Length * 2); + oid = *(GitOid*)rawId; + sha = ToString(oid, HexSize); } /// @@ -110,7 +108,28 @@ internal GitOid Oid /// public byte[] RawId { - get { return oid.Id; } + get + { + if (rawId == null) + { + // CompareExchange keeps every caller on the same array if two threads race here. + Interlocked.CompareExchange(ref rawId, CreateRawId(), null); + } + + return rawId; + } + } + + private unsafe byte[] CreateRawId() + { + var bytes = new byte[rawSize]; + + fixed (byte* destination = bytes) + { + *(GitOid*)destination = oid; + } + + return bytes; } /// @@ -257,35 +276,43 @@ private static byte[] BuildReverseHexDigits() return bytes; } - internal static string ToString(byte[] id, int lengthInNibbles) + internal static unsafe string ToString(GitOid id, int lengthInNibbles) { // Inspired from http://stackoverflow.com/questions/623104/c-byte-to-hex-string/3974535#3974535 + // Indexing the inline buffer isn't bounds checked, so check the length up front. + if ((uint)lengthInNibbles > HexSize) + { + throw new ArgumentOutOfRangeException(nameof(lengthInNibbles)); + } + var c = new char[lengthInNibbles]; for (int i = 0; i < (lengthInNibbles & -2); i++) { int index0 = i >> 1; - var b = ((byte)(id[index0] >> 4)); + var b = ((byte)(id.Id[index0] >> 4)); c[i++] = hexDigits[b]; - b = ((byte)(id[index0] & 0x0F)); + b = ((byte)(id.Id[index0] & 0x0F)); c[i] = hexDigits[b]; } if ((lengthInNibbles & 1) == 1) { int index0 = lengthInNibbles >> 1; - var b = ((byte)(id[index0] >> 4)); + var b = ((byte)(id.Id[index0] >> 4)); c[lengthInNibbles - 1] = hexDigits[b]; } return new string(c); } - private static GitOid ToOid(string sha) + // Only called once LooksValid has checked sha is exactly HexSize characters, + // which keeps the writes inside the 20 byte buffer. + private static unsafe GitOid ToOid(string sha) { - var bytes = new byte[rawSize]; + var oid = new GitOid(); if ((sha.Length & 1) == 1) { @@ -297,10 +324,9 @@ private static GitOid ToOid(string sha) int c1 = byteConverter(sha[i++]) << 4; int c2 = byteConverter(sha[i]); - bytes[i >> 1] = (byte)(c1 + c2); + oid.Id[i >> 1] = (byte)(c1 + c2); } - var oid = new GitOid { Id = bytes }; return oid; } diff --git a/LibGit2Sharp/OdbBackend.cs b/LibGit2Sharp/OdbBackend.cs index 645d0ac5f..4e3c9e2b6 100644 --- a/LibGit2Sharp/OdbBackend.cs +++ b/LibGit2Sharp/OdbBackend.cs @@ -326,7 +326,7 @@ private unsafe static int ReadPrefix( try { - var shortSha = ObjectId.ToString(short_oid.Id, (int)len); + var shortSha = ObjectId.ToString(short_oid, (int)len); ObjectId oid; ObjectType objectType; @@ -343,7 +343,7 @@ private unsafe static int ReadPrefix( return (int)GitErrorCode.Error; } - out_oid.Id = oid.RawId; + out_oid = oid.Oid; len_p = new UIntPtr((ulong)memoryStream.Capacity); type_p = objectType.ToGitObjectType(); @@ -536,14 +536,14 @@ private static int ExistsPrefix( try { ObjectId found; - var shortSha = ObjectId.ToString(short_oid.Id, (int)len); + var shortSha = ObjectId.ToString(short_oid, (int)len); - found_oid.Id = ObjectId.Zero.RawId; + found_oid = ObjectId.Zero.Oid; int result = odbBackend.ExistsPrefix(shortSha, out found); if (result == (int)GitErrorCode.Ok) { - found_oid.Id = found.RawId; + found_oid = found.Oid; } return result; @@ -616,12 +616,9 @@ public ForeachState(GitOdbBackend.foreach_callback_callback cb, IntPtr data) private unsafe int CallbackMethod(ObjectId id) { - var oid = id.RawId; + GitOid oid = id.Oid; - fixed (void* ptr = &oid[0]) - { - return cb(new IntPtr(ptr), data); - } + return cb(new IntPtr(&oid), data); } public readonly ForEachCallback ManagedCallback;