Skip to content

Reduce allocations in diff callbacks and ObjectId - #2198

Open
SimonCropp wants to merge 4 commits into
libgit2:masterfrom
SimonCropp:reduce-diff-allocations
Open

SimonCropp wants to merge 4 commits into
libgit2:masterfrom
SimonCropp:reduce-diff-allocations

Conversation

@SimonCropp

@SimonCropp SimonCropp commented Nov 26, 2026 •

Copy link
Copy Markdown

Diffs and ObjectId construction allocated a lot of short-lived garbage. This removes most of it and adds a benchmark project to measure it.

Changes

  • Diff callbacks: the hunk and line callbacks now receive pointers to blittable git_diff_hunk / git_diff_line structs. Previously each line marshaled two class instances plus a copy of the 128 byte hunk header.
  • ContentChanges: uses constant +, - and prefixes instead of Encoding.ASCII.GetString(new[] { ... }) per line.
  • Patch: no longer concatenates the prefix and line into a new string, and keeps the entry being printed instead of decoding the file path and looking it up for every line. Every line git_patch_print emits belongs to the delta added just before it.
  • ObjectId: the hex string is built from a stackalloc buffer (with a length guard, since OdbBackend passes a libgit2-supplied length), sha validation is a plain loop instead of LINQ, and TryParse reuses the parsed sha instead of re-encoding it.
  • LibGit2Sharp.Benchmarks: new BenchmarkDotNet project (net472 and net11.0) covering ObjectId construction, a 1,000 commit walk, looking up 1,000 commits by id, and diffs of a 10,000 line file where every other line changed. Because it targets net11.0, CI now installs the .NET 11 SDK for the build job and the Windows/macOS test jobs. Linux test jobs override TargetFrameworks, so they are unaffected.
  • Ignore Rider's .idea folder.

Results

Allocated per operation, before → after:

Benchmark net11.0 net472
Patch between trees 8.17 MB → 2.56 MB 9.53 MB → 3.26 MB
ContentChanges between blobs 6.44 MB → 1.98 MB 7.27 MB → 2.68 MB
ObjectId.TryParse 288 B → 80 B 329 B → 80 B
new ObjectId(byte[]) 240 B → 136 B 249 B → 144 B
new ObjectId(string) 80 B → 80 B 112 B → 80 B
Walk 1,000 commits 1.87 MB → 1.77 MB 1.91 MB → 1.81 MB

On .NET 11 the JIT already removes the LINQ enumerator in new ObjectId(string), so that one only improves on older runtimes (net8.0 also drops from 112 B to 80 B).

Run them with:

dotnet run -c Release -f net11.0 --project LibGit2Sharp.Benchmarks -- --filter *

The full test suite passes on net8.0, net472 and net9.0.

Diff hunk and line callbacks now receive pointers to blittable
git_diff_hunk and git_diff_line structs. Previously every line
marshaled two class instances plus a copy of the 128 byte hunk header.

ContentChanges uses constant prefix strings instead of building one per
line. Patch no longer concatenates the prefix and line into a new string,
and keeps the entry being printed instead of decoding the file path and
looking it up for every line.

ObjectId builds its hex string from a stackalloc buffer, validates shas
without LINQ, and TryParse reuses the parsed sha instead of re-encoding it.
LibGit2Sharp.Benchmarks uses BenchmarkDotNet to measure ObjectId
construction, commit walks and diffs, reporting allocations for each.
It targets net472 and net11.0, so CI now installs the .NET 11 SDK for
the build and Windows/macOS test jobs.
Looks up 1,000 commits by id, which passes each id to libgit2 as a ref GitOid.
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