Skip to content

Make GitOid blittable and store ObjectId bytes inline - #2199

Open
SimonCropp wants to merge 1 commit into
libgit2:masterfrom
SimonCropp:blittable-gitoid
Open

SimonCropp wants to merge 1 commit into
libgit2:masterfrom
SimonCropp:blittable-gitoid

Conversation

@SimonCropp

Copy link
Copy Markdown

GitOid held its 20 bytes in a managed byte[] marshaled as ByValArray. That made it non-blittable, so every P/Invoke taking a ref GitOid or out GitOid (about 50 signatures, including git_object_lookup) went through a marshaling stub, and each ref call allocated a new 20 byte array when copying the result back.

Changes

  • GitOid stores its bytes in a fixed buffer, the same layout as the existing git_oid, so those calls now pass a pointer straight through.
  • ObjectId keeps the GitOid inline instead of holding a separate array.
  • ObjectId.RawId creates its array on first access and returns the same array on every later call, as before. An ObjectId built from a byte[] keeps returning that array.
  • OdbBackend, BlameHunkCollection and Index copy the GitOid directly instead of going through RawId and Marshal.Copy.
  • ObjectId.ToString(GitOid, int) checks the length before reading, since indexing a fixed buffer isn't bounds checked. OdbBackend passes 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, while Sha and equality kept the old value.
  • new ObjectId((byte[])null) now throws ArgumentNullException from the existing Ensure.ArgumentNotNull check. Before, an internal check ran first and threw a plain ArgumentException. ArgumentNullException derives from ArgumentException, so existing catch blocks still work.
  • new ObjectId(byte[]) allocates 24 bytes more, because every ObjectId now 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:

Benchmark net11.0 net472
Walk 1,000 commits 1.87 MB → 1.81 MB 1.91 MB → 1.84 MB
Look up 1,000 commits by id 1.59 MB → 1.55 MB 1.62 MB → 1.58 MB
new ObjectId(string) 80 B → 56 B 112 B → 88 B
ObjectId.TryParse 288 B → 264 B 329 B → 305 B
new ObjectId(byte[]) 240 B → 264 B 249 B → 273 B

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, including ObjectId.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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant