Skip to content

Commit 24c4a8e

Browse files
committed
Don't load refspecs eagerly
If the user never asks for the refspecs, we should not spend the time and memory to load them into managed memory.
1 parent d6282d3 commit 24c4a8e

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

LibGit2Sharp/RefSpecCollection.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections;
1+
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Diagnostics;
45
using System.Globalization;
@@ -14,7 +15,9 @@ namespace LibGit2Sharp
1415
[DebuggerDisplay("{DebuggerDisplay,nq}")]
1516
public class RefSpecCollection : IEnumerable<RefSpec>
1617
{
17-
readonly IList<RefSpec> refspecs;
18+
readonly Remote remote;
19+
readonly RemoteSafeHandle handle;
20+
readonly Lazy<IList<RefSpec>> refspecs;
1821

1922
/// <summary>
2023
/// Needed for mocking purposes.
@@ -26,7 +29,10 @@ internal RefSpecCollection(Remote remote, RemoteSafeHandle handle)
2629
{
2730
Ensure.ArgumentNotNull(handle, "handle");
2831

29-
refspecs = RetrieveRefSpecs(remote, handle);
32+
this.remote = remote;
33+
this.handle = handle;
34+
35+
refspecs = new Lazy<IList<RefSpec>>(() => RetrieveRefSpecs(remote, handle));
3036
}
3137

3238
static IList<RefSpec> RetrieveRefSpecs(Remote remote, RemoteSafeHandle remoteHandle)
@@ -48,7 +54,7 @@ static IList<RefSpec> RetrieveRefSpecs(Remote remote, RemoteSafeHandle remoteHan
4854
/// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns>
4955
public virtual IEnumerator<RefSpec> GetEnumerator()
5056
{
51-
return refspecs.GetEnumerator();
57+
return refspecs.Value.GetEnumerator();
5258
}
5359

5460
/// <summary>

0 commit comments

Comments
 (0)