Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions LibGit2Sharp/BlameHunkCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ 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;
}
}

if (options.StoppingAt != null)
{
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;
}
}

Expand Down
13 changes: 7 additions & 6 deletions LibGit2Sharp/Core/GitOid.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Runtime.InteropServices;

namespace LibGit2Sharp.Core
namespace LibGit2Sharp.Core
{
internal struct git_oid
{
Expand All @@ -11,7 +9,11 @@ internal struct git_oid
/// <summary>
/// Represents a unique id in git which is the sha1 hash of this id's content.
/// </summary>
internal struct GitOid
/// <remarks>
/// 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.
/// </remarks>
internal unsafe struct GitOid
{
/// <summary>
/// Number of bytes in the Id.
Expand All @@ -21,8 +23,7 @@ internal struct GitOid
/// <summary>
/// The raw binary 20 byte Id.
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = Size)]
public byte[] Id;
public fixed byte Id[Size];

public static implicit operator ObjectId(GitOid oid)
{
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
84 changes: 55 additions & 29 deletions LibGit2Sharp/ObjectId.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Globalization;
using System.Linq;
using System.Threading;
using LibGit2Sharp.Core;

namespace LibGit2Sharp
Expand All @@ -14,6 +15,10 @@ public sealed class ObjectId : IEquatable<ObjectId>
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;

/// <summary>
/// Size of the string-based representation of a SHA-1.
/// </summary>
Expand All @@ -37,24 +42,27 @@ public sealed class ObjectId : IEquatable<ObjectId>
/// <param name="oid">The oid.</param>
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);
}

/// <summary>
/// Initializes a new instance of the <see cref="ObjectId"/> class.
/// </summary>
/// <param name="rawId">The byte array.</param>
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)
Expand All @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -110,7 +108,28 @@ internal GitOid Oid
/// </summary>
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;
}

/// <summary>
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
}

Expand Down
17 changes: 7 additions & 10 deletions LibGit2Sharp/OdbBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down