Skip to content

Commit 0303d96

Browse files
committed
Avoid an array copy and List<T> allocation
1 parent 8dab6c3 commit 0303d96

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class GitRepository : IDisposable
1717
{
1818
private const string HeadFileName = "HEAD";
1919
private const string GitDirectoryName = ".git";
20-
private readonly Lazy<GitPack[]> packs;
20+
private readonly Lazy<ReadOnlyMemory<GitPack>> packs;
2121

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

140-
this.packs = new Lazy<GitPack[]>(this.LoadPacks);
140+
this.packs = new Lazy<ReadOnlyMemory<GitPack>>(this.LoadPacks);
141141
}
142142

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

376376
var hex = ConvertHexStringToByteArray(objectish);
377377

378-
foreach (var pack in this.packs.Value)
378+
foreach (var pack in this.packs.Value.Span)
379379
{
380380
var objectId = pack.Lookup(hex, endsWithHalfByte);
381381

@@ -514,7 +514,7 @@ public bool TryGetObjectBySha(GitObjectId sha, string objectType, out Stream? va
514514
}
515515
#endif
516516

517-
foreach (var pack in this.packs.Value)
517+
foreach (var pack in this.packs.Value.Span)
518518
{
519519
if (pack.TryGetObject(sha, objectType, out value))
520520
{
@@ -563,7 +563,7 @@ public string GetCacheStatistics()
563563
builder.AppendLine();
564564
#endif
565565

566-
foreach (var pack in this.packs.Value)
566+
foreach (var pack in this.packs.Value.Span)
567567
{
568568
pack.GetCacheStatistics(builder);
569569
}
@@ -582,7 +582,7 @@ public void Dispose()
582582
{
583583
if (this.packs.IsValueCreated)
584584
{
585-
foreach (var pack in this.packs.Value)
585+
foreach (var pack in this.packs.Value.Span)
586586
{
587587
pack.Dispose();
588588
}
@@ -638,7 +638,7 @@ private GitObjectId ResolveReference(object reference)
638638
}
639639
}
640640

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

@@ -648,24 +648,23 @@ private GitPack[] LoadPacks()
648648
}
649649

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

653654
for (int i = 0; i < indexFiles.Length; i++)
654655
{
655-
656656
var name = Path.GetFileNameWithoutExtension(indexFiles[i]);
657657
var indexPath = Path.Combine(this.ObjectDirectory, "pack", $"{name}.idx");
658658
var packPath = Path.Combine(this.ObjectDirectory, "pack", $"{name}.pack");
659659

660660
// Only proceed if both the packfile and index file exist.
661661
if (File.Exists(packPath))
662662
{
663-
packs.Add(new GitPack(this.GetObjectBySha, indexPath, packPath));
663+
packs[addCount++] = new GitPack(this.GetObjectBySha, indexPath, packPath);
664664
}
665-
666665
}
667666

668-
return packs.ToArray();
667+
return packs.AsMemory(0, addCount);
669668
}
670669

671670
private static string TrimEndingDirectorySeparator(string path)

0 commit comments

Comments
 (0)