Repository navigation
Reduce allocations in diff callbacks and ObjectId - #2198
Open
SimonCropp wants to merge 4 commits into
Open
SimonCropp wants to merge 4 commits into
SimonCropp wants to merge 4 commits into
Conversation
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.
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.
Diffs and
ObjectIdconstruction allocated a lot of short-lived garbage. This removes most of it and adds a benchmark project to measure it.Changes
git_diff_hunk/git_diff_linestructs. Previously each line marshaled two class instances plus a copy of the 128 byte hunk header.ContentChanges: uses constant+,-andprefixes instead ofEncoding.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 linegit_patch_printemits belongs to the delta added just before it.ObjectId: the hex string is built from astackallocbuffer (with a length guard, sinceOdbBackendpasses a libgit2-supplied length), sha validation is a plain loop instead of LINQ, andTryParsereuses the parsed sha instead of re-encoding it.LibGit2Sharp.Benchmarks: new BenchmarkDotNet project (net472 and net11.0) coveringObjectIdconstruction, 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 overrideTargetFrameworks, so they are unaffected..ideafolder.Results
Allocated per operation, before → after:
ObjectId.TryParsenew ObjectId(byte[])new ObjectId(string)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:
The full test suite passes on net8.0, net472 and net9.0.