Repository navigation
Make GitOid blittable and store ObjectId bytes inline - #2199
Open
SimonCropp wants to merge 1 commit into
Open
SimonCropp wants to merge 1 commit into
SimonCropp wants to merge 1 commit into
Conversation
GitOid held its bytes in a managed array marshaled as ByValArray, so every P/Invoke taking a ref or out GitOid went through a marshaling stub, and each ref call allocated a new 20 byte array when copying the result back. GitOid now stores the bytes in a fixed buffer, as git_oid already does, so those calls pass a pointer straight through. ObjectId keeps the GitOid inline instead of a separate array. RawId creates its array on first access, or returns the array the ObjectId was constructed from, and returns the same array on every call as before.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
GitOidheld its 20 bytes in a managedbyte[]marshaled asByValArray. That made it non-blittable, so every P/Invoke taking aref GitOidorout GitOid(about 50 signatures, includinggit_object_lookup) went through a marshaling stub, and eachrefcall allocated a new 20 byte array when copying the result back.Changes
GitOidstores its bytes in afixedbuffer, the same layout as the existinggit_oid, so those calls now pass a pointer straight through.ObjectIdkeeps theGitOidinline instead of holding a separate array.ObjectId.RawIdcreates its array on first access and returns the same array on every later call, as before. AnObjectIdbuilt from abyte[]keeps returning that array.OdbBackend,BlameHunkCollectionandIndexcopy theGitOiddirectly instead of going throughRawIdandMarshal.Copy.ObjectId.ToString(GitOid, int)checks the length before reading, since indexing a fixed buffer isn't bounds checked.OdbBackendpasses it a length that comes from libgit2.Behaviour changes
new ObjectId(byte[])copies the bytes. Changing the array afterwards no longer changes the id passed to libgit2; before, it did, whileShaand equality kept the old value.new ObjectId((byte[])null)now throwsArgumentNullExceptionfrom the existingEnsure.ArgumentNotNullcheck. Before, an internal check ran first and threw a plainArgumentException.ArgumentNullExceptionderives fromArgumentException, so existingcatchblocks still work.new ObjectId(byte[])allocates 24 bytes more, because everyObjectIdnow carries the inline bytes plus the lazily created array reference. Building from a sha or from native memory allocates 24 bytes less, and those are far more common.Results
Allocated per operation, before → after:
new ObjectId(string)ObjectId.TryParsenew ObjectId(byte[])These were measured with the benchmark project from #2198, including its commit lookup benchmark. Timings were the same before and after, within noise.
Relationship to #2198
This PR is independent of #2198, but both change
ObjectId.cs, includingObjectId.ToString. Whichever merges second needs a small rebase: there are three mechanical conflicts. I checked that the merged result builds and passes the ObjectId, diff, patch, ODB backend, blame and index tests.The full test suite passes on net8.0, net472 and net9.0.