8000 How about dropping ParentCount and promoting Parents.Count()? · libgit2/libgit2sharp@bd343d3 · GitHub
[go: up one dir, main page]

Skip to content

Commit bd343d3

Browse files
committed
How about dropping ParentCount and promoting Parents.Count()?
Output from LibGit2Sharp.Tests.BranchFixture.Parents: Commits: 42000 Time: 2834 MaxParents: 8 Commits: 42000 Time: 3491 MaxParents: 8
1 parent c2f6e5e commit bd343d3

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

LibGit2Sharp.Tests/BranchFixture.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
24
using System.Linq;
35
using LibGit2Sharp.Tests.TestHelpers;
46
using Xunit;
@@ -10,6 +12,63 @@ public class BranchFixture : BaseFixture
1012
{
1113
private readonly string[] expectedBranches = new[] { "br2", "master", "packed", "packed-test", "test", };
1214

15+
[Fact]
16+
public void Parents()
17+
{
18+
using (var repo = new Repository(@"D:\temp\linux\linux"))
19+
{
20+
int max = 42000;
21+
var commits = new List<NewCommit>();
22+
int iter = 0;
23+
var s = new Stack<ObjectId>();
24+
25+
var id = repo.Head.Tip.Id;
26+
27+
s.Push(id);
28+
29+
while (s.Count > 0 && iter < max)
30+
{
31+
var c = new NewCommit(repo, s.Pop());
32+
33+
var p = c.Parents.FirstOrDefault();
34+
s.Push(p.Id);
35+
36+
commits.Add(new NewCommit(repo, c.Id));
37+
iter++;
38+
}
39+
40+
s.Clear();
41+
s = null;
42+
43+
GC.Collect();
44+
45+
MaxNumberOfParents(commits, x => x.ParentsCount);
46+
GC.Collect();
47+
48+
MaxNumberOfParents(commits, x => x.Parents.Count());
49+
}
50+
}
51+
52+
private void MaxNumberOfParents(List<NewCommit> commits, Func<NewCommit, int> parentCountEvaluator)
53+
{
54+
int maxP = -1;
55+
56+
var sw = Stopwatch.StartNew();
57+
foreach (var newCommit in commits)
58+
{
59+
int count = parentCountEvaluator(newCommit);
60+
if (maxP > count)
61+
continue;
62+
63+
maxP = count;
64+
}
65+
sw.Stop();
66+
67+
Console.WriteLine("Commits: {0}", commits.Count);
68+
Console.WriteLine("Time: {0}", sw.ElapsedMilliseconds);
69+
Console.WriteLine("MaxParents: {0}", maxP);
70+
}
71+
1372
[Theory]
1473
[InlineData("unit_test")]
1574
[InlineData("Ångström")]

LibGit2Sharp/NewCommit.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class NewCommit : GitObject
1212
private readonly LazyGroup group1;
1313
private readonly LazyGroup group2;
1414

15-
private readonly Lazy<IEnumerable<NewCommit>> parents;
15+
private readonly Lazy<IList<NewCommit>> parents;
1616
private readonly LazyProperty<string> _lazyMessage;
1717
private readonly LazyProperty<string> _lazyEncoding;
1818
private readonly LazyProperty<Signature> _lazyAuthor;
@@ -22,7 +22,7 @@ public class NewCommit : GitObject
2222
protected NewCommit()
2323
{}
2424

25-
protected NewCommit(Repository repo, ObjectId id)
25+
public NewCommit(Repository repo, ObjectId id)
2626
: base(id)
2727
{
2828
this.repo = repo;
@@ -36,7 +36,7 @@ protected NewCommit(Repository repo, ObjectId id)
3636
_lazyEncoding = group2.AddLazy<string>(RetrieveEncodingOf);
3737
_lazyCommitter = group2.AddLazy<Signature>(Proxy.git_commit_committer);
3838

39-
parents = new Lazy<IEnumerable<NewCommit>>(() => RetrieveParentsOfCommit(id));
39+
parents = new Lazy<IList<NewCommit>>(() => RetrieveParentsOfCommit(id));
4040
}
4141

4242
// Lazy batch loaded properies
@@ -62,18 +62,22 @@ private static string RetrieveEncodingOf(GitObjectSafeHandle obj)
6262
return encoding ?? "UTF-8";
6363
}
6464

65-
private IEnumerable<NewCommit> RetrieveParentsOfCommit(ObjectId oid)
65+
private IList<NewCommit> RetrieveParentsOfCommit(ObjectId oid)
6666
{
67+
var parents = new List<NewCommit>();
68+
6769
using (var obj = new ObjectSafeWrapper(oid, repo.Handle))
6870
{
6971
int parentsCount = Proxy.git_commit_parentcount(obj);
7072

7173
for (uint i = 0; i < parentsCount; i++)
7274
{
7375
ObjectId parentCommitId = Proxy.git_commit_parent_oid(obj.ObjectPtr, i);
74-
yield return new NewCommit(repo, parentCommitId);
76+
parents.Add(new NewCommit(repo, parentCommitId));
7577
}
7678
}
79+
80+
return parents;
7781
}
7882
}
7983
}

0 commit comments

Comments
 (0)
0