11using System ;
22using System . Globalization ;
33using System . Linq ;
4+ using System . Threading ;
45using LibGit2Sharp . Core ;
56
67namespace LibGit2Sharp
@@ -14,6 +15,10 @@ public sealed class ObjectId : IEquatable<ObjectId>
1415 private const int rawSize = GitOid . Size ;
1516 private readonly string sha ;
1617
18+ // The bytes live inline in oid. This array is only created when RawId is read,
19+ // so ids that never expose their raw bytes don't carry a second copy of them.
20+ private byte [ ] rawId ;
21+
1722 /// <summary>
1823 /// Size of the string-based representation of a SHA-1.
1924 /// </summary>
@@ -37,24 +42,27 @@ public sealed class ObjectId : IEquatable<ObjectId>
3742 /// <param name="oid">The oid.</param>
3843 internal ObjectId ( GitOid oid )
3944 {
40- if ( oid . Id == null || oid . Id . Length != rawSize )
41- {
42- throw new ArgumentException ( string . Format ( CultureInfo . InvariantCulture , "A non null array of {0} bytes is expected." , rawSize ) , nameof ( oid ) ) ;
43- }
44-
4545 this . oid = oid ;
46- sha = ToString ( oid . Id , oid . Id . Length * 2 ) ;
46+ sha = ToString ( oid , HexSize ) ;
4747 }
4848
4949 /// <summary>
5050 /// Initializes a new instance of the <see cref="ObjectId"/> class.
5151 /// </summary>
5252 /// <param name="rawId">The byte array.</param>
53- public ObjectId ( byte [ ] rawId )
54- : this ( new GitOid { Id = rawId } )
53+ public unsafe ObjectId ( byte [ ] rawId )
5554 {
5655 Ensure . ArgumentNotNull ( rawId , "rawId" ) ;
5756 Ensure . ArgumentConformsTo ( rawId , b => b . Length == rawSize , "rawId" ) ;
57+
58+ fixed ( byte * source = rawId )
59+ {
60+ oid = * ( GitOid * ) source ;
61+ }
62+
63+ // Keep the caller's array so RawId keeps returning it, as it always has.
64+ this . rawId = rawId ;
65+ sha = ToString ( oid , HexSize ) ;
5866 }
5967
6068 internal static unsafe ObjectId BuildFromPtr( IntPtr ptr )
@@ -69,18 +77,8 @@ internal static unsafe ObjectId BuildFromPtr(git_oid* id)
6977
7078 internal unsafe ObjectId( byte * rawId )
7179 {
72- byte [ ] id = new byte [ GitOid . Size ] ;
73-
74- fixed ( byte * p = id )
75- {
76- for ( int i = 0 ; i < rawSize ; i ++ )
77- {
78- p [ i ] = rawId [ i ] ;
79- }
80- }
81-
82- this . oid = new GitOid { Id = id } ;
83- this . sha = ToString ( oid . Id , oid . Id . Length * 2 ) ;
80+ oid = * ( GitOid * ) rawId ;
81+ sha = ToString ( oid , HexSize ) ;
8482 }
8583
8684 /// <summary>
@@ -110,7 +108,28 @@ internal GitOid Oid
110108 /// </summary>
111109 public byte [ ] RawId
112110 {
113- get { return oid . Id ; }
111+ get
112+ {
113+ if ( rawId == null )
114+ {
115+ // CompareExchange keeps every caller on the same array if two threads race here.
116+ Interlocked . CompareExchange ( ref rawId , CreateRawId ( ) , null ) ;
117+ }
118+
119+ return rawId;
120+ }
121+ }
122+
123+ private unsafe byte [ ] CreateRawId ( )
124+ {
125+ var bytes = new byte [ rawSize ] ;
126+
127+ fixed ( byte * destination = bytes)
128+ {
129+ * ( GitOid * ) destination = oid ;
130+ }
131+
132+ return bytes;
114133 }
115134
116135 /// <summary>
@@ -257,35 +276,43 @@ private static byte[] BuildReverseHexDigits()
257276 return bytes ;
258277 }
259278
260- internal static string ToString ( byte [ ] id , int lengthInNibbles )
279+ internal static unsafe string ToString ( GitOid id , int lengthInNibbles )
261280 {
262281 // Inspired from http://stackoverflow.com/questions/623104/c-byte-to-hex-string/3974535#3974535
263282
283+ // Indexing the inline buffer isn't bounds checked, so check the length up front.
284+ if ( ( uint ) lengthInNibbles > HexSize)
285+ {
286+ throw new ArgumentOutOfRangeException( nameof ( lengthInNibbles ) ) ;
287+ }
288+
264289 var c = new char [ lengthInNibbles ] ;
265290
266291 for ( int i = 0 ; i < ( lengthInNibbles & - 2 ) ; i ++ )
267292 {
268293 int index0 = i >> 1 ;
269- var b = ( ( byte ) ( id [ index0 ] >> 4 ) ) ;
294+ var b = ( ( byte ) ( id . Id [ index0 ] > > 4 ) ) ;
270295 c [ i ++ ] = hexDigits[ b] ;
271296
272- b = ( ( byte ) ( id [ index0 ] & 0x0F ) ) ;
297+ b = ( ( byte ) ( id. Id [ index0] & 0x0F ) ) ;
273298 c[ i] = hexDigits[ b] ;
274299 }
275300
276301 if ( ( lengthInNibbles & 1 ) == 1 )
277302 {
278303 int index0 = lengthInNibbles >> 1 ;
279- var b = ( ( byte ) ( id [ index0 ] >> 4 ) ) ;
304+ var b = ( ( byte ) ( id . Id [ index0 ] > > 4 ) ) ;
280305 c [ lengthInNibbles - 1 ] = hexDigits[ b] ;
281306 }
282307
283308 return new string ( c) ;
284309 }
285310
286- private static GitOid ToOid ( string sha )
311+ // Only called once LooksValid has checked sha is exactly HexSize characters,
312+ // which keeps the writes inside the 20 byte buffer.
313+ private static unsafe GitOid ToOid( string sha )
287314 {
288- var bytes = new byte [ rawSize ] ;
315+ var oid = new GitOid ( ) ;
289316
290317 if ( ( sha. Length & 1 ) == 1 )
291318 {
@@ -297,10 +324,9 @@ private static GitOid ToOid(string sha)
297324 int c1 = byteConverter( sha[ i++ ] ) << 4 ;
298325 int c2 = byteConverter( sha[ i] ) ;
299326
300- bytes [ i >> 1 ] = ( byte ) ( c1 + c2 ) ;
327+ oid . Id [ i >> 1 ] = ( byte ) ( c1 + c2) ;
301328 }
302329
303- var oid = new GitOid { Id = bytes } ;
304330 return oid;
305331 }
306332
0 commit comments