Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Feb 3, 2023. It is now read-only.

Commit 35e9a66

Browse files
author
Kim Gybels
committed
Improve marshaling of git oid
Doing a simple copy of the byte array is a lot faster than using Marshal.PtrToStructure.
1 parent 6ff7bf8 commit 35e9a66

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

LibGit2Sharp/Core/GitOid.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ namespace LibGit2Sharp.Core
77
/// </summary>
88
internal struct GitOid
99
{
10+
/// <summary>
11+
/// Number of bytes in the Id.
12+
/// </summary>
13+
public const int Size = 20;
14+
1015
/// <summary>
1116
/// The raw binary 20 byte Id.
1217
/// </summary>
13-
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
18+
[MarshalAs(UnmanagedType.ByValArray, SizeConst = Size)]
1419
public byte[] Id;
1520

1621
public static implicit operator ObjectId(GitOid oid)

LibGit2Sharp/Core/Handles/OidSafeHandle.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ internal class OidSafeHandle : NotOwnedSafeHandleBase
66
{
77
private GitOid? MarshalAsGitOid()
88
{
9-
return IsInvalid ? null : (GitOid?)handle.MarshalAs<GitOid>();
9+
return IsInvalid ? null : (GitOid?)MarshalAsGitOid(handle);
10+
}
11+
12+
private static GitOid MarshalAsGitOid(System.IntPtr data)
13+
{
14+
var gitOid = new GitOid { Id = new byte[GitOid.Size] };
15+
Marshal.Copy(data, gitOid.Id, 0, GitOid.Size);
16+
return gitOid;
1017
}
1118

1219
public ObjectId MarshalAsObjectId()

0 commit comments

Comments
 (0)