Skip to content

Commit

Permalink
Avoid an array copy and List<T> allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Apr 29, 2021
1 parent 8dab6c3 commit 0303d96
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class GitRepository : IDisposable
{
private const string HeadFileName = "HEAD";
private const string GitDirectoryName = ".git";
private readonly Lazy<GitPack[]> packs;
private readonly Lazy<ReadOnlyMemory<GitPack>> packs;

/// <summary>
/// UTF-16 encoded string.
Expand Down Expand Up @@ -137,7 +137,7 @@ public GitRepository(string workingDirectory, string gitDirectory, string common
this.objectPathBuffer[this.ObjectDirectory.Length + 3] = '/';
this.objectPathBuffer[pathLengthInChars - 1] = '\0'; // Make sure to initialize with zeros

this.packs = new Lazy<GitPack[]>(this.LoadPacks);
this.packs = new Lazy<ReadOnlyMemory<GitPack>>(this.LoadPacks);
}

// TODO: read from Git settings
Expand Down Expand Up @@ -375,7 +375,7 @@ public GitCommit GetCommit(GitObjectId sha, bool readAuthor = false)

var hex = ConvertHexStringToByteArray(objectish);

foreach (var pack in this.packs.Value)
foreach (var pack in this.packs.Value.Span)
{
var objectId = pack.Lookup(hex, endsWithHalfByte);

Expand Down Expand Up @@ -514,7 +514,7 @@ public bool TryGetObjectBySha(GitObjectId sha, string objectType, out Stream? va
}
#endif

foreach (var pack in this.packs.Value)
foreach (var pack in this.packs.Value.Span)
{
if (pack.TryGetObject(sha, objectType, out value))
{
Expand Down Expand Up @@ -563,7 +563,7 @@ public string GetCacheStatistics()
builder.AppendLine();
#endif

foreach (var pack in this.packs.Value)
foreach (var pack in this.packs.Value.Span)
{
pack.GetCacheStatistics(builder);
}
Expand All @@ -582,7 +582,7 @@ public void Dispose()
{
if (this.packs.IsValueCreated)
{
foreach (var pack in this.packs.Value)
foreach (var pack in this.packs.Value.Span)
{
pack.Dispose();
}
Expand Down Expand Up @@ -638,7 +638,7 @@ private GitObjectId ResolveReference(object reference)
}
}

private GitPack[] LoadPacks()
private ReadOnlyMemory<GitPack> LoadPacks()
{
var packDirectory = Path.Combine(this.ObjectDirectory, "pack/");

Expand All @@ -648,24 +648,23 @@ private GitPack[] LoadPacks()
}

var indexFiles = Directory.GetFiles(packDirectory, "*.idx");
List<GitPack> packs = new List<GitPack>(indexFiles.Length);
var packs = new GitPack[indexFiles.Length];
int addCount = 0;

for (int i = 0; i < indexFiles.Length; i++)
{

var name = Path.GetFileNameWithoutExtension(indexFiles[i]);
var indexPath = Path.Combine(this.ObjectDirectory, "pack", $"{name}.idx");
var packPath = Path.Combine(this.ObjectDirectory, "pack", $"{name}.pack");

// Only proceed if both the packfile and index file exist.
if (File.Exists(packPath))
{
packs.Add(new GitPack(this.GetObjectBySha, indexPath, packPath));
packs[addCount++] = new GitPack(this.GetObjectBySha, indexPath, packPath);
}

}

return packs.ToArray();
return packs.AsMemory(0, addCount);
}

private static string TrimEndingDirectorySeparator(string path)
Expand Down

0 comments on commit 0303d96

Please sign in to comment.